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.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Harden the media tools: safe merge copies, slideshow progress, compress never bigger
cre8ive-marketing1c11b1e0f175
diff --git a/src/Filee.Core/Conversion/IConverter.cs b/src/Filee.Core/Conversion/IConverter.csindex 11b521b9..402ef8d8 100644--- a/src/Filee.Core/Conversion/IConverter.cs+++ b/src/Filee.Core/Conversion/IConverter.cs@@ -203,9 +203,6 @@ public interface ITiffMerger/// </summary>public interface IMediaJoiner{- /// <summary>True if files can be joined into <paramref name="format"/> now (FFmpeg is installed).</summary>- bool CanJoinInto(string format);-/// <summary>/// Writes every file of <paramref name="inputPaths"/>, in order, into <paramref name="outputPath"/>: video or audio/// clips back to back, or pictures shown one after the other (<see cref="MediaOptions.Slideshow"/>).diff --git a/src/Filee.Core/Conversion/JobQueue.cs b/src/Filee.Core/Conversion/JobQueue.csindex dd04a7ec..a476e43d 100644--- a/src/Filee.Core/Conversion/JobQueue.cs+++ b/src/Filee.Core/Conversion/JobQueue.cs@@ -340,6 +340,9 @@ public sealed class JobQueue : IAsyncDisposableNotify(job);}+ /// <summary>Share of a slideshow's progress that preparing its pictures takes; encoding the video fills the rest.</summary>+ private const double SlidesReadyShare = 0.3;+/// <summary>/// "Merge videos" / "Merge audio": the videos or audio files go into one file as they are. "Video from images":/// every picture becomes PNG in the work directory first (a PNG is taken as it is), with its orientation applied by@@ -372,7 +375,8 @@ public sealed class JobQueue : IAsyncDisposable{parts[i] = source.Id == "png" && !job.Preset.Image.TurnsOrMirrors? [file.SourcePath]- : await RunFileAsync(job, file, i + 1, planner, workDir, finalOutput: false, intermediate: "png");+ : await RunFileAsync(job, file, i + 1, planner, workDir, finalOutput: false, intermediate: "png",+ ceiling: SlidesReadyShare);return;}// A video's sound can join an audio file; a picture or a document joins nothing.@@ -597,11 +601,12 @@ public sealed class JobQueue : IAsyncDisposable/// <summary>/// Converts one source file along its planned route./// With <paramref name="finalOutput"/> false, results stay in the work directory as <paramref name="intermediate"/>- /// files (PDF for merging, TIFF pages for a multi-page TIFF).+ /// files (PDF for merging, TIFF pages for a multi-page TIFF). <paramref name="ceiling"/> is the file's progress once+ /// the route is done (a slideshow's pictures leave the rest to the video)./// </summary>private async Task<IReadOnlyList<string>> RunFileAsync(ConversionJob job, FileResult file, int index, RoutePlanner planner, string workDir, bool finalOutput,- string intermediate = "pdf")+ string intermediate = "pdf", double ceiling = 1){var ct = job.CancellationToken;if (Stopped(file))@@ -652,6 +657,7 @@ public sealed class JobQueue : IAsyncDisposableNotify(job);// A file saved from a browser first has part of its progress already.var floor = file.Progress;+ ceiling = Math.Max(ceiling, floor);// The last step's allocator: how many paths it handed out tells a single output from several, and what a// failed or cancelled conversion started there is undone through it.FinalAllocator? final = null;@@ -677,7 +683,7 @@ public sealed class JobQueue : IAsyncDisposablevar progress = new Progress<double>(p =>{// Reports arrive on the thread pool, possibly out of order: progress only moves forward.- var value = floor + (1 - floor) * (stepIndex + Math.Clamp(p, 0, 1)) / route.Steps.Count;+ var value = floor + (ceiling - floor) * (stepIndex + Math.Clamp(p, 0, 1)) / route.Steps.Count;if (value <= file.Progress)return;file.Progress = value;@@ -709,6 +715,8 @@ public sealed class JobQueue : IAsyncDisposableif (zipResult && inputs.Count > 0)inputs = PackZip(job, file, index, inputs, ct);+ else if (finalOutput && job.Preset.Output.OnlyIfSmaller && inputs.Count == 1 && CopyIfNotSmaller(file.SourcePath, inputs[0]))+ file.NoticeKey = "notice.pdf_not_smaller";// One output, and nothing else handed out for it (a Markdown file's picture folder goes with it).else if (finalOutput && inputs.Count == 1 && final is { Handed: 1 })inputs = [NextToSource(job, file, index, inputs[0], final)];@@ -722,7 +730,7 @@ public sealed class JobQueue : IAsyncDisposableCopyDates(file.SourcePath, inputs);}file.State = inputs.Count == 0 && finalOutput ? FileState.Skipped : FileState.Done;- file.Progress = 1;+ file.Progress = ceiling;Notify(job);return inputs;}@@ -811,6 +819,19 @@ public sealed class JobQueue : IAsyncDisposablereturn [zipPath];}+ /// <summary>+ /// Puts the source in place of <paramref name="output"/> when both have the same format and the output is not+ /// smaller (<see cref="OutputRule.OnlyIfSmaller"/>). True when it did.+ /// </summary>+ private static bool CopyIfNotSmaller(string source, string output)+ {+ if (!string.Equals(FormatRegistry.Detect(source)?.Id, FormatRegistry.Detect(output)?.Id, StringComparison.Ordinal)+ || !File.Exists(output) || new FileInfo(output).Length < new FileInfo(source).Length)+ return false;+ File.Copy(source, output, overwrite: true);+ return true;+ }+/// <summary>The engine's check; a check that fails (an unreadable file) finds nothing, and the conversion reports the file.</summary>private string? ScriptNeedingShaping(ITextShapingCheck check, string path, string format){diff --git a/src/Filee.Core/Presets/BuiltInData.cs b/src/Filee.Core/Presets/BuiltInData.csindex d9342014..81ab6be0 100644--- a/src/Filee.Core/Presets/BuiltInData.cs+++ b/src/Filee.Core/Presets/BuiltInData.cs@@ -124,13 +124,13 @@ public static class BuiltInData// Quality 75 like most photo apps' "smaller"; a JPEG saved below it stays at its own quality.Id = "compress-image", NameKey = "preset.compress_image", TargetFormat = SameAsSource,Image = { Quality = 75, MaxCompression = true, TiffCompression = TiffCompression.Zip },- Output = { FileNamePattern = "{name}_compressed" },+ Output = { FileNamePattern = "{name}_compressed", OnlyIfSmaller = true },},new(){// H.264 at the Small quality, at most 1080p: plays everywhere, a fraction of a phone video's size.Id = "compress-video", NameKey = "preset.compress_video", TargetFormat = "mp4",- Media = { Quality = MediaQuality.Small, MaxHeight = 1080 }, Output = { FileNamePattern = "{name}_compressed" },+ Media = { Quality = MediaQuality.Small, MaxHeight = 1080 }, Output = { FileNamePattern = "{name}_compressed", OnlyIfSmaller = true },},new(){@@ -151,7 +151,7 @@ public static class BuiltInData{// AAC at the Small quality: 128 kbit/s for stereo.Id = "compress-audio", NameKey = "preset.compress_audio", TargetFormat = "m4a",- Media = { Quality = MediaQuality.Small }, Output = { FileNamePattern = "{name}_compressed" },+ Media = { Quality = MediaQuality.Small }, Output = { FileNamePattern = "{name}_compressed", OnlyIfSmaller = true },},new(){diff --git a/src/Filee.Core/Presets/OutputRule.cs b/src/Filee.Core/Presets/OutputRule.csindex 157df1ed..5fa4930a 100644--- a/src/Filee.Core/Presets/OutputRule.cs+++ b/src/Filee.Core/Presets/OutputRule.cs@@ -85,6 +85,12 @@ public sealed class OutputRule/// </summary>public bool ZipResult { get; set; }+ /// <summary>+ /// "Compress" presets: when a file that keeps its format doesn't get smaller, the output is a copy of the source+ /// instead, with a notice. A result in another format is always kept.+ /// </summary>+ public bool OnlyIfSmaller { get; set; }+public OutputRule Clone(){var clone = (OutputRule)MemberwiseClone();diff --git a/src/Filee.Core/Presets/Preset.cs b/src/Filee.Core/Presets/Preset.csindex 09a8b205..3a7a92db 100644--- a/src/Filee.Core/Presets/Preset.cs+++ b/src/Filee.Core/Presets/Preset.cs@@ -581,8 +581,8 @@ public sealed class MediaOptionspublic bool JoinIntoOne { get; set; }/// <summary>- /// MP4: the dropped pictures become one video, each shown for <see cref="SlideSeconds"/>, fitted into- /// <see cref="SlideshowWidth"/> × <see cref="SlideshowHeight"/> with black bars.+ /// MP4: the dropped pictures become one 1080p video, each shown for <see cref="SlideSeconds"/>, fitted in with+ /// black bars./// </summary>public bool Slideshow { get; set; }@@ -596,13 +596,7 @@ public sealed class MediaOptionspublic bool Crossfade { get; set; } = true;/// <summary>With <see cref="Crossfade"/>: how long a fade takes, in seconds (at most half a picture's time).</summary>- public double CrossfadeSeconds { get; set; } = 1;-- /// <summary>Frame size of a <see cref="Slideshow"/> (1080p unless the preset says otherwise).</summary>- public int SlideshowWidth { get; set; } = 1920;-- /// <inheritdoc cref="SlideshowWidth"/>- public int SlideshowHeight { get; set; } = 1080;+ public const double CrossfadeSeconds = 1;/// <summary>/// Audio target: copy the sound as it is when the target takes its codec (AAC from a phone video into M4A), with@@ -622,9 +616,7 @@ public sealed class MediaOptions/// <summary>The fade between two pictures in seconds: 0 without <see cref="Crossfade"/>, never over half a picture's time.</summary>[JsonIgnore]- public double FadeDuration => Crossfade && double.IsFinite(CrossfadeSeconds) && CrossfadeSeconds > 0- ? Math.Min(CrossfadeSeconds, SlideDuration / 2)- : 0;+ public double FadeDuration => Crossfade ? Math.Min(CrossfadeSeconds, SlideDuration / 2) : 0;public MediaOptions Clone() => (MediaOptions)MemberwiseClone();}diff --git a/src/Filee.Engines/Media/FfmpegConverter.cs b/src/Filee.Engines/Media/FfmpegConverter.csindex 94be8ec1..6c6b9938 100644--- a/src/Filee.Engines/Media/FfmpegConverter.cs+++ b/src/Filee.Engines/Media/FfmpegConverter.cs@@ -201,8 +201,6 @@ public sealed partial class FfmpegConverter(ILogger? logger = null) : IConverterreturn [output];}- public bool CanJoinInto(string format) => MediaOptions.JoinTargets.Contains(format) && (_tools ?? Locate()) is not null;-public async Task JoinAsync(IReadOnlyList<string> inputPaths, string outputPath, string format, Preset preset,string workDirectory, IProgress<double>? progress, CancellationToken cancellationToken){@@ -267,7 +265,7 @@ public sealed partial class FfmpegConverter(ILogger? logger = null) : IConverter{if (pictures.Count > MediaJoining.MaxSlides)throw new ConversionRefusedException("error.too_many_pictures", $"{pictures.Count} > {MediaJoining.MaxSlides}");- var (width, height) = MediaJoining.SlideshowFrame(media);+ var (width, height) = (MediaJoining.SlideshowWidth, MediaJoining.SlideshowHeight);var slides = new List<string>();for (var i = 0; i < pictures.Count; i++){diff --git a/src/Filee.Engines/Media/MediaEncoding.cs b/src/Filee.Engines/Media/MediaEncoding.csindex 361f139a..50170f95 100644--- a/src/Filee.Engines/Media/MediaEncoding.cs+++ b/src/Filee.Engines/Media/MediaEncoding.cs@@ -588,6 +588,10 @@ internal static class MediaEncodingif (info.Video?.Codec == "hevc" && target is "mp4" or "m4v" or "mov")args.AddRange(["-tag:v", "hvc1"]);args.AddRange(["-map_metadata", "-1", "-map_metadata:s", "-1", "-map_chapters", "-1", "-fflags", "+bitexact"]);+ // Attachments are an MKV's subtitle fonts (without them styled subtitles fall back to another font); each needs+ // its own file name and type tags. Only when there are any: FFmpeg refuses a specifier that matches nothing.+ if (info.HasAttachments)+ args.AddRange(["-map", "0:t", "-map_metadata:s:t", "0:s:t"]);args.AddRange(ContainerArguments(target));args.AddRange(["-max_muxing_queue_size", "4096", "-f", spec.Muxer, "-progress", "pipe:1", output]);return args;diff --git a/src/Filee.Engines/Media/MediaInfo.cs b/src/Filee.Engines/Media/MediaInfo.csindex e058fbac..695397b8 100644--- a/src/Filee.Engines/Media/MediaInfo.cs+++ b/src/Filee.Engines/Media/MediaInfo.cs@@ -16,8 +16,12 @@ namespace Filee.Engines.Media;/// <param name="Codec">FFmpeg's codec name (<c>h264</c>, <c>hevc</c>, …), null when unknown.</param>/// <param name="PixelFormat">FFmpeg's pixel format (<c>yuv420p</c>, <c>yuv420p10le</c>, …), null when unknown.</param>/// <param name="Color">Colour primaries, transfer and matrix as FFmpeg names them (<c>bt2020</c>, <c>smpte2084</c>, …).</param>+/// <param name="HeaderHash">+/// A hash of the codec's setup data (H.264 / HEVC parameter sets), equal only for streams a decoder reads with the same+/// setup; null when the file has none.+/// </param>internal sealed record VideoStreamInfo(int Width, int Height, double SampleAspect, int Rotation, double FrameRate,- string? Codec = null, string? PixelFormat = null, VideoColor? Color = null)+ string? Codec = null, string? PixelFormat = null, VideoColor? Color = null, string? HeaderHash = null){/// <summary>/// True for more than 8 bits per sample (yuv420p10le, p010le, yuv422p12le, …): phones' HDR video, ProRes, 10-bit@@ -71,8 +75,9 @@ internal sealed record AudioStreamInfo(int Channels, int SampleRate, string? Cod/// <param name="Audio">First audio stream, if any.</param>/// <param name="CoverStreamIndex">Index of an embedded cover picture (JPEG or PNG) such as album art, if any.</param>/// <param name="SubtitleStreams">Every subtitle stream in file order, null when there is none.</param>+/// <param name="HasAttachments">The file carries attachments (an MKV's subtitle fonts).</param>internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, AudioStreamInfo? Audio, int? CoverStreamIndex,- IReadOnlyList<SubtitleStreamInfo>? SubtitleStreams = null)+ IReadOnlyList<SubtitleStreamInfo>? SubtitleStreams = null, bool HasAttachments = false){/// <summary>Every subtitle stream, in file order.</summary>public IReadOnlyList<SubtitleStreamInfo> Subtitles => SubtitleStreams ?? [];@@ -83,7 +88,7 @@ internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, Audpublic static async Task<MediaInfo> ProbeAsync(string ffprobe, string input, CancellationToken cancellationToken){var result = await ProcessRunner.RunAsync(ffprobe,- ["-hide_banner", "-v", "error", "-print_format", "json", "-show_format", "-show_streams", input],+ ["-hide_banner", "-v", "error", "-print_format", "json", "-show_format", "-show_streams", "-show_data_hash", "MD5", input],ProbeTimeout, cancellationToken);if (result.ExitCode != 0){@@ -103,6 +108,7 @@ internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, AudAudioStreamInfo? audio = null;int? cover = null;List<SubtitleStreamInfo> subtitles = [];+ var attachments = false;double longestStream = 0;if (root.TryGetProperty("streams", out var streams) && streams.ValueKind == JsonValueKind.Array){@@ -125,12 +131,16 @@ internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, AudFrameRateOf(stream),Text(stream, "codec_name"),Text(stream, "pix_fmt"),- ColorOf(stream));+ ColorOf(stream),+ Text(stream, "extradata_hash"));break;case "audio" when audio is null:audio = new AudioStreamInfo((int)Number(stream, "channels"), (int)Number(stream, "sample_rate"),Text(stream, "codec_name"));break;+ case "attachment":+ attachments = true;+ break;case "subtitle":subtitles.Add(new SubtitleStreamInfo((int)Number(stream, "index"), Text(stream, "codec_name"),stream.TryGetProperty("tags", out var tags) ? Text(tags, "language") : null));@@ -143,7 +153,7 @@ internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, Audif (seconds <= 0)seconds = longestStream;return new MediaInfo(seconds > 0 ? TimeSpan.FromSeconds(seconds) : null, video, audio, cover,- subtitles.Count > 0 ? subtitles : null);+ subtitles.Count > 0 ? subtitles : null, attachments);}/// <summary>The colour tags of a video stream; null when it has none ("unknown" counts as none).</summary>diff --git a/src/Filee.Engines/Media/MediaJoining.cs b/src/Filee.Engines/Media/MediaJoining.csindex bca79430..2928c6f1 100644--- a/src/Filee.Engines/Media/MediaJoining.cs+++ b/src/Filee.Engines/Media/MediaJoining.cs@@ -30,8 +30,8 @@ internal static class MediaJoining/// <summary>/// True when the clips can be joined without an encode: every one is a video MP4 takes as it is, all with the same- /// codec, picture size, pixel format, turn and pixel shape, and the same kind of sound (or none at all). Videos from- /// one phone usually are.+ /// codec setup (parameter sets: the MP4 keeps only the first clip's), picture size, pixel format, frame rate, turn+ /// and pixel shape, and the same kind of sound (or none at all). Videos from one phone usually are./// </summary>public static bool CanCopyVideos(IReadOnlyList<MediaInfo> clips, MediaOptions options){@@ -39,7 +39,8 @@ internal static class MediaJoiningreturn false;var sameVideo = clips.All(c => c.Video is { } v && MediaEncoding.CanCopyVideo("mp4", v, options)&& v.Codec == first.Codec && v.Width == first.Width && v.Height == first.Height && v.PixelFormat == first.PixelFormat- && v.Rotation == first.Rotation && Math.Abs(v.SampleAspect - first.SampleAspect) < 0.01);+ && v.Rotation == first.Rotation && Math.Abs(v.SampleAspect - first.SampleAspect) < 0.01+ && Math.Abs(v.FrameRate - first.FrameRate) < 0.01 && v.HeaderHash is not null && v.HeaderHash == first.HeaderHash);if (!sameVideo)return false;if (clips[0].Audio is not { } sound)@@ -184,9 +185,8 @@ internal static class MediaJoining// ───────────────────────── Pictures ─────────────────────────- /// <summary>The slideshow frame of a preset: even, between 16 and 8K, 1080p by default.</summary>- public static (int Width, int Height) SlideshowFrame(MediaOptions options) =>- (MediaEncoding.Even(Math.Clamp(options.SlideshowWidth, 16, 7680)), MediaEncoding.Even(Math.Clamp(options.SlideshowHeight, 16, 4320)));+ /// <summary>The frame every slideshow picture is fitted into: 1080p.</summary>+ public const int SlideshowWidth = 1920, SlideshowHeight = 1080;/// <summary>How long a slideshow of <paramref name="count"/> pictures plays: the fades overlap the pictures.</summary>public static TimeSpan SlideshowDuration(int count, MediaOptions options) => count <= 0@@ -213,7 +213,7 @@ internal static class MediaJoining/// <param name="workDirectory">Where a long filter graph is written (<see cref="MaxInlineGraph"/>).</param>public static List<string> Slideshow(IReadOnlyList<string> slides, string output, MediaOptions options, string encoder, string workDirectory){- var (width, height) = SlideshowFrame(options);+ var (width, height) = (SlideshowWidth, SlideshowHeight);var show = Seconds(options.SlideDuration);var fade = options.FadeDuration;var graph = new List<string>();diff --git a/tests/Filee.Core.Tests/FakeConverter.cs b/tests/Filee.Core.Tests/FakeConverter.csindex d500fddf..cf85eadb 100644--- a/tests/Filee.Core.Tests/FakeConverter.cs+++ b/tests/Filee.Core.Tests/FakeConverter.cs@@ -165,11 +165,13 @@ internal sealed class FakeJoiner : IMediaJoinerpublic bool Throw { get; set; }- public bool CanJoinInto(string format) => true;+ /// <summary>Called when the join starts, before anything is written.</summary>+ public Action? Joining { get; set; }public Task JoinAsync(IReadOnlyList<string> inputPaths, string outputPath, string format, Filee.Core.Presets.Preset preset,string workDirectory, IProgress<double>? progress, CancellationToken cancellationToken){+ Joining?.Invoke();if (Throw)throw new InvalidOperationException("boom");Inputs.AddRange(inputPaths);diff --git a/tests/Filee.Core.Tests/MediaToolTests.cs b/tests/Filee.Core.Tests/MediaToolTests.csindex e48fe033..324725c1 100644--- a/tests/Filee.Core.Tests/MediaToolTests.cs+++ b/tests/Filee.Core.Tests/MediaToolTests.cs@@ -55,8 +55,9 @@ public class MediaToolTests[Fact]public void Slides_and_fades_stay_within_their_limits(){- var media = new MediaOptions { SlideSeconds = 2, CrossfadeSeconds = 5 };- Assert.Equal(1, media.FadeDuration); // at most half of the time a picture is shown+ var media = new MediaOptions { SlideSeconds = 1.5 };+ Assert.Equal(0.75, media.FadeDuration); // at most half of the time a picture is shown+ Assert.Equal(1, new MediaOptions { SlideSeconds = 3 }.FadeDuration);media.Crossfade = false;Assert.Equal(0, media.FadeDuration);Assert.Equal(MediaOptions.MaxSlideSeconds, new MediaOptions { SlideSeconds = 1e9 }.SlideDuration);@@ -183,6 +184,42 @@ public class MediaToolTestsAssert.False(File.Exists(Path.Combine(dir.Path, "a.png"))); // no picture left beside the source}+ [Fact]+ public async Task A_slideshow_is_not_shown_as_done_while_its_video_is_made()+ {+ using var dir = new TempDir();+ var photos = new[] { dir.File("a.jpg"), dir.File("b.jpg") };+ var job = new ConversionJob(photos, BuiltIn("video-from-images"), "Test");+ double atJoin = -1;+ var joiner = new FakeJoiner { Joining = () => atJoin = job.Progress };+ await using var queue = new JobQueue(new ConverterCatalog([new FakeConverter("magick", new ConversionEdge("jpg", "png"))]),+ mediaJoiner: joiner);++ await queue.RunAsync(job);++ Assert.Equal(JobState.Completed, job.State);+ Assert.InRange(atJoin, 0.01, 0.5); // the pictures are ready, the video isn't+ Assert.Equal(1, job.Progress);+ }++ [Fact]+ public async Task Compressing_keeps_the_original_when_the_copy_would_not_be_smaller()+ {+ using var dir = new TempDir();+ // The fake engine writes a short line: smaller than the big file, bigger than the tiny one.+ var big = dir.File("big.jpg", new string('x', 10_000));+ var tiny = dir.File("tiny.jpg", "x");+ await using var queue = new JobQueue(new ConverterCatalog([new FakeConverter("magick", new ConversionEdge("jpg", "jpg"))]));++ var job = await Run(queue, [big, tiny], BuiltIn("compress-image"));++ Assert.Equal(JobState.Completed, job.State);+ Assert.Null(job.Files[0].NoticeKey);+ Assert.True(new FileInfo(Path.Combine(dir.Path, "big_compressed.jpg")).Length < 10_000);+ Assert.Equal("notice.pdf_not_smaller", job.Files[1].NoticeKey);+ Assert.Equal("x", File.ReadAllText(Path.Combine(dir.Path, "tiny_compressed.jpg"))); // the original, copied+ }+[Fact]public async Task A_failed_join_fails_every_file_and_leaves_no_output(){diff --git a/tests/Filee.Engines.Tests/MediaJoiningTests.cs b/tests/Filee.Engines.Tests/MediaJoiningTests.csindex 03ba9efe..9bd35985 100644--- a/tests/Filee.Engines.Tests/MediaJoiningTests.cs+++ b/tests/Filee.Engines.Tests/MediaJoiningTests.cs@@ -8,7 +8,7 @@ namespace Filee.Engines.Tests;public class MediaJoiningTests{- private static readonly VideoStreamInfo PhoneVideo = new(1920, 1080, 1, 0, 30, "h264", "yuv420p");+ private static readonly VideoStreamInfo PhoneVideo = new(1920, 1080, 1, 0, 30, "h264", "yuv420p", HeaderHash: "MD5:1");private static MediaInfo Clip(VideoStreamInfo? video = null, AudioStreamInfo? audio = null, double seconds = 10) =>new(TimeSpan.FromSeconds(seconds), video, audio, null);@@ -37,6 +37,11 @@ public class MediaJoiningTestsAssert.False(MediaJoining.CanCopyVideos([clip, clip with { Audio = new AudioStreamInfo(2, 44100, "aac") }], options));Assert.False(MediaJoining.CanCopyVideos([clip, clip with { Audio = null }], options));Assert.False(MediaJoining.CanCopyVideos([clip, clip], new MediaOptions { Quality = MediaQuality.Small }));+ // Same size and codec from another encoder (other parameter sets), another frame rate, or no setup data to compare.+ Assert.False(MediaJoining.CanCopyVideos([clip, clip with { Video = PhoneVideo with { HeaderHash = "MD5:2" } }], options));+ Assert.False(MediaJoining.CanCopyVideos([clip, clip with { Video = PhoneVideo with { FrameRate = 60 } }], options));+ var unknown = clip with { Video = PhoneVideo with { HeaderHash = null } };+ Assert.False(MediaJoining.CanCopyVideos([unknown, unknown], options));}[Fact]@@ -108,7 +113,7 @@ public class MediaJoiningTests[Fact]public void Slides_fade_into_each_other_at_the_right_times(){- var options = new MediaOptions { Slideshow = true, SlideSeconds = 3, CrossfadeSeconds = 1 };+ var options = new MediaOptions { Slideshow = true, SlideSeconds = 3 };var args = MediaJoining.Slideshow(["1.jpg", "2.jpg", "3.jpg"], "out.mp4", options, "libx264", "work");@@ -140,7 +145,6 @@ public class MediaJoiningTestsAssert.Contains("scale=1920:1080:force_original_aspect_ratio=decrease:force_divisible_by=2:flags=lanczos,pad=1920:1080",After(args, "-vf"));Assert.Equal("mjpeg", After(args, "-c:v"));- Assert.Equal((1920, 1080), MediaJoining.SlideshowFrame(new MediaOptions()));}[Fact]@@ -202,6 +206,12 @@ public class MediaJoiningTestsAssert.DoesNotContain("0", args.Where((a, i) => i > 0 && args[i - 1] == "-map")); // never every stream (data tracks)Assert.Equal("hvc1", After(args, "-tag:v"));Assert.Equal("mov", After(args, "-f"));+ Assert.DoesNotContain("0:t", args); // FFmpeg refuses an attachment specifier that matches nothing++ var episode = new MediaInfo(TimeSpan.FromSeconds(10), PhoneVideo, null, null, HasAttachments: true);+ var mkv = MediaEncoding.WithoutMetadata("in.mkv", "out.mkv", "mkv", episode);+ Assert.Contains("0:t", mkv); // subtitle fonts stay, with their file name and type+ Assert.Equal("0:s:t", After(mkv, "-map_metadata:s:t"));var song = new MediaInfo(TimeSpan.FromSeconds(10), null, new AudioStreamInfo(2, 44100, "mp3"), 1);var audio = MediaEncoding.WithoutMetadata("in.mp3", "out.mp3", "mp3", song);diff --git a/tests/Filee.Engines.Tests/MediaToolRealTests.cs b/tests/Filee.Engines.Tests/MediaToolRealTests.csindex d8c3d7ef..f4667b67 100644--- a/tests/Filee.Engines.Tests/MediaToolRealTests.cs+++ b/tests/Filee.Engines.Tests/MediaToolRealTests.cs@@ -110,6 +110,13 @@ public class MediaToolRealTests(EngineFixture fx) : IClassFixture<EngineFixture>return result.StandardOutput.Split('=', 2)[^1].Trim();}+ /// <summary>The MD5 of every video packet of a file, in order.</summary>+ private static async Task<List<string>> PacketHashesAsync(string path)+ {+ var result = await FfmpegAsync(["-hide_banner", "-nostdin", "-v", "error", "-i", path, "-map", "0:v:0", "-c", "copy", "-f", "framemd5", "-"]);+ return [.. result.StandardOutput.Split('\n').Where(l => l.Length > 0 && l[0] != '#').Select(l => l.Split(',')[^1].Trim())];+ }+// ───────────────────────── Merge videos ─────────────────────────[Fact]@@ -126,10 +133,34 @@ public class MediaToolRealTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal(Path.Combine(folder, "a_merged.mp4"), output);var probed = await ProbeAsync(output);Assert.InRange(probed.Duration, 3.3, 3.7);+ // Copied, not encoded again: the merged file starts with the first clip's packets, byte for byte (FFmpeg puts+ // the stream's parameter sets into each clip's first packet, so that one is skipped).+ var firstPackets = await PacketHashesAsync(first);+ Assert.Equal(firstPackets.Skip(1), (await PacketHashesAsync(output)).Take(firstPackets.Count).Skip(1));Assert.Equal(("h264", 320, 240), (probed.Video!.Codec, probed.Video.Width, probed.Video.Height));Assert.Equal("aac", probed.Audio!.Codec);}+ [Fact]+ public async Task Clips_of_one_size_but_other_encoder_settings_still_play_after_merging()+ {+ SkipUnlessInstalled();+ var folder = fx.NewFolder();+ // Same codec, size and pixel format; another profile and header, as from two different phones.+ var first = await MakeClipAsync(folder, "a.mp4", 2, extra: ["-c:v", "libx264", "-profile:v", "high", "-pix_fmt", "yuv420p", "-c:a", "aac"]);+ var second = await MakeClipAsync(folder, "b.mp4", 2, extra:+ ["-c:v", "libx264", "-profile:v", "baseline", "-pix_fmt", "yuv420p", "-c:a", "aac"]);++ var output = await RunAsync([first, second], BuiltIn("merge-videos"));++ var decode = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,+ ["-hide_banner", "-nostdin", "-v", "error", "-i", output, "-f", "null", "-"], TimeSpan.FromMinutes(2), CancellationToken.None);+ Assert.True(decode.ExitCode == 0 && decode.StandardError.Trim().Length == 0, decode.StandardError);+ Assert.InRange((await ProbeAsync(output)).Duration, 3.8, 4.2);+ // Not copied: the first clip's packets are encoded again.+ Assert.NotEqual((await PacketHashesAsync(first)).Skip(1).Take(10), (await PacketHashesAsync(output)).Skip(1).Take(10));+ }+[Fact]public async Task Clips_of_other_sizes_and_without_sound_are_fitted_into_the_first_ones_frame(){@@ -274,6 +305,42 @@ public class MediaToolRealTests(EngineFixture fx) : IClassFixture<EngineFixture>Assert.Equal(await StreamHashAsync(clip, "a"), await StreamHashAsync(output, "a"));}+ [Fact]+ public async Task A_portrait_phone_video_stays_upright()+ {+ SkipUnlessInstalled();+ var folder = fx.NewFolder();+ var plain = await MakeClipAsync(folder, "plain.mov", 1, audio: false, extra: ["-c:v", "libx264"]);+ var clip = Path.Combine(folder, "IMG_0003.mov");+ // Phones store portrait video landscape, with a display matrix that turns it 90° (set on a copy: an encode would+ // turn the pixels instead).+ await FfmpegAsync(["-hide_banner", "-nostdin", "-v", "error", "-y", "-display_rotation", "90", "-i", plain, "-c", "copy",+ "-metadata", "location=+37.5+127.0/", clip]);+ Assert.Equal(90, Math.Abs((await MediaInfo.ProbeAsync(Tools!.Value.Ffprobe, clip, CancellationToken.None)).Video!.Rotation));++ var output = await RunAsync([clip], BuiltIn("remove-metadata"));++ Assert.Equal(90, Math.Abs((await MediaInfo.ProbeAsync(Tools!.Value.Ffprobe, output, CancellationToken.None)).Video!.Rotation));+ Assert.False((await ProbeAsync(output)).Tags.ContainsKey("location"));+ }++ [Fact]+ public async Task An_mkv_keeps_its_subtitle_fonts()+ {+ SkipUnlessInstalled();+ var folder = fx.NewFolder();+ var font = Path.Combine(folder, "Sub Font.ttf");+ await File.WriteAllBytesAsync(font, Enumerable.Range(0, 2048).Select(i => (byte)i).ToArray(), TestContext.Current.CancellationToken);+ var clip = await MakeClipAsync(folder, "episode.mkv", 1, extra:+ ["-c:v", "libx264", "-c:a", "aac", "-metadata", "title=Episode 1", "-attach", font,+ "-metadata:s:t", "mimetype=application/x-truetype-font"]);++ var after = await ProbeAsync(await RunAsync([clip], BuiltIn("remove-metadata")));++ Assert.EndsWith("Sub Font.ttf", Assert.Single(after.Streams, s => s.Type == "attachment").Tags["filename"]);+ Assert.False(after.Tags.ContainsKey("title"));+ }+[Fact]public async Task A_song_loses_its_tags_and_stays_an_mp3(){