wip video thumbnail generation

This commit is contained in:
2025-06-28 00:13:59 -04:00
parent 052a95e09b
commit 360fa53439
4 changed files with 25 additions and 4 deletions

View File

@@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="FFMpegCore" Version="5.2.0" />
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" /> <PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.5" /> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="9.0.5" />
<PackageReference Include="MaybeError" Version="1.1.0" /> <PackageReference Include="MaybeError" Version="1.1.0" />

View File

@@ -1,5 +1,8 @@
using AobaCore.Models; using AobaCore.Models;
using FFMpegCore;
using FFMpegCore.Pipes;
using MaybeError.Errors; using MaybeError.Errors;
using MongoDB.Bson; using MongoDB.Bson;
@@ -44,16 +47,18 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
try try
{ {
var mediaData = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true }, cancellationToken); using var mediaData = await _gridfs.OpenDownloadStreamAsync(media.MediaId, new GridFSDownloadOptions { Seekable = true }, cancellationToken);
var thumb = await GenerateThumbnailAsync(mediaData, size, media.MediaType, media.Ext, cancellationToken); var thumb = await GenerateThumbnailAsync(mediaData, size, media.MediaType, media.Ext, cancellationToken);
if (thumb.HasError) if (thumb.HasError)
return thumb.Error; return thumb.Error;
cancellationToken.ThrowIfCancellationRequested(); cancellationToken.ThrowIfCancellationRequested();
#if !DEBUG
var thumbId = await _gridfs.UploadFromStreamAsync($"{media.Filename}.webp", thumb, cancellationToken: CancellationToken.None); var thumbId = await _gridfs.UploadFromStreamAsync($"{media.Filename}.webp", thumb, cancellationToken: CancellationToken.None);
var update = Builders<MediaThumbnail>.Update.Set(t => t.Sizes[size], thumbId); var update = Builders<MediaThumbnail>.Update.Set(t => t.Sizes[size], thumbId);
await _thumbnails.UpdateOneAsync(t => t.Id == id, update, cancellationToken: CancellationToken.None); await _thumbnails.UpdateOneAsync(t => t.Id == id, update, cancellationToken: CancellationToken.None);
#endif
thumb.Value.Position = 0; thumb.Value.Position = 0;
return thumb; return thumb;
} catch (Exception ex) { } catch (Exception ex) {
@@ -101,6 +106,7 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
var img = Image.Load(stream); var img = Image.Load(stream);
img.Mutate(o => img.Mutate(o =>
{ {
var size =
o.Resize(new ResizeOptions o.Resize(new ResizeOptions
{ {
Position = AnchorPositionMode.Center, Position = AnchorPositionMode.Center,
@@ -116,7 +122,20 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService)
public async Task<Maybe<Stream>> GenerateVideoThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default) public async Task<Maybe<Stream>> GenerateVideoThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)
{ {
return new NotImplementedException(); var w = (int)size;
var source = new MemoryStream();
data.CopyTo(source);
source.Position = 0;
var output = new MemoryStream();
await FFMpegArguments.FromPipeInput(new StreamPipeSource(source))
.OutputToPipe(new StreamPipeSink(output), opt =>
{
opt.WithCustomArgument($"-t 5 -vf \"crop='min(in_w,in_h)':'min(in_w,in_h)',scale={w}:{w}\" -loop 0")
.ForceFormat("webp");
}).ProcessAsynchronously();
output.Position = 0;
return output;
} }
public async Task<Maybe<Stream>> GenerateDocumentThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default) public async Task<Maybe<Stream>> GenerateDocumentThumbnailAsync(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default)

View File

@@ -16,8 +16,8 @@
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>
<PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" /> <PackageReference Include="Isopoh.Cryptography.Argon2" Version="2.0.0" />
<PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.5" /> <PackageReference Include="Microsoft.AspNetCore.Authentication.JwtBearer" Version="9.0.6" />
<PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.11.0" /> <PackageReference Include="Microsoft.IdentityModel.JsonWebTokens" Version="8.12.1" />
<PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" /> <PackageReference Include="Microsoft.VisualStudio.Azure.Containers.Tools.Targets" Version="1.21.2" />
<PackageReference Include="MimeTypesMap" Version="1.0.9" /> <PackageReference Include="MimeTypesMap" Version="1.0.9" />
<PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" /> <PackageReference Include="OpenTelemetry.Exporter.OpenTelemetryProtocol" Version="1.12.0" />

View File

@@ -57,4 +57,5 @@ RUN dotnet publish "./AobaServer.csproj" -c $BUILD_CONFIGURATION -o /app/publish
FROM base AS final FROM base AS final
WORKDIR /app WORKDIR /app
COPY --from=publish /app/publish . COPY --from=publish /app/publish .
RUN sudo apt-get install -y ffmpeg libgdiplus
ENTRYPOINT ["dotnet", "AobaServer.dll"] ENTRYPOINT ["dotnet", "AobaServer.dll"]