Skip to content

armgit / Filee

Public Imported from GitHub
0

Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.

40.9 KB
Raw Blame
// "Cut a clip" with FFmpeg: the command lines of a copied and of an exact clip (no FFmpeg needed), and real clips
// cut through the job queue, which run only when Filee's FFmpeg is installed (`pwsh build/fetch-engines.ps1 -Only
// ffmpeg`). Inputs are generated by FFmpeg itself and every output is checked with ffprobe.

using System.Globalization;
using System.Text.Json;
using Filee.Core.Conversion;
using Filee.Core.Presets;
using Filee.Engines.Infrastructure;
using Filee.Engines.Media;

namespace Filee.Engines.Tests;

[Collection(nameof(FfmpegCollection))]
public class ClipTests(EngineFixture fx) : IClassFixture<EngineFixture>
{
    private static readonly (string Ffmpeg, string Ffprobe)? Tools = FfmpegConverter.Locate();

    private static void SkipUnlessInstalled() =>
        Assert.SkipUnless(Tools is not null, "FFmpeg is not installed (pwsh build/fetch-engines.ps1 -Only ffmpeg)");

    // ───────────────────────── Command lines (no FFmpeg needed) ─────────────────────────

    private static readonly VideoStreamInfo Hd = new(1920, 1080, 1, 0, 29.97, "h264", "yuv420p");
    private static readonly MediaInfo Movie = new(TimeSpan.FromMinutes(10), Hd, new AudioStreamInfo(2, 48000, "aac"), null);

    private static int IndexOf(IReadOnlyList<string> args, string option) => args.ToList().IndexOf(option);
    private static string After(IReadOnlyList<string> args, string option) => args[IndexOf(args, option) + 1];

    [Fact]
    public void A_fast_clip_copies_every_stream_from_the_key_frame_before_the_start()
    {
        var args = MediaEncoding.ClipCopy("in.mp4", "out.mp4", "mp4", new ClipRange(65.3, 95.3), new MediaOptions(), Movie);

        // Positions in the source, read before the input: FFmpeg seeks instead of decoding up to the start.
        Assert.Equal("65.300", After(args, "-ss"));
        Assert.Equal("95.300", After(args, "-to"));
        Assert.True(IndexOf(args, "-ss") < IndexOf(args, "-i"));
        Assert.True(IndexOf(args, "-to") < IndexOf(args, "-i"));
        Assert.Equal("copy", After(args, "-c"));
        Assert.DoesNotContain("-c:v", args);
        Assert.DoesNotContain("-vf", args);
        // The video and every audio track; the copied timestamps start at 0.
        Assert.Equal(["0:V:0", "0:a:0"], args.Select((a, i) => (a, i)).Where(x => x.a == "-map").Select(x => args[x.i + 1]));
        Assert.Equal("make_zero", After(args, "-avoid_negative_ts"));
        Assert.Equal("mp4", After(args, "-f"));
        Assert.Equal("+faststart", After(args, "-movflags"));
        Assert.Equal("out.mp4", args[^1]);
    }

    [Fact]
    public void Clips_take_the_sound_track_the_probe_chose()
    {
        var info = Movie with { Audio = new AudioStreamInfo(2, 48000, "aac", 1) };
        var fast = MediaEncoding.ClipCopy("in.mp4", "out.mp4", "mp4", new ClipRange(12.5, 42.5), new MediaOptions(), info);
        Assert.Equal(["0:V:0", "0:a:1"], fast.Where((a, i) => i > 0 && fast[i - 1] == "-map"));
        var exact = MediaEncoding.Plan("in.mp4", "out.mp4", "mp4", new MediaOptions(), info, "work",
            clip: new ClipRange(12.5, 42.5)).Single().Arguments;
        Assert.Equal(["0:V:0", "0:a:1"], exact.Where((a, i) => i > 0 && exact[i - 1] == "-map"));
        var edited = MediaEncoding.ClipEditArguments("in.mp4", "out.mp4", "mp4", Parts(), new MediaOptions(), info);
        Assert.Contains("[0:a:1]asplit=2", After(edited, "-filter_complex"));
        Assert.Contains("[0:a:1]", After(MediaEncoding.ClipPreviewAudio("in.mp4", Parts(), Hd, audioTrack: 1), "-filter_complex"));
    }

    [Fact]
    public void A_fast_clip_without_sound_leaves_the_audio_out()
    {
        var args = MediaEncoding.ClipCopy("in.mkv", "out.mkv", "mkv", new ClipRange(0, 15), new MediaOptions { RemoveAudio = true }, Movie);
        Assert.DoesNotContain("0:a:0", args);
        Assert.Contains("-an", args);
        Assert.Equal("matroska", After(args, "-f"));

        var silent = MediaEncoding.ClipCopy("in.mkv", "out.mkv", "mkv", new ClipRange(0, 15), new MediaOptions(), Movie with { Audio = null });
        Assert.Contains("-an", silent);
    }

    [Fact]
    public void A_fast_hevc_clip_keeps_the_tag_apple_plays()
    {
        var hevc = Movie with { Video = Hd with { Codec = "hevc" } };
        Assert.Equal("hvc1", After(MediaEncoding.ClipCopy("in.mov", "out.mov", "mov", new ClipRange(1, 2), new MediaOptions(), hevc), "-tag:v"));
        Assert.DoesNotContain("-tag:v", MediaEncoding.ClipCopy("in.mkv", "out.mkv", "mkv", new ClipRange(1, 2), new MediaOptions(), hevc));
    }

