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.

fix(media): mux separate M4S audio and video

Stamina9c74758bf41db
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index 9e0cc5d5..575a7e55 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -801,6 +801,8 @@ in the preset library only). Video and audio need FFmpeg.
clip gives its first track FFmpeg can decode, and the clips are copied only when that track has the same number in
every clip. **Merge audio** always decodes and joins the sound
into one M4A, so no encoder padding or gap is left between files. Both are in `Media/MediaJoining.cs`.
+ As a special case, a pair of self-contained `.m4s` files, one with only video and one with only audio, is muxed
+ together into one MP4 without re-encoding (in either order). Both codecs and the preset's settings must allow copying.
- **Video from images**: each picture is fitted into 1920 × 1080 as a JPEG, then shown for the preset's seconds with
crossfades (`xfade`) of 1 s, at most half a picture's time, or cuts. At most `MediaOptions.MaxSlides` (300)
pictures, not counting downloads that failed; the queue refuses more before any is converted. A filter graph too
diff --git a/src/Filee.Engines/Media/FfmpegConverter.cs b/src/Filee.Engines/Media/FfmpegConverter.cs
index ab192d74..9e78ce6e 100644
--- a/src/Filee.Engines/Media/FfmpegConverter.cs
+++ b/src/Filee.Engines/Media/FfmpegConverter.cs
@@ -277,7 +277,15 @@ public sealed partial class FfmpegConverter(ILogger? logger = null) : IConverter
}
duration = TimeSpan.FromSeconds(clips.Sum(c => c.Info.Duration?.TotalSeconds ?? 0));
start = 0.05;
- if (format == "m4a")
+ if (format == "mp4" && MediaJoining.M4sPair(clips) is { } pair)
+ {
+ if (!MediaEncoding.CanCopyVideo("mp4", pair.Video.Info.Video!, media)
+ || !MediaEncoding.CanCopyAudio("mp4", pair.Audio.Info.Audio!, media))
+ throw new InvalidOperationException("The selected streams or settings cannot be copied into MP4.");
+ args = MediaJoining.MuxM4sPair(pair.Video, pair.Audio, outputPath);
+ duration = pair.Video.Info.Duration ?? TimeSpan.Zero;
+ }
+ else if (format == "m4a")
{
if (clips.FirstOrDefault(c => c.Info.Audio is null && c.Info.Video is null) is { Path: { } silent })
throw new InvalidOperationException($"{Path.GetFileName(silent)} has no sound.");
diff --git a/src/Filee.Engines/Media/MediaJoining.cs b/src/Filee.Engines/Media/MediaJoining.cs
index f609c272..709f612b 100644
--- a/src/Filee.Engines/Media/MediaJoining.cs
+++ b/src/Filee.Engines/Media/MediaJoining.cs
@@ -25,6 +25,30 @@ internal static class MediaJoining
// ───────────────────────── Videos ─────────────────────────
+ /// <summary>A self-contained video-only and audio-only M4S pair, in either selection order.</summary>
+ public static ((string Path, MediaInfo Info) Video, (string Path, MediaInfo Info) Audio)? M4sPair(
+ IReadOnlyList<(string Path, MediaInfo Info)> clips)
+ {
+ if (clips.Count != 2 || clips.Any(c => !Path.GetExtension(c.Path).Equals(".m4s", StringComparison.OrdinalIgnoreCase)))
+ return null;
+ var video = clips.Where(c => c.Info.Video is not null && c.Info.Audio is null).ToList();
+ var audio = clips.Where(c => c.Info.Video is null && c.Info.Audio is not null).ToList();
+ return video.Count == 1 && audio.Count == 1 ? (video[0], audio[0]) : null;
+ }
+
+ /// <summary>Copy the selected streams into MP4 without re-encoding.</summary>
+ public static List<string> MuxM4sPair((string Path, MediaInfo Info) video, (string Path, MediaInfo Info) audio, string output)
+ {
+ List<string> args = [.. MediaEncoding.Common, "-i", video.Path, "-i", audio.Path,
+ "-map", "0:V:0", "-map", $"1:a:{audio.Info.Audio!.Ordinal}", "-c", "copy"];
+ if (video.Info.Video?.Codec == "hevc")
+ args.AddRange(["-tag:v", "hvc1"]);
+ args.AddRange(MediaEncoding.MetadataArguments());
+ args.AddRange(MediaEncoding.ContainerArguments("mp4"));
+ args.AddRange(["-max_muxing_queue_size", "4096", "-f", "mp4", "-progress", "pipe:1", output]);
+ return args;
+ }
+
/// <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 setup (parameter sets: the MP4 keeps only the first clip's), picture size, pixel format, frame rate, turn
diff --git a/tests/Filee.Engines.Tests/MediaJoiningTests.cs b/tests/Filee.Engines.Tests/MediaJoiningTests.cs
index 51214b94..a322a288 100644
--- a/tests/Filee.Engines.Tests/MediaJoiningTests.cs
+++ b/tests/Filee.Engines.Tests/MediaJoiningTests.cs
@@ -15,6 +15,25 @@ public class MediaJoiningTests
private static string After(IReadOnlyList<string> args, string option) => args[args.ToList().IndexOf(option) + 1];
+ [Fact]
+ public void Separate_m4s_streams_are_identified_in_either_order()
+ {
+ var video = ("picture.m4s", Clip(PhoneVideo));
+ var audio = ("sound.M4S", Clip(audio: new AudioStreamInfo(2, 48000, "aac", 1)));
+ foreach (var clips in new[] { new[] { video, audio }, new[] { audio, video } })
+ {
+ var pair = MediaJoining.M4sPair(clips)!.Value;
+ Assert.Equal(video.Item1, pair.Video.Path);
+ var args = MediaJoining.MuxM4sPair(pair.Video, pair.Audio, "out.mp4");
+ Assert.Contains("0:V:0", args);
+ Assert.Contains("1:a:1", args);
+ Assert.Equal("copy", After(args, "-c"));
+ }
+ Assert.Null(MediaJoining.M4sPair([video, ("sound.mp4", audio.Item2)]));
+ Assert.Null(MediaJoining.M4sPair([video, video]));
+ Assert.Null(MediaJoining.M4sPair([video]));
+ }
+
[Fact]
public void Clips_from_one_phone_are_copied()
{
diff --git a/tests/Filee.Engines.Tests/MediaToolRealTests.cs b/tests/Filee.Engines.Tests/MediaToolRealTests.cs
index f7148882..c69d67e6 100644
--- a/tests/Filee.Engines.Tests/MediaToolRealTests.cs
+++ b/tests/Filee.Engines.Tests/MediaToolRealTests.cs
@@ -119,6 +119,28 @@ public class MediaToolRealTests(EngineFixture fx) : IClassFixture<EngineFixture>
// ───────────────────────── Merge videos ─────────────────────────
+ [Theory]
+ [InlineData(false)]
+ [InlineData(true)]
+ public async Task Separate_m4s_video_and_audio_are_muxed_without_reencoding(bool audioFirst)
+ {
+ SkipUnlessInstalled();
+ var folder = fx.NewFolder();
+ var video = await MakeClipAsync(folder, "画面.m4s", 2, audio: false,
+ extra: ["-c:v", "libx264", "-pix_fmt", "yuv420p", "-movflags", "frag_keyframe+empty_moov", "-f", "mp4"]);
+ var audio = await MakeToneAsync(folder, "声音.m4s", 2,
+ extra: ["-c:a", "aac", "-movflags", "frag_keyframe+empty_moov", "-f", "mp4"]);
+
+ var output = await RunAsync(audioFirst ? [audio, video] : [video, audio], BuiltIn("merge-videos"));
+
+ var info = await ProbeAsync(output);
+ Assert.Equal("h264", info.Video!.Codec);
+ Assert.Equal("aac", info.Audio!.Codec);
+ Assert.InRange(info.Duration, 1.9, 2.2);
+ Assert.Equal(await StreamHashAsync(video, "v"), await StreamHashAsync(output, "v"));
+ Assert.Equal(await StreamHashAsync(audio, "a"), await StreamHashAsync(output, "a"));
+ }
+
[Fact]
public async Task Clips_from_one_camera_are_merged_without_an_encode()
{