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.

Keep every audio track when converting a video (#396)

cre8ive-marketing0a96b1f984ad
diff --git a/docs/ENGINES.md b/docs/ENGINES.md
index 3ba038b0..6f69b593 100644
--- a/docs/ENGINES.md
+++ b/docs/ENGINES.md
@@ -1328,8 +1328,9 @@ format, and FFmpeg reads what the file holds, so a `.dat` with no video in it fa
A video without subtitles fails with "The video has no subtitle track."; one with only picture subtitles (Blu-ray
PGS, DVD, DVB) says they can't be turned into text without OCR. Video → SAMI, LRC, TTML, … goes through SRT and the
subtitle engine (ASS and WebVTT extraction cost 11 so SRT is the hub).
-- The first video stream (cover pictures excluded) and the first audio stream are converted; files without sound
- still convert to video. Title, artist and other tags and chapters are kept, album art too for MP3, M4A, M4B and
+- The first video stream (cover pictures excluded) is converted. Video to video keeps every audio track (languages,
+ commentary), each copied where the target takes its codec or encoded on its own, with its language tag; FLV and DV
+ hold one and keep the first. Video to audio takes the first. Files without sound still convert to video. Title, artist and other tags and chapters are kept, album art too for MP3, M4A, M4B and
FLAC. Portrait phone videos are turned upright. Odd sizes are evened out, frame rates that MPEG-2/MXF/MPEG-4 part 2
cannot store are converted, and channels WMA or AMR cannot hold are mixed down.
- Progress comes from `-progress` (position ÷ duration from ffprobe); the GIF palette pass reports per-frame stats of
diff --git a/src/Filee.Engines/Media/MediaEncoding.cs b/src/Filee.Engines/Media/MediaEncoding.cs
index be71f1a5..e669986b 100644
--- a/src/Filee.Engines/Media/MediaEncoding.cs
+++ b/src/Filee.Engines/Media/MediaEncoding.cs
@@ -230,12 +230,15 @@ internal static class MediaEncoding
IReadOnlySet<string>? encoders, HardwareEncoder? hardware)
{
var video = info.Video ?? throw new InvalidOperationException("The file has no video stream.");
- // An input without sound is fine: the output simply has no audio track.
- var audio = options.RemoveAudio ? null : info.Audio;
+ // An input without sound is fine: the output simply has no audio track. Every track is kept (languages,
+ // commentary, issue #396), except in containers that hold one.
+ IReadOnlyList<AudioStreamInfo> tracks = options.RemoveAudio ? []
+ : SingleAudioTargets.Contains(target) ? [.. info.AudioTracks.Take(1)]
+ : info.AudioTracks;
List<string> args = [.. Common, "-i", input, "-map", "0:V:0"];
- if (audio is not null)
- args.AddRange(["-map", "0:a:0"]);
+ for (var i = 0; i < tracks.Count; i++)
+ args.AddRange(["-map", $"0:a:{i}"]);
if (CanCopyVideo(target, video, options))
{
@@ -258,12 +261,16 @@ internal static class MediaEncoding
args.AddRange(VideoCodecArguments(target, VideoEncoder(target, spec.VideoCodec!, options.VideoCodec, encoders), options.Quality, video, pal));
}
- if (audio is null)
+ if (tracks.Count == 0)
args.Add("-an");
- else if (CanCopyAudio(target, audio, options))
- args.AddRange(["-c:a", "copy"]);
- else
- args.AddRange(AudioCodecArguments(target, spec.AudioCodec, options, audio, inVideo: true));
+ for (var i = 0; i < tracks.Count; i++)
+ {
+ // Each track on its own: copied where the container takes it, else encoded for its own channel count.
+ var trackArgs = CanCopyAudio(target, tracks[i], options)
+ ? ["-c:a", "copy"]
+ : AudioCodecArguments(target, spec.AudioCodec, options, tracks[i], inVideo: true);
+ args.AddRange(tracks.Count == 1 ? trackArgs : ForAudioTrack(trackArgs, i));
+ }
args.AddRange(MetadataArguments());
args.AddRange(ContainerArguments(target));
@@ -272,6 +279,20 @@ internal static class MediaEncoding
return args;
}
+ /// <summary>Containers that hold a single audio track: the first one is kept.</summary>
+ private static readonly HashSet<string> SingleAudioTargets = ["flv", "dv"];
+
+ /// <summary>Audio options addressed to one output track (<c>-b:a</c> → <c>-b:a:2</c>, <c>-ac</c> → <c>-ac:a:2</c>).</summary>
+ internal static IEnumerable<string> ForAudioTrack(IReadOnlyList<string> args, int track) =>
+ args.Select((arg, i) => arg switch
+ {
+ // Values are never options: only the names are addressed.
+ _ when i > 0 && args[i - 1].StartsWith('-') && !arg.StartsWith('-') => arg,
+ "-c:a" or "-b:a" or "-q:a" => $"{arg}:{track}",
+ "-ac" or "-ar" => $"{arg}:a:{track}",
+ _ => arg,
+ });
+
/// <summary>
/// Codecs a container takes as they are. A video whose streams are listed for the target is only rewrapped
/// (stream copy): no quality loss, HDR and 10-bit kept, seconds instead of minutes. iPhone videos (HEVC or H.264
diff --git a/src/Filee.Engines/Media/MediaInfo.cs b/src/Filee.Engines/Media/MediaInfo.cs
index e058fbac..8cd483b2 100644
--- a/src/Filee.Engines/Media/MediaInfo.cs
+++ b/src/Filee.Engines/Media/MediaInfo.cs
@@ -71,9 +71,13 @@ 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="AudioStreams">Every audio stream in file order (languages, commentary), null for none or only one.</param>
internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, AudioStreamInfo? Audio, int? CoverStreamIndex,
- IReadOnlyList<SubtitleStreamInfo>? SubtitleStreams = null)
+ IReadOnlyList<SubtitleStreamInfo>? SubtitleStreams = null, IReadOnlyList<AudioStreamInfo>? AudioStreams = null)
{
+ /// <summary>Every audio stream, in file order: <see cref="Audio"/> first.</summary>
+ public IReadOnlyList<AudioStreamInfo> AudioTracks => AudioStreams ?? (Audio is null ? [] : [Audio]);
+
/// <summary>Every subtitle stream, in file order.</summary>
public IReadOnlyList<SubtitleStreamInfo> Subtitles => SubtitleStreams ?? [];
@@ -103,6 +107,7 @@ internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, Aud
AudioStreamInfo? audio = null;
int? cover = null;
List<SubtitleStreamInfo> subtitles = [];
+ List<AudioStreamInfo> audios = [];
double longestStream = 0;
if (root.TryGetProperty("streams", out var streams) && streams.ValueKind == JsonValueKind.Array)
{
@@ -127,9 +132,10 @@ internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, Aud
Text(stream, "pix_fmt"),
ColorOf(stream));
break;
- case "audio" when audio is null:
- audio = new AudioStreamInfo((int)Number(stream, "channels"), (int)Number(stream, "sample_rate"),
- Text(stream, "codec_name"));
+ case "audio":
+ audios.Add(new AudioStreamInfo((int)Number(stream, "channels"), (int)Number(stream, "sample_rate"),
+ Text(stream, "codec_name")));
+ audio ??= audios[0];
break;
case "subtitle":
subtitles.Add(new SubtitleStreamInfo((int)Number(stream, "index"), Text(stream, "codec_name"),
@@ -143,7 +149,7 @@ internal sealed record MediaInfo(TimeSpan? Duration, VideoStreamInfo? Video, Aud
if (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, audios.Count > 1 ? audios : null);
}
/// <summary>The colour tags of a video stream; null when it has none ("unknown" counts as none).</summary>
diff --git a/tests/Filee.Engines.Tests/AudioTracksTests.cs b/tests/Filee.Engines.Tests/AudioTracksTests.cs
new file mode 100644
index 00000000..1d083802
--- /dev/null
+++ b/tests/Filee.Engines.Tests/AudioTracksTests.cs
@@ -0,0 +1,90 @@
+// Every audio track of a video is kept when it is converted (issue #396): a film with three languages keeps all three,
+// each copied where the target takes its codec or encoded on its own, with its language tag.
+
+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 AudioTracksTests(EngineFixture fx) : IClassFixture<EngineFixture>
+{
+ private static readonly (string Ffmpeg, string Ffprobe)? Tools = FfmpegConverter.Locate();
+
+ private static readonly VideoStreamInfo Hd = new(1920, 1080, 1, 0, 23.976, "h264", "yuv420p");
+
+ private static List<string> Plan(string target, MediaInfo info, MediaOptions? options = null) =>
+ [.. MediaEncoding.Plan("in.mkv", "out." + target, target, options ?? new MediaOptions(), info, "work").Single().Arguments];
+
+ private static string After(List<string> args, string option) => args[args.IndexOf(option) + 1];
+
+ [Fact]
+ public void Every_track_is_mapped_and_each_gets_its_own_options()
+ {
+ var info = new MediaInfo(TimeSpan.FromMinutes(24), Hd, new AudioStreamInfo(2, 48000, "aac"), null,
+ AudioStreams: [new(2, 48000, "aac"), new(6, 48000, "flac"), new(2, 48000, "opus")]);
+
+ var args = Plan("mp4", info, new MediaOptions { MaxHeight = 720 });
+
+ Assert.Equal(["0:V:0", "0:a:0", "0:a:1", "0:a:2"], args.Where((a, i) => i > 0 && args[i - 1] == "-map"));
+ Assert.Equal("copy", After(args, "-c:a:0")); // AAC goes into MP4 as it is
+ Assert.Equal("aac", After(args, "-c:a:1")); // FLAC doesn't: encoded, at a bitrate for 5.1
+ Assert.Equal("576k", After(args, "-b:a:1"));
+ Assert.Equal("aac", After(args, "-c:a:2"));
+ Assert.Equal("192k", After(args, "-b:a:2"));
+ Assert.DoesNotContain("-c:a", args); // nothing left unaddressed
+ }
+
+ [Fact]
+ public void One_track_keeps_the_plain_options_and_single_track_containers_take_the_first()
+ {
+ var one = new MediaInfo(TimeSpan.FromMinutes(1), Hd, new AudioStreamInfo(2, 48000, "opus"), null);
+ Assert.Equal("aac", After(Plan("mp4", one), "-c:a"));
+
+ var three = one with { AudioStreams = [new(2, 48000, "opus"), new(2, 48000, "opus"), new(2, 48000, "opus")] };
+ var flv = Plan("flv", three);
+ Assert.Equal(["0:V:0", "0:a:0"], flv.Where((a, i) => i > 0 && flv[i - 1] == "-map"));
+ Assert.Contains("-an", Plan("mp4", three, new MediaOptions { RemoveAudio = true }));
+ }
+
+ [Fact]
+ public void Option_values_are_never_renamed()
+ {
+ Assert.Equal(["-c:a:1", "wmav2", "-b:a:1", "128k", "-ac:a:1", "2"],
+ MediaEncoding.ForAudioTrack(["-c:a", "wmav2", "-b:a", "128k", "-ac", "2"], 1));
+ }
+
+ [Fact]
+ public async Task A_three_language_mkv_keeps_its_languages_as_a_720p_mp4()
+ {
+ Assert.SkipUnless(Tools is not null, "FFmpeg is not installed (pwsh build/fetch-engines.ps1 -Only ffmpeg)");
+ var folder = fx.NewFolder();
+ var film = Path.Combine(folder, "episode.mkv");
+ var made = await ProcessRunner.RunAsync(Tools!.Value.Ffmpeg,
+ [
+ "-hide_banner", "-nostdin", "-v", "error", "-y",
+ "-f", "lavfi", "-i", "testsrc2=size=1920x1080:rate=24:duration=2",
+ "-f", "lavfi", "-i", "sine=frequency=330:duration=2",
+ "-f", "lavfi", "-i", "sine=frequency=440:duration=2",
+ "-f", "lavfi", "-i", "sine=frequency=550:duration=2",
+ "-map", "0", "-map", "1", "-map", "2", "-map", "3",
+ "-c:v", "libx264", "-c:a:0", "aac", "-c:a:1", "libopus", "-c:a:2", "flac",
+ "-metadata:s:a:0", "language=jpn", "-metadata:s:a:1", "language=eng", "-metadata:s:a:2", "language=kor", film,
+ ], TimeSpan.FromMinutes(2), CancellationToken.None);
+ Assert.True(made.ExitCode == 0, made.StandardError);
+
+ var job = await fx.ConvertAsync([film], BuiltInData.CreatePresets().Single(p => p.Id == "mp4-720"));
+
+ Assert.True(job.State == JobState.Completed, string.Join("; ", job.Files.Select(f => $"{f.ErrorKey} {f.ErrorDetail}")));
+ var probe = await ProcessRunner.RunAsync(Tools.Value.Ffprobe,
+ ["-v", "error", "-select_streams", "a", "-show_entries", "stream=codec_name:stream_tags=language", "-of", "json", job.Outputs.Single()],
+ TimeSpan.FromMinutes(1), CancellationToken.None);
+ using var json = JsonDocument.Parse(probe.StandardOutput);
+ var tracks = json.RootElement.GetProperty("streams").EnumerateArray()
+ .Select(s => (s.GetProperty("codec_name").GetString(), s.GetProperty("tags").GetProperty("language").GetString())).ToList();
+ Assert.Equal([("aac", "jpn"), ("aac", "eng"), ("aac", "kor")], tracks);
+ }
+}