    [Fact]
    public void A_fast_clip_needs_a_video_and_a_target_ffmpeg_writes()
    {
        Assert.Throws<InvalidOperationException>(() =>
            MediaEncoding.ClipCopy("in.mp4", "out.mp4", "mp4", new ClipRange(0, 1), new MediaOptions(), Movie with { Video = null }));
        Assert.Throws<NotSupportedException>(() =>
            MediaEncoding.ClipCopy("in.rm", "out.rm", "rm", new ClipRange(0, 1), new MediaOptions(), Movie));
    }

    [Fact]
    public void An_exact_clip_is_encoded_with_the_presets_video_options()
    {
        var options = new MediaOptions { Quality = MediaQuality.High, MaxHeight = 720 };
        var args = MediaEncoding.Plan("in.mp4", "out.mp4", "mp4", options, Movie, "work", clip: new ClipRange(12.5, 42.5)).Single().Arguments;

        Assert.Equal("12.500", After(args, "-ss"));
        Assert.Equal("42.500", After(args, "-to"));
        Assert.True(IndexOf(args, "-ss") < IndexOf(args, "-i"));
        // Encoded although the video could be copied (H.264 in MP4): only an encode cuts at any frame.
        Assert.Equal("libx264", After(args, "-c:v"));
        Assert.Contains("720", After(args, "-vf"));
        Assert.Equal(["0:V:0", "0:a:0"], args.Select((a, i) => (a, i)).Where(x => x.a == "-map").Select(x => args[x.i + 1]));
        // The sound too: copied, it would start with the packets before the seek point.
        Assert.Equal("aac", After(args, "-c:a"));
        // Without a clip the same video is only rewrapped, as before.
        var whole = MediaEncoding.Plan("in.mp4", "out.mp4", "mp4", new MediaOptions(), Movie, "work").Single().Arguments;
        Assert.Equal("copy", After(whole, "-c:v"));
        Assert.Equal("copy", After(whole, "-c:a"));
        Assert.DoesNotContain("-ss", whole);
    }

    [Fact]
    public void An_exact_clip_takes_the_graphics_chips_encoder_when_asked_for()
    {
        var options = new MediaOptions { HardwareEncoding = true };
        // A whole H.264 MP4 is copied, so no hardware encoder is wanted; a clip of it is encoded.
        Assert.Null(MediaEncoding.HardwareCodec("mp4", options, Movie));
        Assert.Equal("h264", MediaEncoding.HardwareCodec("mp4", options, Movie, encode: true));
        Assert.Null(MediaEncoding.HardwareCodec("mp4", new MediaOptions(), Movie, encode: true));
    }

    [Fact]
    public void A_frame_for_the_window_is_one_small_jpeg_at_the_time()
    {
        var args = MediaEncoding.FrameAt("in.mp4", 61.25, "frame.jpg", 96, null);
        Assert.Equal("61.250", After(args, "-ss"));
        Assert.True(IndexOf(args, "-ss") < IndexOf(args, "-i"));
        Assert.Equal("1", After(args, "-frames:v"));
        Assert.Equal("mjpeg", After(args, "-c:v"));
        Assert.Contains("min(ih,96)", After(args, "-vf"));
        Assert.Equal("frame.jpg", args[^1]);
    }

    [Theory]
    [InlineData(0, 16, 600, 18.75)]
    [InlineData(15, 16, 600, 581.25)]
    [InlineData(0, 16, 0, 0)]
    public void Timeline_pictures_show_the_middle_of_their_part(int index, int count, double duration, double expected) =>
        Assert.Equal(expected, VideoFrames.ThumbnailTime(index, count, duration), 6);

    [Fact]
    public void The_last_frame_is_a_frame_before_the_end()
    {
        Assert.Equal(9.96, new VideoSummary(10, 1920, 1080, 25).LastFrameTime, 6);
        Assert.Equal(1 / 30.0, new VideoSummary(10, 1920, 1080, 0).FrameLength, 6);
        Assert.Equal(0, new VideoSummary(0, 1920, 1080, 25).LastFrameTime);
    }

    [Fact]
    public void A_clip_is_one_ffmpeg_step_in_the_videos_own_format()
    {
        var planner = fx.Catalog.CreatePlanner(["ffmpeg"]);
        foreach (var format in new[] { "mp4", "mov", "mkv", "webm", "avi" })
        {
            Assert.Equal(format, ClipTool.TargetFor(format, planner));
            Assert.True(ClipTool.CanCut(format, planner));
            Assert.Equal("ffmpeg", Assert.Single(planner.Plan(format, format)!.Steps).Converter.Id);
        }
        // RealMedia is read, not written: its clips are MP4.
        Assert.Equal("mp4", ClipTool.TargetFor("rm", planner));
        Assert.True(ClipTool.CanCut("rm", planner));
        Assert.False(ClipTool.CanCut("mp3", planner));
    }

    // ───────────────────────── Real clips ─────────────────────────

