From 8964d1c0692d53e5b6f2eaa58437d08208c42e54 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Sun, 17 Aug 2025 14:57:21 -0400 Subject: [PATCH] testing using libvips --- AobaCore/Services/ThumbnailService.cs | 42 ++++++++++++--------------- AobaServer/Dockerfile | 2 +- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/AobaCore/Services/ThumbnailService.cs b/AobaCore/Services/ThumbnailService.cs index ace49f0..90a5b2c 100644 --- a/AobaCore/Services/ThumbnailService.cs +++ b/AobaCore/Services/ThumbnailService.cs @@ -16,6 +16,7 @@ using SixLabors.ImageSharp.Processing; using System; using System.Collections.Generic; +using System.Diagnostics; using System.Linq; using System.Net.Http.Headers; using System.Text; @@ -135,9 +136,6 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService) public static async Task> GenerateImageThumbnailAsync(Stream stream, ThumbnailSize size, string ext, CancellationToken cancellationToken = default) { - if(ext == ".avif") - return GenerateAvifThumbnail(stream, size, cancellationToken); - var img = LoadImage(stream, ext); if (img.HasError) return img.Error; @@ -196,32 +194,29 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService) { var w = (int)size; var fn = ObjectId.GenerateNewId().ToString(); - var filePath = $"/tmp/{fn}.in"; - using var source = new FileStream(filePath, FileMode.CreateNew); + var inFilePath = $"/tmp/{fn}.avif"; + var outFilePath = $"/tmp/{fn}.webp"; + using var source = new FileStream(inFilePath, FileMode.CreateNew); data.CopyTo(source); source.Flush(); source.Dispose(); data.Dispose(); try { + var process = Process.Start(new ProcessStartInfo + { + FileName = "vips", + Arguments = $"smartcrop \"{inFilePath}\" \"{outFilePath}\"[Q=75] {w} {w}", + WorkingDirectory = "/tmp" + }); + if (process == null) + return new Error("Failed to run vips command"); + process.WaitForExit(); + if (process.ExitCode != 0) + return new Error("Failed to convert"); var output = new MemoryStream(); - FFMpegArguments.FromFileInput(filePath, false, opt => - { - //opt.WithCustomArgument("-vf "); - }) - .OutputToPipe(new StreamPipeSink(output), opt => - { - var tonemap = ",format=gbrpf32le,zscale=primaries=bt2020:transfer=smpte2084:matrix=gbr,tonemap=hable,zscale=primaries=bt709:transfer=bt709:matrix=bt709,format=yuv420p"; - var args = $"-vf \"crop='min(in_w,in_h)':'min(in_w,in_h)',scale={w}:{w}," - + $"{tonemap}\"" - //+ "zscale=primaries=bt2020:transfer=smpte2084:matrix=bt2020nc,format=gbrpf32le," - //+ "zscale=primaries=bt709:transfer=bt709:matrix=bt709:range=tv,tonemap=hable," - //+ "zscale=matrix=bt709:transfer=bt709:primaries=bt709," - //+ "format=yuv420p\" " - + "-colorspace bt709"; - opt.WithCustomArgument(args) - .ForceFormat("webp"); - }).ProcessSynchronously(); + using var oFile = File.OpenRead(outFilePath); + oFile.CopyTo(output); output.Position = 0; return output; } @@ -231,7 +226,8 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService) } finally { - File.Delete(filePath); + File.Delete(inFilePath); + File.Delete(outFilePath); } } diff --git a/AobaServer/Dockerfile b/AobaServer/Dockerfile index 2add420..17a4602 100644 --- a/AobaServer/Dockerfile +++ b/AobaServer/Dockerfile @@ -32,7 +32,7 @@ RUN dx bundle --platform web # Server Build # This stage is used when running from VS in fast mode (Default for Debug configuration) FROM mcr.microsoft.com/dotnet/aspnet:9.0 AS base -RUN apt-get update && apt-get install -y ffmpeg +RUN apt-get update && apt-get install -y ffmpeg #libvips libvips-tools USER $APP_UID WORKDIR /app EXPOSE 8080