Skip to content

Commit

Permalink
Support track reordering and set default flags properly
Browse files Browse the repository at this point in the history
Now, for each AudioTrack/SubtitleTrack you can additionally set
an int Order field (default is the maximum allowed int). The muxer
will do a *stable* sort to lay out the tracks by ascending Order.

We will only ever set default flag for the first audio track in
mkv (not mka) files.

Signed-off-by: akarin <[email protected]>
  • Loading branch information
AkarinVS committed Jan 24, 2023
1 parent cae379f commit 6547ac2
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 6 deletions.
12 changes: 7 additions & 5 deletions OKEGui/OKEGui/JobProcessor/Muxer/AutoMuxer.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.IO;
using System.Text.RegularExpressions;
using System.Threading;
Expand Down Expand Up @@ -148,7 +149,7 @@ private Episode GenerateEpisode(

private string GenerateMkvMergeParameter(Episode episode)
{
string trackTemplate = "--language 0:{0} --track-name \"0:{1}\" \"(\" \"{2}\" \")\"";
string trackTemplate = "--default-track \"0:{0}\" --language 0:{1} --track-name \"0:{2}\" \"(\" \"{3}\" \")\"";

var parameters = new List<string>();
var trackOrder = new List<string>();
Expand All @@ -164,14 +165,15 @@ private string GenerateMkvMergeParameter(Episode episode)
{
parameters.Add($"--timestamps \"0:{episode.timeCodeFile}\"");
}
parameters.Add(string.Format(trackTemplate, "und", episode.VideoName, episode.VideoFile));
parameters.Add(string.Format(trackTemplate, "1", "und", episode.VideoName, episode.VideoFile));
trackOrder.Add($"{fileID++}:0");
}

for (int i = 0; i < episode.AudioFiles.Count; i++)
{
string audioFile = episode.AudioFiles[i];
parameters.Add(string.Format(trackTemplate, episode.AudioLanguages[i], episode.AudioNames[i], audioFile));
bool isDefault = episode.VideoFile != null & i == 0;
parameters.Add(string.Format(trackTemplate, isDefault ? "1":"0", episode.AudioLanguages[i], episode.AudioNames[i], audioFile));
trackOrder.Add($"{fileID++}:0");
}

Expand All @@ -180,7 +182,7 @@ private string GenerateMkvMergeParameter(Episode episode)
for (int i = 0; i < episode.SubtitleFiles.Count; i++)
{
string subtitleFile = episode.SubtitleFiles[i];
parameters.Add(string.Format(trackTemplate, episode.SubtitleLanguages[i], episode.SubtitleNames[i], subtitleFile));
parameters.Add(string.Format(trackTemplate, "0", episode.SubtitleLanguages[i], episode.SubtitleNames[i], subtitleFile));
trackOrder.Add($"{fileID++}:0");
}
}
Expand Down Expand Up @@ -366,7 +368,7 @@ public OKEFile StartMuxing(string path, MediaFile mediaFile)
List<string> subtitleLanguages = new List<string>();
List<string> subtitleNames = new List<string>();

foreach (var track in mediaFile.Tracks)
foreach (var track in mediaFile.Tracks.OrderBy(trk => trk.Info.Order))
{
if (track.Info.MuxOption != MuxOption.Default && track.Info.MuxOption != MuxOption.Mka)
{
Expand Down
1 change: 1 addition & 0 deletions OKEGui/OKEGui/Model/Info/Info.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class Info : ICloneable
public string Language = Constants.language;
public string Name = "";
public bool Optional = false;
public int Order = Int32.MaxValue;
private bool _dupOrEmpty;
public bool DupOrEmpty
{
Expand Down
3 changes: 2 additions & 1 deletion OKEGui/OKEGui/Task/ChapterService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public static bool FindChapterFile(TaskDetail task)
string inputPath = Path.GetFullPath(inputFile.FullName);
string basename = Path.GetFileNameWithoutExtension(inputFile.FullName);
string[] files = Directory.GetFiles(Path.GetDirectoryName(inputPath), basename + ".*txt");
Logger.Warn($"ChapterFile: found {String.Join(",", files)}.");
if (files.Length > 0)
Logger.Warn($"ChapterFile: found {String.Join(",", files)}.");
if (files.Length > 1)
throw new Exception("More than one chapter files found for " + task.InputFile + ": " + String.Join(",", files));
if (files.Length == 1)
Expand Down

0 comments on commit 6547ac2

Please sign in to comment.