    /// <summary>
    /// A 10-second test video (moving pattern and a tone) with a key frame every second, so a copied clip starts at
    /// most a second early.
    /// </summary>
    private static async Task<string> MakeVideoAsync(string folder, string name, params string[] codecs)
    {
        var path = Path.Combine(folder, name);
        List<string> args =
        [
            "-hide_banner", "-nostdin", "-v", "error", "-y",
            "-f", "lavfi", "-i", "testsrc2=size=320x240:rate=25:duration=10",
            "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=10", "-ac", "2",
            .. codecs.Length > 0 ? codecs : ["-c:v", "libx264", "-g", "25", "-pix_fmt", "yuv420p", "-c:a", "aac"],
            path,
        ];
        var result = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg, args, TimeSpan.FromMinutes(2), CancellationToken.None);
        Assert.True(result.ExitCode == 0, result.StandardError);
        return path;
    }

    private sealed record Probed(double Duration, string? VideoCodec, string? AudioCodec, int Frames);

    private static async Task<Probed> ProbeAsync(string path)
    {
        var result = await ProcessRunner.RunAsync(Tools!.Value.Ffprobe,
            ["-v", "error", "-count_frames", "-print_format", "json", "-show_format", "-show_streams", path],
            TimeSpan.FromMinutes(1), CancellationToken.None);
        Assert.True(result.ExitCode == 0, $"ffprobe {path}: {result.StandardError}");
        using var json = JsonDocument.Parse(result.StandardOutput);
        var streams = json.RootElement.GetProperty("streams").EnumerateArray().ToList();
        string? Codec(string type) => streams.FirstOrDefault(s => s.GetProperty("codec_type").GetString() == type) is { ValueKind: JsonValueKind.Object } s
            ? s.GetProperty("codec_name").GetString() : null;
        var video = streams.First(s => s.GetProperty("codec_type").GetString() == "video");
        var frames = video.TryGetProperty("nb_read_frames", out var n) && int.TryParse(n.GetString(), out var count) ? count : 0;
        var duration = double.Parse(json.RootElement.GetProperty("format").GetProperty("duration").GetString()!, CultureInfo.InvariantCulture);
        return new Probed(duration, Codec("video"), Codec("audio"), frames);
    }

    private async Task<(ConversionJob Job, string Output)> CutAsync(string source, string target, ClipRange clip, bool exact = false,
        Action<Preset>? change = null)
    {
        var tool = BuiltInData.CreatePresets().Single(p => p.Id == "tool-clip");
        tool.Media.ExactCut = exact;
        change?.Invoke(tool);
        var job = await fx.ConvertAsync([source], ClipTool.ConversionOf(tool, target, clip));
        Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
        return (job, Assert.Single(job.Outputs));
    }

    [Fact]
    public async Task A_fast_clip_is_copied_from_the_key_frame_before_the_start_next_to_the_video()
    {
        SkipUnlessInstalled();
        var source = await MakeVideoAsync(fx.NewFolder(), "match.mp4");

        var (_, output) = await CutAsync(source, "mp4", new ClipRange(4.5, 7.5));

        Assert.Equal(Path.Combine(Path.GetDirectoryName(source)!, "match_clip.mp4"), output);
        var probed = await ProbeAsync(output);
        Assert.Equal(("h264", "aac"), (probed.VideoCodec, probed.AudioCodec));
        // Copied: from the key frame at 4 s (a second at most early) to 7.5 s.
        Assert.InRange(probed.Duration, 2.9, 4.2);
        Assert.True(File.Exists(source));
    }

    [Fact]
    public async Task An_exact_clip_starts_and_ends_at_the_chosen_frames()
    {
        SkipUnlessInstalled();
        var source = await MakeVideoAsync(fx.NewFolder(), "match.mp4");

        var (_, output) = await CutAsync(source, "mp4", new ClipRange(4.5, 7.5), exact: true);

        var probed = await ProbeAsync(output);
        Assert.Equal("h264", probed.VideoCodec);
        Assert.InRange(probed.Duration, 2.9, 3.15);
        Assert.InRange(probed.Frames, 74, 76); // 3 s at 25 fps
    }

    [Fact]
    public async Task The_end_of_a_clip_stops_at_the_end_of_the_video()
    {
        SkipUnlessInstalled();
        var source = await MakeVideoAsync(fx.NewFolder(), "match.mkv", "-c:v", "libx264", "-g", "25", "-pix_fmt", "yuv420p", "-c:a", "libopus");

        var (_, output) = await CutAsync(source, "mkv", new ClipRange(8, 40), exact: true);

        Assert.EndsWith("match_clip.mkv", output);
        var probed = await ProbeAsync(output);
        // Sound and picture alike: a copied sound would start a second early and make the file longer.
        Assert.InRange(probed.Duration, 1.9, 2.15);
        Assert.Equal("aac", probed.AudioCodec); // MKV's usual sound, encoded
        // Encoded with the preset's video options: smaller than asked for.
        var (_, small) = await CutAsync(source, "mkv", new ClipRange(1, 3), exact: true, tool => tool.Media.RemoveAudio = true);
        Assert.Null((await ProbeAsync(small)).AudioCodec);
    }

    [Fact]
    public async Task A_copy_the_target_cant_hold_is_encoded_instead()
    {
        SkipUnlessInstalled();
        // H.264 can't go into WebM as it is: the fast clip is encoded (VP9 and Opus) instead of failing.
        var source = await MakeVideoAsync(fx.NewFolder(), "match.mkv");

        var (_, output) = await CutAsync(source, "webm", new ClipRange(2, 4));

        var probed = await ProbeAsync(output);
        Assert.Equal(("vp9", "opus"), (probed.VideoCodec, probed.AudioCodec));
        Assert.InRange(probed.Duration, 1.9, 2.2);
    }

    [Fact]
    public async Task An_existing_clip_is_kept_and_the_new_one_numbered()
    {
        SkipUnlessInstalled();
        var source = await MakeVideoAsync(fx.NewFolder(), "match.mp4");
        var (_, first) = await CutAsync(source, "mp4", new ClipRange(0, 2));
        var (_, second) = await CutAsync(source, "mp4", new ClipRange(5, 7));
        Assert.NotEqual(first, second);
        Assert.True(File.Exists(first));
        Assert.True(File.Exists(second));
    }

    [Fact]
    public async Task A_clip_can_be_cancelled_and_leaves_nothing_behind()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var source = await MakeVideoAsync(folder, "match.mp4");
        var tool = BuiltInData.CreatePresets().Single(p => p.Id == "tool-clip");
        tool.Media.ExactCut = true;
        var preset = ClipTool.ConversionOf(tool, "mp4", new ClipRange(0, 10));

        await using var queue = new JobQueue(fx.Catalog);
        var job = new ConversionJob([source], preset, "clip");
        job.Cancel();
        await queue.RunAsync(job);

        Assert.Equal(JobState.Cancelled, job.State);
        Assert.Equal(["match.mp4"], Directory.GetFiles(folder).Select(Path.GetFileName));
    }

    [Fact]
    public async Task The_window_reads_the_length_and_takes_frames()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var source = await MakeVideoAsync(folder, "match.mp4");

        var video = await VideoFrames.ProbeAsync(source, CancellationToken.None);
        Assert.InRange(video.Duration, 9.9, 10.1);
        Assert.Equal((320, 240), (video.Width, video.Height));
        Assert.Equal(25, video.FrameRate, 1);

        var frame = Path.Combine(folder, "frame.jpg");
        Assert.True(await VideoFrames.WriteFrameAsync(source, 5, frame, 96, CancellationToken.None));
        using (var image = new ImageMagick.MagickImage(frame))
            Assert.Equal(96u, image.Height);
        Assert.True(await VideoFrames.WriteFrameAsync(source, video.LastFrameTime, Path.Combine(folder, "last.jpg"), 96, CancellationToken.None));
        Assert.False(await VideoFrames.WriteFrameAsync(source, 60, Path.Combine(folder, "past.jpg"), 96, CancellationToken.None));
    }

    // ───────────────────────── Clips with parts (no FFmpeg needed) ─────────────────────────

    private static ClipEdit Parts() => new()
    {
        Parts =
        [
            new ClipPart { Start = 10, End = 20, Volume = 0.5 },
            new ClipPart { Start = 20, End = 22, Blank = true },
            new ClipPart { Start = 30, End = 40, Speed = 2 },
        ],
        FadeIn = 1,
        FadeOut = 2,
    };

    [Fact]
    public void A_clip_with_parts_is_one_graph_that_reads_the_video_once()
    {
        var graph = MediaEncoding.ClipGraphOf("in.mp4", Parts(), audio: true, 1920, 1080, "30");

        // From the first part to the last, seeking before the input.
        Assert.Equal(["-ss", "10.000", "-to", "40.000", "-i", "in.mp4"], graph.Inputs);
        Assert.Equal(10 + 2 + 5, graph.Length);
        var filter = graph.Filter;
        Assert.Contains("[0:V:0]split=2[vs0][vs1];", filter);
        Assert.Contains("[0:a:0]asplit=2[as0][as1];", filter);
        // The parts are cut out of what was read: their times count from the first part's start.
        Assert.Contains("[vs0]trim=start=0:end=10,setpts=PTS-STARTPTS,fps=30,scale=1920:1080,setsar=1,format=yuv420p[v0]", filter);
        Assert.Contains("[vs1]trim=start=20:end=30,setpts=(PTS-STARTPTS)/2,", filter);
        Assert.Contains("[as0]atrim=start=0:end=10,asetpts=PTS-STARTPTS,volume=0.5,", filter);
        Assert.Contains("[as1]atrim=start=20:end=30,asetpts=PTS-STARTPTS,atempo=2,", filter);
        // The pause: black and silence as long as the part played.
        Assert.Contains("color=c=black:s=1920x1080:r=30:d=2,setsar=1,format=yuv420p[v1]", filter);
        Assert.Contains("anullsrc=r=48000:cl=stereo,atrim=duration=2[a1]", filter);
        Assert.Contains("[v0][a0][v1][a1][v2][a2]concat=n=3:v=1:a=1[vc][ac]", filter);
        Assert.EndsWith("[vc]fade=t=in:st=0:d=1,fade=t=out:st=15:d=2[v];[ac]afade=t=in:st=0:d=1,afade=t=out:st=15:d=2[a]", filter);
    }

    [Fact]
    public void A_clip_without_sound_or_fades_has_only_pictures()
    {
        var edit = ClipEdit.Of(new ClipRange(5, 9));
        edit.Parts[0].Speed = 0.5;
        var graph = MediaEncoding.ClipGraphOf("in.mp4", edit, audio: false, 640, 360, "25");
        Assert.False(graph.HasAudio);
        Assert.DoesNotContain("[0:a:0]", graph.Filter);
        Assert.Contains("[0:V:0]null[vs0];", graph.Filter);
        Assert.EndsWith("concat=n=1:v=1:a=0[vc];[vc]null[v]", graph.Filter);
        Assert.Equal(8, graph.Length);
    }

    [Theory]
    [InlineData(1, new double[0])]
    [InlineData(2, new[] { 2.0 })]
    [InlineData(0.75, new[] { 0.75 })]
    [InlineData(3, new[] { 2.0, 1.5 })]
    [InlineData(4, new[] { 2.0, 2.0 })]
    [InlineData(0.25, new[] { 0.5, 0.5 })]
    public void Sound_changes_speed_in_steps_every_ffmpeg_takes(double speed, double[] steps) =>
        Assert.Equal(steps, MediaEncoding.Tempo(speed));

    [Fact]
    public void A_clip_with_parts_is_encoded_with_the_presets_options()
    {
        var options = new MediaOptions { Quality = MediaQuality.Small, MaxHeight = 720 };
        var args = MediaEncoding.ClipEditArguments("in.mp4", "out.mp4", "mp4", Parts(), options, Movie);

        Assert.Equal("[v]", After(args, "-map"));
        Assert.Contains("[a]", args);
        Assert.Contains("scale=1280:720", After(args, "-filter_complex"));
        Assert.Equal("libx264", After(args, "-c:v"));
        Assert.Equal("28", After(args, "-crf"));
        Assert.Equal("aac", After(args, "-c:a"));
        Assert.Equal("mp4", After(args, "-f"));

        var silent = MediaEncoding.ClipEditArguments("in.mp4", "out.webm", "webm", Parts(), new MediaOptions { RemoveAudio = true }, Movie);
        Assert.DoesNotContain("[a]", silent);
        Assert.Contains("-an", silent);
        Assert.Equal("libvpx-vp9", After(silent, "-c:v"));
        // A portrait phone video stays portrait.
        Assert.Equal((1080, 1920), MediaEncoding.ClipSize(Hd with { Rotation = 90 }, 0));
        Assert.Equal((404, 720), MediaEncoding.ClipSize(Hd with { Rotation = -90 }, 720));
    }

    [Fact]
    public void The_preview_plays_small_raw_pictures_and_the_sound_as_wav()
    {
        var pictures = MediaEncoding.ClipPreviewVideo("in.mp4", Parts(), Hd, 360, 25, out var width, out var height);
        Assert.Equal((640, 360), (width, height));
        Assert.Equal(("rawvideo", "bgra"), (After(pictures, "-f"), After(pictures, "-pix_fmt")));
        Assert.Contains("format=bgra", After(pictures, "-filter_complex"));
        Assert.Equal("pipe:1", pictures[^1]);

        var sound = MediaEncoding.ClipPreviewAudio("in.mp4", Parts(), Hd);
        Assert.Equal("[a]", After(sound, "-map"));
        Assert.Equal("wav", After(sound, "-f"));
    }

    [Fact]
    public void Silences_are_read_from_silencedetect()
    {
        const string log = """
            [silencedetect @ 0000] silence_start: 0
            [silencedetect @ 0000] silence_end: 1.25 | silence_duration: 1.25
            [silencedetect @ 0000] silence_start: 3.5
            [silencedetect @ 0000] silence_end: 5 | silence_duration: 1.5
            [silencedetect @ 0000] silence_start: 9.75
            """;
        Assert.Equal([new ClipRange(60, 61.25), new ClipRange(63.5, 65), new ClipRange(69.75, 70)], ClipPreview.ParseSilences(log, 60, 70));
        Assert.Empty(ClipPreview.ParseSilences("nothing here", 0, 10));
        var args = ClipPreview.SilenceArguments("in.mp4", 60, 70);
        Assert.Equal("0:a:0", After(args, "-map"));
        Assert.Equal("0:a:1", After(ClipPreview.SilenceArguments("in.mp4", 60, 70, audioTrack: 1), "-map"));
        Assert.Equal("60.000", After(args, "-ss"));
        Assert.StartsWith("silencedetect=noise=-35dB:d=0.4", After(args, "-af"));
    }

    [Fact]
    public void Loudness_is_one_level_per_step_the_loudest_one()
    {
        float[] samples = [.. Enumerable.Repeat(0.5f, 100), .. Enumerable.Repeat(0f, 100), .. Enumerable.Repeat(0.25f, 100)];
        Assert.Equal([1f, 0f, 0.5f], ClipPreview.Loudness(samples, 100, 3, 3));
        Assert.Empty(ClipPreview.Loudness([], 100, 3, 3));
    }

    // ───────────────────────── Clips with parts (FFmpeg) ─────────────────────────

    [Fact]
    public async Task A_clip_with_parts_plays_them_one_after_the_other()
    {
        SkipUnlessInstalled();
        var source = await MakeVideoAsync(fx.NewFolder(), "match.mp4");
        var edit = new ClipEdit
        {
            Parts =
            [
                new ClipPart { Start = 1, End = 3 },
                new ClipPart { Start = 3, End = 4, Blank = true },
                new ClipPart { Start = 6, End = 8, Speed = 2, Volume = 0.5 },
            ],
            FadeIn = 0.5,
            FadeOut = 0.5,
        };
        var tool = BuiltInData.CreatePresets().Single(p => p.Id == "tool-clip");

        var job = await fx.ConvertAsync([source], ClipTool.ConversionOf(tool, "mp4", edit));

        Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
        var probed = await ProbeAsync(Assert.Single(job.Outputs));
        Assert.Equal(("h264", "aac"), (probed.VideoCodec, probed.AudioCodec));
        // 2 s + a 1 s pause + 2 s of video at double speed.
        Assert.InRange(probed.Duration, 3.9, 4.15);
        Assert.InRange(probed.Frames, 98, 102);
    }

    [Fact]
    public async Task Silences_and_loudness_are_found_in_the_sound()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var source = Path.Combine(folder, "talk.mp4");
        // A tone with two seconds of silence in the middle (3 s to 5 s).
        var result = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,
        [
            "-hide_banner", "-nostdin", "-v", "error", "-y",
            "-f", "lavfi", "-i", "testsrc2=size=160x120:rate=25:duration=8",
            "-f", "lavfi", "-i", "aevalsrc='if(between(t,3,5),0,0.5*sin(2*PI*440*t))':s=48000:d=8",
            "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", source,
        ], TimeSpan.FromMinutes(2), CancellationToken.None);
        Assert.True(result.ExitCode == 0, result.StandardError);

        var silences = await ClipPreview.FindSilencesAsync(source, 1, 7, CancellationToken.None);
        var silence = Assert.Single(silences);
        Assert.InRange(silence.Start, 2.9, 3.2);
        Assert.InRange(silence.End, 4.9, 5.2);

        var loudness = await ClipPreview.ReadLoudnessAsync(source, 8, 8, folder, CancellationToken.None);
        Assert.Equal(8, loudness.Length);
        Assert.True(loudness[3] < 0.1 && loudness[4] < 0.1, string.Join(", ", loudness));
        Assert.True(loudness[1] > 0.8 && loudness[6] > 0.8, string.Join(", ", loudness));
        Assert.Single(Directory.GetFiles(folder)); // the samples are gone
    }

    [Fact]
    public async Task The_waveform_and_silences_come_from_the_first_sound_ffmpeg_can_decode()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var source = Path.Combine(folder, "talk.mov");
        var result = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,
        [
            "-hide_banner", "-nostdin", "-v", "error", "-y",
            "-f", "lavfi", "-i", "testsrc2=size=160x120:rate=25:duration=8",
            "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=8",
            "-f", "lavfi", "-i", "aevalsrc='if(between(t,3,5),0,0.5*sin(2*PI*440*t))':s=48000:d=8",
            "-map", "0:v", "-map", "1:a", "-map", "2:a", "-ac:a:0", "4",
            "-c:v", "mjpeg", "-c:a", "aac", "-f", "mov", source,
        ], TimeSpan.FromMinutes(2), CancellationToken.None);
        Assert.True(result.ExitCode == 0, result.StandardError);
        MediaTests.WithoutDecoder(source);
        var probed = await ProcessRunner.RunAsync(Tools.Value.Ffprobe,
            ["-v", "error", "-select_streams", "a", "-show_entries", "stream=codec_name", "-of", "json", source],
            TimeSpan.FromMinutes(1), CancellationToken.None);
        Assert.True(probed.ExitCode == 0, probed.StandardError);
        using var json = JsonDocument.Parse(probed.StandardOutput);
        Assert.Equal(["apple_apac", "aac"], json.RootElement.GetProperty("streams").EnumerateArray().Select(s => s.GetProperty("codec_name").GetString()));

        var silence = Assert.Single(await ClipPreview.FindSilencesAsync(source, 1, 7, CancellationToken.None));
        Assert.InRange(silence.Start, 2.9, 3.2);
        Assert.InRange(silence.End, 4.9, 5.2);
        var loudness = await ClipPreview.ReadLoudnessAsync(source, 8, 8, folder, CancellationToken.None);
        Assert.Equal(8, loudness.Length);
        Assert.True(loudness[3] < 0.1 && loudness[4] < 0.1, string.Join(", ", loudness));
        Assert.True(loudness[1] > 0.8 && loudness[6] > 0.8, string.Join(", ", loudness));
        Assert.Single(Directory.GetFiles(folder)); // the samples are gone
    }

    [Fact]
    public async Task The_waveform_and_silences_go_on_with_track_0_when_the_file_cant_be_probed()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var notMedia = Path.Combine(folder, "not-media.mp4");
        await File.WriteAllTextAsync(notMedia, "not media", TestContext.Current.CancellationToken);
        Assert.Empty(await ClipPreview.ReadLoudnessAsync(notMedia, 8, 8, folder, CancellationToken.None));
        var error = await Assert.ThrowsAsync<InvalidOperationException>(() =>
            ClipPreview.FindSilencesAsync(notMedia, 1, 7, CancellationToken.None));
        Assert.StartsWith("FFmpeg failed", error.Message);

        var source = Path.Combine(folder, "talk.mp4");
        var result = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,
        [
            "-hide_banner", "-nostdin", "-v", "error", "-y",
            "-f", "lavfi", "-i", "testsrc2=size=160x120:rate=25:duration=8",
            "-f", "lavfi", "-i", "aevalsrc='if(between(t,3,5),0,0.5*sin(2*PI*440*t))':s=48000:d=8",
            "-c:v", "libx264", "-pix_fmt", "yuv420p", "-c:a", "aac", source,
        ], TimeSpan.FromMinutes(2), CancellationToken.None);
        Assert.True(result.ExitCode == 0, result.StandardError);
        var tools = (Tools.Value.Ffmpeg, Path.Combine(folder, "no-ffprobe", "ffprobe"));
        var silence = Assert.Single(await ClipPreview.FindSilencesAsync(tools, source, 1, 7, CancellationToken.None));
        Assert.InRange(silence.Start, 2.9, 3.2);
        Assert.InRange(silence.End, 4.9, 5.2);
        var loudness = await ClipPreview.ReadLoudnessAsync(tools, source, 0, 8, 8, folder, CancellationToken.None);
        Assert.Equal(8, loudness.Length);
        Assert.True(loudness[3] < 0.1 && loudness[4] < 0.1, string.Join(", ", loudness));
        Assert.True(loudness[1] > 0.8 && loudness[6] > 0.8, string.Join(", ", loudness));
    }

    [Fact]
    public void The_previews_sound_is_a_graph_of_sound_alone()
    {
        // A picture output nobody takes makes FFmpeg refuse the whole graph: the preview played no sound.
        var sound = MediaEncoding.ClipPreviewAudio("in.mp4", Parts(), Hd);
        var filter = After(sound, "-filter_complex");
        Assert.DoesNotContain("[v", filter);
        Assert.DoesNotContain("color=", filter);
        Assert.Contains("concat=n=3:v=0:a=1[ac]", filter);
        Assert.EndsWith("[a]", filter);
        Assert.Throws<ArgumentException>(() => MediaEncoding.ClipGraphOf("in.mp4", Parts(), audio: false, 160, 90, "10", video: false));
    }

    [Fact]
    public async Task The_previews_sound_is_decoded_and_reaches_the_player()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var source = await MakeVideoAsync(folder, "match.mp4");
        var edit = new ClipEdit
        {
            Parts = [new ClipPart { Start = 1, End = 2 }, new ClipPart { Start = 2, End = 2.5, Blank = true }, new ClipPart { Start = 5, End = 6, Speed = 2 }],
        };
        var info = await MediaInfo.ProbeAsync(Tools!.Value.Ffprobe, source, CancellationToken.None);

        // What FFmpeg writes for the player: a WAV of the clip's length (1 + 0.5 + 0.5 s at 48 kHz stereo 16-bit).
        var wav = Path.Combine(folder, "sound.wav");
        var args = MediaEncoding.ClipPreviewAudio(source, edit, info.Video!).ToList();
        args[^1] = wav;
        var result = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg, args, TimeSpan.FromMinutes(1), CancellationToken.None);
        Assert.True(result.ExitCode == 0, result.StandardError);
        Assert.InRange(new FileInfo(wav).Length, 1.9 * 48000 * 4, 2.1 * 48000 * 4);

        // Played: the sound goes through ffplay (silently here) alongside the pictures.
        Assert.SkipUnless(ClipPreview.HasSound, "ffplay is not next to FFmpeg");
        long played = 0;
        ClipPreview.PlayerVolume = 0;
        ClipPreview.SoundPlayed = bytes => played = bytes;
        try
        {
            var frames = 0;
            await ClipPreview.PlayAsync(source, edit, 90, (_, _, _, _) => frames++, CancellationToken.None);
            Assert.InRange(frames, 40, 55);
            Assert.InRange(played, 1.9 * 48000 * 4, 2.1 * 48000 * 4 + 1000);
        }
        finally
        {
            ClipPreview.PlayerVolume = null;
            ClipPreview.SoundPlayed = null;
        }
    }

    [Fact]
    public async Task The_preview_plays_the_clips_pictures_in_time()
    {
        SkipUnlessInstalled();
        // No sound: the test plays nothing aloud.
        var source = await MakeVideoAsync(fx.NewFolder(), "silent.mp4", "-c:v", "libx264", "-g", "25", "-pix_fmt", "yuv420p", "-an");
        var edit = new ClipEdit { Parts = [new ClipPart { Start = 2, End = 3 }, new ClipPart { Start = 5, End = 6, Speed = 2 }] };
        var frames = new List<(int Width, int Height, double Seconds)>();
        var clock = System.Diagnostics.Stopwatch.StartNew();

        await ClipPreview.PlayAsync(source, edit, 120, (pixels, width, height, seconds) =>
        {
            Assert.Equal(width * height * 4, pixels.Length);
            frames.Add((width, height, seconds));
        }, CancellationToken.None);

        // 1.5 s of clip at 25 pictures a second, shown in real time.
        Assert.InRange(frames.Count, 30, 40);
        Assert.All(frames, f => Assert.Equal((160, 120), (f.Width, f.Height)));
        Assert.InRange(clock.Elapsed.TotalSeconds, 1.2, 6);

        // Stopping ends it at once.
        using var stop = new CancellationTokenSource();
        var seen = 0;
        var playing = ClipPreview.PlayAsync(source, ClipEdit.Of(new ClipRange(0, 10)), 120, (_, _, _, _) =>
        {
            if (++seen == 5)
                stop.Cancel();
        }, stop.Token);
        await playing.WaitAsync(TimeSpan.FromSeconds(10), TestContext.Current.CancellationToken);
        Assert.InRange(seen, 5, 8);
    }

    // ───────────────────────── Muted parts and music ─────────────────────────

    [Fact]
    public void Music_is_a_second_input_mixed_under_the_clip()
    {
        var edit = Parts();
        edit.Parts[0].Muted = true;
        edit.Music = new ClipMusic { Path = "song.mp3", Offset = 2.5, From = 30, Length = 100, Volume = 0.5 };
        var graph = MediaEncoding.ClipGraphOf("in.mp4", edit, audio: true, 1920, 1080, "30");

        Assert.Equal(["-ss", "10.000", "-to", "40.000", "-i", "in.mp4", "-ss", "30.000", "-i", "song.mp3"], graph.Inputs);
        Assert.True(graph.HasAudio);
        var filter = graph.Filter;
        Assert.Contains("[as0]atrim=start=0:end=10,asetpts=PTS-STARTPTS,volume=0,", filter); // muted: silent, still there
        // 17 s of clip, from 2.5 s: 14.5 s of music, faded a second at its ends, 2.5 s late.
        Assert.Contains("[1:a:0]atrim=duration=14.5,asetpts=PTS-STARTPTS,volume=0.5,afade=t=in:st=0:d=1,afade=t=out:st=13.5:d=1,adelay=2500:all=1,", filter);
        var nextTrack = MediaEncoding.ClipGraphOf("in.mp4", edit, audio: true, 1920, 1080, "30", musicTrack: 1).Filter;
        Assert.Contains("[1:a:1]atrim=duration=14.5", nextTrack);
        Assert.DoesNotContain("[1:a:0]", nextTrack);
        Assert.Contains("[ac][m]amix=inputs=2:duration=first:dropout_transition=0:normalize=0[am]", filter);
        Assert.EndsWith("[am]afade=t=in:st=0:d=1,afade=t=out:st=15:d=2[a]", filter);

        // Without the video's sound the music plays over silence; "Remove audio" leaves it out too.
        var silentVideo = MediaEncoding.ClipGraphOf("in.mp4", edit, audio: false, 1920, 1080, "30");
        Assert.True(silentVideo.HasAudio);
        Assert.Contains("anullsrc=r=48000:cl=stereo,atrim=duration=17[ac]", silentVideo.Filter);
        var removed = MediaEncoding.ClipEditArguments("in.mp4", "out.mp4", "mp4", edit, new MediaOptions { RemoveAudio = true }, Movie);
        Assert.Contains("-an", removed);
        Assert.DoesNotContain("song.mp3", removed);
        // The preview plays the music too, also of a video without sound.
        Assert.Contains("song.mp3", MediaEncoding.ClipPreviewAudio("in.mp4", edit, Hd, hasAudio: false));
    }

    [Fact]
    public async Task Music_plays_under_a_clip_with_a_part_muted()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var source = await MakeVideoAsync(folder, "match.mp4");
        var song = Path.Combine(folder, "song.wav");
        var made = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,
            ["-hide_banner", "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=880:sample_rate=44100:duration=20", "-ac", "2", song],
            TimeSpan.FromMinutes(1), CancellationToken.None);
        Assert.True(made.ExitCode == 0, made.StandardError);
        Assert.Equal(20, await ClipPreview.ReadSoundLengthAsync(song, CancellationToken.None), 1);

        // 0-2 s as recorded, 2-4 s muted; music from 1 s into the clip.
        var edit = new ClipEdit
        {
            Parts = [new ClipPart { Start = 1, End = 3 }, new ClipPart { Start = 3, End = 5, Muted = true }],
            Music = new ClipMusic { Path = song, Offset = 1, Length = 20, Volume = 1, Fade = false },
        };
        var tool = BuiltInData.CreatePresets().Single(p => p.Id == "tool-clip");
        var job = await fx.ConvertAsync([source], ClipTool.ConversionOf(tool, "mp4", edit));
        Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
        var output = Assert.Single(job.Outputs);
        var probed = await ProbeAsync(output);
        Assert.Equal("aac", probed.AudioCodec);
        Assert.InRange(probed.Duration, 3.9, 4.15);

        // Loud where the tone and the music sound together, quieter where only the music plays, silent before it.
        var loudness = await ClipPreview.ReadLoudnessAsync(output, 4, 8, folder, CancellationToken.None);
        Assert.True(loudness[0] < 0.75, string.Join(", ", loudness)); // the tone only (0-0.5 s)
        Assert.True(loudness[3] > 0.9, string.Join(", ", loudness)); // tone and music (1.5-2 s)
        Assert.True(loudness[6] is > 0.3f and < 0.85f, string.Join(", ", loudness)); // music only (3-3.5 s)
    }

    [Fact]
    public async Task A_video_without_sound_gets_the_musics()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var source = await MakeVideoAsync(folder, "silent.mp4", "-c:v", "libx264", "-g", "25", "-pix_fmt", "yuv420p", "-an");
        var song = Path.Combine(folder, "song.m4a");
        var made = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,
            ["-hide_banner", "-nostdin", "-v", "error", "-y", "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=8", "-c:a", "aac", song],
            TimeSpan.FromMinutes(1), CancellationToken.None);
        Assert.True(made.ExitCode == 0, made.StandardError);
        var edit = ClipEdit.Of(new ClipRange(2, 5));
        edit.Music = new ClipMusic { Path = song, Length = 8 };

        var job = await fx.ConvertAsync([source], ClipTool.ConversionOf(BuiltInData.CreatePresets().Single(p => p.Id == "tool-clip"), "mp4", edit));

        Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
        var probed = await ProbeAsync(Assert.Single(job.Outputs));
        Assert.Equal(("h264", "aac"), (probed.VideoCodec, probed.AudioCodec));
        Assert.InRange(probed.Duration, 2.9, 3.15);
    }

    [Fact]
    public async Task Music_whose_first_sound_ffmpeg_cannot_decode_plays_its_next_one()
    {
        SkipUnlessInstalled();
        var folder = fx.NewFolder();
        var song = Path.Combine(folder, "song.mov");
        var made = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,
        [
            "-hide_banner", "-nostdin", "-v", "error", "-y",
            "-f", "lavfi", "-i", "sine=frequency=440:sample_rate=48000:duration=8",
            "-f", "lavfi", "-i", "sine=frequency=880:sample_rate=44100:duration=8",
            "-ac:a:0", "4", "-map", "0:a", "-map", "1:a", "-c:a", "aac", "-f", "mov", song,
        ], TimeSpan.FromMinutes(1), CancellationToken.None);
        Assert.True(made.ExitCode == 0, made.StandardError);
        MediaTests.WithoutDecoder(song);
        var source = await MakeVideoAsync(folder, "silent.mp4", "-c:v", "libx264", "-g", "25", "-pix_fmt", "yuv420p", "-an");
        var edit = ClipEdit.Of(new ClipRange(2, 5));
        edit.Music = new ClipMusic { Path = song, Length = 8 };

        var job = await fx.ConvertAsync([source],
            ClipTool.ConversionOf(BuiltInData.CreatePresets().Single(p => p.Id == "tool-clip"), "mp4", edit));
        Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
        var probed = await ProbeAsync(Assert.Single(job.Outputs));
        Assert.Equal(("h264", "aac"), (probed.VideoCodec, probed.AudioCodec));
        Assert.InRange(probed.Duration, 2.85, 3.15);

        var info = await MediaInfo.ProbeAsync(Tools.Value.Ffprobe, source, CancellationToken.None);
        var clip = edit.Clamped(info.Duration?.TotalSeconds);
        var args = await ClipPreview.PreviewSoundArgumentsAsync(Tools.Value.Ffprobe, source, clip, info.Video!, info.Audio, CancellationToken.None);
        var filter = After(args, "-filter_complex");
        Assert.Contains("[1:a:1]atrim=", filter);
        Assert.DoesNotContain("[1:a:0]", filter);
        var wav = Path.Combine(folder, "sound.wav");
        args[^1] = wav;
        var result = await ProcessRunner.RunAsync(Tools.Value.Ffmpeg, args, TimeSpan.FromMinutes(1), CancellationToken.None);
        Assert.True(result.ExitCode == 0, result.StandardError);
        Assert.InRange(new FileInfo(wav).Length, 3 * 48000 * 4 * 0.95, 3 * 48000 * 4 * 1.05);
    }
}