| | | 1 | | using Anichron.Worker.Settings; |
| | | 2 | | using Microsoft.Extensions.Options; |
| | | 3 | | |
| | | 4 | | namespace Anichron.Worker.Ingestion.Proxy; |
| | | 5 | | |
| | | 6 | | internal interface IVideoProcessor |
| | | 7 | | { |
| | | 8 | | Task TranscodeAsync(string sourcePath, string outputPath, CancellationToken ct); |
| | | 9 | | } |
| | | 10 | | |
| | 11 | 11 | | internal sealed partial class FfmpegVideoProcessor( |
| | 11 | 12 | | IProcessLauncher processLauncher, |
| | 11 | 13 | | IOptions<WorkerSettings> options, |
| | 11 | 14 | | ILogger<FfmpegVideoProcessor> logger) : IVideoProcessor, IDisposable |
| | | 15 | | { |
| | 1 | 16 | | private static readonly string[] encoderPriority = |
| | 1 | 17 | | [ |
| | 1 | 18 | | H264Encoder.QuickSync, |
| | 1 | 19 | | H264Encoder.Nvenc, |
| | 1 | 20 | | H264Encoder.Software, |
| | 1 | 21 | | ]; |
| | | 22 | | |
| | 11 | 23 | | private readonly SemaphoreSlim encoderDetectionLock = new(1, 1); |
| | | 24 | | private string? detectedEncoder; |
| | | 25 | | |
| | | 26 | | public async Task TranscodeAsync(string sourcePath, string outputPath, CancellationToken ct) |
| | 11 | 27 | | { |
| | | 28 | | string encoder; |
| | 11 | 29 | | await encoderDetectionLock.WaitAsync(ct); |
| | | 30 | | try |
| | 11 | 31 | | { |
| | 11 | 32 | | detectedEncoder ??= await DetectEncoderAsync(ct); |
| | 11 | 33 | | encoder = detectedEncoder; |
| | 11 | 34 | | } |
| | | 35 | | finally |
| | 11 | 36 | | { |
| | 11 | 37 | | encoderDetectionLock.Release(); |
| | 11 | 38 | | } |
| | | 39 | | |
| | 11 | 40 | | await RunFfmpegAsync(sourcePath, outputPath, encoder, ct); |
| | 9 | 41 | | } |
| | | 42 | | |
| | | 43 | | private async Task<string> DetectEncoderAsync(CancellationToken ct) |
| | 10 | 44 | | { |
| | 47 | 45 | | foreach (var encoder in encoderPriority) |
| | 13 | 46 | | { |
| | 13 | 47 | | if (await ProbeEncoderAsync(encoder, ct)) |
| | 9 | 48 | | { |
| | 9 | 49 | | Log.EncoderSelected(logger, encoder); |
| | 9 | 50 | | return encoder; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // Stryker disable once Equality |
| | 4 | 54 | | if (encoder != H264Encoder.Software) |
| | 3 | 55 | | Log.EncoderUnavailable(logger, encoder); |
| | 4 | 56 | | } |
| | | 57 | | |
| | 1 | 58 | | return H264Encoder.Software; |
| | 10 | 59 | | } |
| | | 60 | | |
| | | 61 | | private async Task<bool> ProbeEncoderAsync(string encoder, CancellationToken ct) |
| | 13 | 62 | | { |
| | | 63 | | // Encode a tiny synthetic clip to /dev/null — success means the encoder is available. |
| | | 64 | | // /dev/null is Linux-only; the deployment target is linux/amd64 + linux/arm64 (see Dockerfile). |
| | 13 | 65 | | string[] arguments = |
| | 13 | 66 | | [ |
| | 13 | 67 | | "-f", "lavfi", |
| | 13 | 68 | | "-i", "color=black:s=64x64:r=1:d=0.1", |
| | 13 | 69 | | "-c:v", encoder, |
| | 13 | 70 | | "-f", "mp4", |
| | 13 | 71 | | "/dev/null", |
| | 13 | 72 | | "-y", |
| | 13 | 73 | | ]; |
| | 13 | 74 | | var result = await processLauncher.RunAsync(options.Value.FfmpegPath, arguments, ct); |
| | 13 | 75 | | return result.ExitCode == 0; |
| | 13 | 76 | | } |
| | | 77 | | |
| | | 78 | | private async Task RunFfmpegAsync(string sourcePath, string outputPath, string encoder, CancellationToken ct) |
| | 11 | 79 | | { |
| | 11 | 80 | | var shortEdge = options.Value.VideoShortEdge; |
| | 11 | 81 | | string[] arguments = |
| | 11 | 82 | | [ |
| | 11 | 83 | | "-i", sourcePath, |
| | 11 | 84 | | "-vf", $"scale='if(gt(iw,ih),-2,trunc(min({shortEdge},iw)/2)*2)':'if(gt(iw,ih),trunc(min({shortEdge},ih)/2)* |
| | 11 | 85 | | "-c:v", encoder, |
| | 11 | 86 | | "-pix_fmt", "yuv420p", |
| | 11 | 87 | | "-profile:v", "high", |
| | 11 | 88 | | "-level:v", "4.0", |
| | 11 | 89 | | "-b:v", $"{options.Value.VideoBitrateKbps}k", |
| | 11 | 90 | | "-c:a", "aac", |
| | 11 | 91 | | "-b:a", "128k", |
| | 11 | 92 | | "-movflags", "+faststart", |
| | 11 | 93 | | "-f", "mp4", |
| | 11 | 94 | | "-y", |
| | 11 | 95 | | outputPath, |
| | 11 | 96 | | ]; |
| | 11 | 97 | | await processLauncher.RunFfmpegQuietlyAsync(options.Value.FfmpegPath, arguments, ct); |
| | 9 | 98 | | } |
| | | 99 | | |
| | 1 | 100 | | public void Dispose() => encoderDetectionLock.Dispose(); |
| | | 101 | | |
| | | 102 | | private static partial class Log |
| | | 103 | | { |
| | | 104 | | [LoggerMessage(Level = LogLevel.Information, Message = "Selected H.264 encoder: {Encoder}.")] |
| | | 105 | | public static partial void EncoderSelected(ILogger logger, string encoder); |
| | | 106 | | |
| | | 107 | | [LoggerMessage(Level = LogLevel.Debug, Message = "H.264 encoder {Encoder} is unavailable; trying next.")] |
| | | 108 | | public static partial void EncoderUnavailable(ILogger logger, string encoder); |
| | | 109 | | } |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | internal static class H264Encoder |
| | | 113 | | { |
| | | 114 | | internal const string QuickSync = "h264_qsv"; |
| | | 115 | | internal const string Nvenc = "h264_nvenc"; |
| | | 116 | | internal const string Software = "libx264"; |
| | | 117 | | } |