| | | 1 | | using Anichron.Worker.Settings; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | using SixLabors.ImageSharp; |
| | | 4 | | using SixLabors.ImageSharp.PixelFormats; |
| | | 5 | | |
| | | 6 | | namespace Anichron.Worker.Ingestion.Proxy; |
| | | 7 | | |
| | | 8 | | internal interface IVideoFrameExtractor |
| | | 9 | | { |
| | | 10 | | Task<Image<Rgb24>> ExtractAsync(string sourcePath, CancellationToken ct); |
| | | 11 | | } |
| | | 12 | | |
| | 5 | 13 | | internal sealed class FfmpegFrameExtractor(IProcessLauncher processLauncher, IOptions<WorkerSettings> options) : IVideoF |
| | | 14 | | { |
| | | 15 | | public async Task<Image<Rgb24>> ExtractAsync(string sourcePath, CancellationToken ct) |
| | 5 | 16 | | { |
| | 5 | 17 | | var frame = await ReadFrameAsync(["-ss", "1", "-i", sourcePath], ct); |
| | 4 | 18 | | if (frame.Length == 0) |
| | 3 | 19 | | frame = await ReadFrameAsync(["-i", sourcePath], ct); |
| | 3 | 20 | | if (frame.Length == 0) |
| | 1 | 21 | | throw new FfmpegException($"FFmpeg read no frame from {sourcePath}."); |
| | 2 | 22 | | return Image.Load<Rgb24>(frame); |
| | 2 | 23 | | } |
| | | 24 | | |
| | | 25 | | private Task<byte[]> ReadFrameAsync(string[] input, CancellationToken ct) |
| | 8 | 26 | | => processLauncher.RunFfmpegQuietlyAsync( |
| | 8 | 27 | | options.Value.FfmpegPath, |
| | 8 | 28 | | [.. input, "-frames:v", "1", "-pix_fmt", "rgb24", "-c:v", "png", "-f", "image2pipe", "-"], |
| | 8 | 29 | | ct); |
| | | 30 | | } |