< Summary

Line coverage
94%
Covered lines: 68
Uncovered lines: 4
Coverable lines: 72
Total lines: 1224
Line coverage: 94.4%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
File 1: .cctor()100%210%
File 2: .ctor(...)100%11100%
File 2: .cctor()100%11100%
File 2: TranscodeAsync()100%22100%
File 2: DetectEncoderAsync()100%66100%
File 2: ProbeEncoderAsync()100%11100%
File 2: RunFfmpegAsync()100%11100%
File 2: Dispose()100%11100%

File(s)

/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs

File '/home/runner/work/anichron/anichron/src/.artifacts/obj/Anichron.Worker/debug/Microsoft.Extensions.Logging.Generators/Microsoft.Extensions.Logging.Generators.LoggerMessageGenerator/LoggerMessage.g.cs' does not exist (any more).

/home/runner/work/anichron/anichron/src/Anichron.Worker/Ingestion/Proxy/VideoProcessor.cs

#LineLine coverage
 1using Anichron.Worker.Settings;
 2using Microsoft.Extensions.Options;
 3
 4namespace Anichron.Worker.Ingestion.Proxy;
 5
 6internal interface IVideoProcessor
 7{
 8    Task TranscodeAsync(string sourcePath, string outputPath, CancellationToken ct);
 9}
 10
 1111internal sealed partial class FfmpegVideoProcessor(
 1112    IProcessLauncher processLauncher,
 1113    IOptions<WorkerSettings> options,
 1114    ILogger<FfmpegVideoProcessor> logger) : IVideoProcessor, IDisposable
 15{
 116    private static readonly string[] encoderPriority =
 117    [
 118        H264Encoder.QuickSync,
 119        H264Encoder.Nvenc,
 120        H264Encoder.Software,
 121    ];
 22
 1123    private readonly SemaphoreSlim encoderDetectionLock = new(1, 1);
 24    private string? detectedEncoder;
 25
 26    public async Task TranscodeAsync(string sourcePath, string outputPath, CancellationToken ct)
 1127    {
 28        string encoder;
 1129        await encoderDetectionLock.WaitAsync(ct);
 30        try
 1131        {
 1132            detectedEncoder ??= await DetectEncoderAsync(ct);
 1133            encoder = detectedEncoder;
 1134        }
 35        finally
 1136        {
 1137            encoderDetectionLock.Release();
 1138        }
 39
 1140        await RunFfmpegAsync(sourcePath, outputPath, encoder, ct);
 941    }
 42
 43    private async Task<string> DetectEncoderAsync(CancellationToken ct)
 1044    {
 4745        foreach (var encoder in encoderPriority)
 1346        {
 1347            if (await ProbeEncoderAsync(encoder, ct))
 948            {
 949                Log.EncoderSelected(logger, encoder);
 950                return encoder;
 51            }
 52
 53            // Stryker disable once Equality
 454            if (encoder != H264Encoder.Software)
 355                Log.EncoderUnavailable(logger, encoder);
 456        }
 57
 158        return H264Encoder.Software;
 1059    }
 60
 61    private async Task<bool> ProbeEncoderAsync(string encoder, CancellationToken ct)
 1362    {
 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).
 1365        string[] arguments =
 1366        [
 1367            "-f", "lavfi",
 1368            "-i", "color=black:s=64x64:r=1:d=0.1",
 1369            "-c:v", encoder,
 1370            "-f", "mp4",
 1371            "/dev/null",
 1372            "-y",
 1373        ];
 1374        var result = await processLauncher.RunAsync(options.Value.FfmpegPath, arguments, ct);
 1375        return result.ExitCode == 0;
 1376    }
 77
 78    private async Task RunFfmpegAsync(string sourcePath, string outputPath, string encoder, CancellationToken ct)
 1179    {
 1180        var shortEdge = options.Value.VideoShortEdge;
 1181        string[] arguments =
 1182        [
 1183            "-i", sourcePath,
 1184            "-vf", $"scale='if(gt(iw,ih),-2,trunc(min({shortEdge},iw)/2)*2)':'if(gt(iw,ih),trunc(min({shortEdge},ih)/2)*
 1185            "-c:v", encoder,
 1186            "-pix_fmt", "yuv420p",
 1187            "-profile:v", "high",
 1188            "-level:v", "4.0",
 1189            "-b:v", $"{options.Value.VideoBitrateKbps}k",
 1190            "-c:a", "aac",
 1191            "-b:a", "128k",
 1192            "-movflags", "+faststart",
 1193            "-f", "mp4",
 1194            "-y",
 1195            outputPath,
 1196        ];
 1197        await processLauncher.RunFfmpegQuietlyAsync(options.Value.FfmpegPath, arguments, ct);
 998    }
 99
 1100    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
 112internal 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}