From 90078a0f62bfeb3ee18fe32b2cb9f26f96f9e780 Mon Sep 17 00:00:00 2001 From: Amatsugu Date: Fri, 1 May 2026 02:16:54 -0400 Subject: [PATCH] addd audio file thumbnail generation --- AobaCore/Models/Media.cs | 3 +++ AobaCore/Services/AobaService.cs | 6 +++++ AobaCore/Services/ThumbnailService.cs | 35 +++++++++++++++++++++++++++ AobaServer/Services/DebugService.cs | 9 +++---- 4 files changed, 48 insertions(+), 5 deletions(-) diff --git a/AobaCore/Models/Media.cs b/AobaCore/Models/Media.cs index a7baea5..04af6a5 100644 --- a/AobaCore/Models/Media.cs +++ b/AobaCore/Models/Media.cs @@ -36,6 +36,9 @@ public class Media { ".ico", MediaType.Image }, { ".gif", MediaType.Image }, { ".mp3", MediaType.Audio }, + { ".ogg", MediaType.Audio }, + { ".wav", MediaType.Audio }, + { ".aac", MediaType.Audio }, { ".flac", MediaType.Audio }, { ".alac", MediaType.Audio }, { ".mp4", MediaType.Video }, diff --git a/AobaCore/Services/AobaService.cs b/AobaCore/Services/AobaService.cs index 07d4e8b..0527426 100644 --- a/AobaCore/Services/AobaService.cs +++ b/AobaCore/Services/AobaService.cs @@ -87,6 +87,12 @@ public class AobaService(IMongoDatabase db) } + public async Task SetMediaTypeAsync(ObjectId mediaId, MediaType type, CancellationToken cancellationToken = default) + { + var update = Builders.Update.Set(m => m.MediaType, type); + await _media.UpdateOneAsync(m => m.MediaId == mediaId, update, null, cancellationToken); + } + public async Task> UploadFileAsync(Stream data, string filename, ObjectId owner, CancellationToken cancellationToken = default) { try diff --git a/AobaCore/Services/ThumbnailService.cs b/AobaCore/Services/ThumbnailService.cs index 7d8f0ad..fd411bd 100644 --- a/AobaCore/Services/ThumbnailService.cs +++ b/AobaCore/Services/ThumbnailService.cs @@ -119,6 +119,7 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService) { MediaType.Image => await GenerateImageThumbnailAsync(stream, size, ext, cancellationToken), MediaType.Video => GenerateVideoThumbnail(stream, size, cancellationToken), + MediaType.Audio => GenerateAudioThumbnail(stream, size, ext, cancellationToken), MediaType.Text or MediaType.Code => await GenerateDocumentThumbnailAsync(stream, size, cancellationToken), _ => new Error($"No Thumbnail for {type}"), }; @@ -156,6 +157,40 @@ public class ThumbnailService(IMongoDatabase db, AobaService aobaService) return result; } + public static Maybe GenerateAudioThumbnail(Stream data, ThumbnailSize size, string ext, CancellationToken cancellationToken = default) + { + + var w = (int)size; + var fn = ObjectId.GenerateNewId().ToString(); + var filePath = $"/tmp/{fn}{ext}"; + + using var source = new FileStream(filePath, FileMode.CreateNew); + data.CopyTo(source); + source.Flush(); + source.Dispose(); + data.Dispose(); + //ffmpeg -i test.wav -lavfi "showspectrumpic=s=512x512:legend=0:color=plasma:scale=log" output3.png + try + { + var output = new MemoryStream(); + FFMpegArguments.FromFileInput(filePath, false) + .OutputToPipe(new StreamPipeSink(output), opt => + { + opt.WithCustomArgument("-lavfi \"showspectrumpic=s=512x512:legend=0:color=plasma:scale=log\"").ForceFormat("webp"); + }).ProcessSynchronously(); + output.Position = 0; + return output; + } + catch (Exception ex) + { + return ex; + } + finally + { + File.Delete(filePath); + } + } + public static Maybe GenerateVideoThumbnail(Stream data, ThumbnailSize size, CancellationToken cancellationToken = default) { var w = (int)size; diff --git a/AobaServer/Services/DebugService.cs b/AobaServer/Services/DebugService.cs index a139822..7f72988 100644 --- a/AobaServer/Services/DebugService.cs +++ b/AobaServer/Services/DebugService.cs @@ -1,4 +1,5 @@  +using AobaCore.Models; using AobaCore.Services; namespace AobaServer.Services; @@ -7,13 +8,11 @@ public class DebugService(AobaService aobaService, ThumbnailService thumbnailSer { protected override async Task ExecuteAsync(CancellationToken stoppingToken) { - var mediaItems = await aobaService.FindMediaWithExtAsync(".avif", stoppingToken); + var mediaItems = await aobaService.FindMediaWithExtAsync(".ogg", stoppingToken); foreach (var item in mediaItems) { - foreach (var size in item.Thumbnails.Keys) - { - await thumbnailService.DeleteThumbnailAsync(item.MediaId, size); - } + if(item.MediaType != MediaType.Audio) + await aobaService.SetMediaTypeAsync(item.MediaId, MediaType.Audio); } } }