Skip to content

Commit

Permalink
Add support for custom chapter language
Browse files Browse the repository at this point in the history
use filename like: 00001.LANG.txt where LANG is the desired argument
to `mkvmerge --chapter-language` for this chapter.
A bare name like 00001.txt will continue to use the default chapter
language (en).

Signed-off-by: akarin <[email protected]>
  • Loading branch information
AkarinVS committed Jan 20, 2023
1 parent 90ea9f8 commit 130543c
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 13 deletions.
20 changes: 14 additions & 6 deletions OKEGui/OKEGui/JobProcessor/Muxer/AutoMuxer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ private struct Episode
public List<string> AudioNames;

public string ChapterFile;
public string ChapterLanguage;

public List<string> SubtitleFiles;
public List<string> SubtitleLanguages;
Expand Down Expand Up @@ -74,7 +75,8 @@ private Episode GenerateEpisode(
List<string> audioLanguages/* = "jpn"*/,
List<string> audioNames/* = ""*/,
List<string> subtitleLanguages/* = "jpn"*/,
List<string> subtitleNames/* = ""*/
List<string> subtitleNames/* = ""*/,
string chapterLanguage = null
)
{
var episode = new Episode
Expand All @@ -88,7 +90,8 @@ private Episode GenerateEpisode(
AudioNames = audioNames,
SubtitleLanguages = subtitleLanguages,
SubtitleNames = subtitleNames,
TotalFileSize = 0
TotalFileSize = 0,
ChapterLanguage = chapterLanguage
};

foreach (string file in inputFileNames)
Expand Down Expand Up @@ -182,7 +185,11 @@ private string GenerateMkvMergeParameter(Episode episode)
}
}

if (episode.ChapterFile != null) parameters.Add($"--chapters \"{episode.ChapterFile}\"");
if (episode.ChapterFile != null)
{
if (!string.IsNullOrEmpty(episode.ChapterLanguage)) parameters.Add($"--chapter-language \"{episode.ChapterLanguage}\"");
parameters.Add($"--chapters \"{episode.ChapterFile}\"");
}

parameters.Add(string.Format("--track-order {0}", string.Join(",", trackOrder)));

Expand Down Expand Up @@ -326,10 +333,11 @@ public void StartMerge(
List<string> audioLanguages,
List<string> audioNames,
List<string> subtitleLanguages,
List<string> subtitleNames
List<string> subtitleNames,
string chapterLanguage
)
{
_episode = GenerateEpisode(inputFileNames, outputFileName, videoFps, videoName, timeCodeFile, audioLanguages, audioNames, subtitleLanguages, subtitleNames);
_episode = GenerateEpisode(inputFileNames, outputFileName, videoFps, videoName, timeCodeFile, audioLanguages, audioNames, subtitleLanguages, subtitleNames, chapterLanguage);
string mainProgram = string.Empty;
string args = string.Empty;
switch (_episode.OutputFileType)
Expand Down Expand Up @@ -388,7 +396,7 @@ public OKEFile StartMuxing(string path, MediaFile mediaFile)
input.Add(track.File.GetFullPath());
}

this.StartMerge(input, path, videoFps, videoName, timeCodeFile, audioLanguages, audioNames, subtitleLanguages, subtitleNames);
this.StartMerge(input, path, videoFps, videoName, timeCodeFile, audioLanguages, audioNames, subtitleLanguages, subtitleNames, mediaFile.ChapterLanguage);

OKEFile outFile = new OKEFile(path);
outFile.AddCRC32();
Expand Down
1 change: 1 addition & 0 deletions OKEGui/OKEGui/Model/MediaFile.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ public List<Track> Tracks
public List<SubtitleTrack> SubtitleTracks = new List<SubtitleTrack>();

public ChapterTrack ChapterTrack = null;
public string ChapterLanguage = null;

/// <summary>
/// 插入多媒体轨道
Expand Down
34 changes: 28 additions & 6 deletions OKEGui/OKEGui/Task/ChapterService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
Expand Down Expand Up @@ -32,8 +33,7 @@ public static ChapterStatus UpdateChapterStatus(TaskDetail task)

private static bool HasChapterFile(TaskDetail task)
{
FileInfo txtChapter = new FileInfo(Path.ChangeExtension(task.InputFile, ".txt"));
return txtChapter.Exists;
return FindChapterFile(task);
}

private static bool HasBlurayStructure(TaskDetail task)
Expand Down Expand Up @@ -86,16 +86,38 @@ private static bool HasMatroskaChapter(TaskDetail task)
return true;
}

public static bool FindChapterFile(TaskDetail task)
{
if (!string.IsNullOrEmpty(task.ChapterFileName))
return true;
FileInfo inputFile = new FileInfo(task.InputFile);
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 > 1)
throw new Exception("More than one chapter files found for " + task.InputFile + ": " + String.Join(",", files));
if (files.Length == 1)
{
task.ChapterFileName = files[0];
string ext = Path.GetFileNameWithoutExtension(task.ChapterFileName);
if (ext.Length > basename.Length)
task.ChapterLanguage = ext.Substring(basename.Length + 1);
Logger.Warn($"ChapterFile {task.ChapterFileName}, language \"{task.ChapterLanguage}\".");
return true;
}
return false;
}

public static ChapterInfo LoadChapter(TaskDetail task)
{
FileInfo inputFile = new FileInfo(task.InputFile);
ChapterInfo chapterInfo;
switch (task.ChapterStatus)
{
case ChapterStatus.Yes:
FileInfo txtChapter = new FileInfo(Path.ChangeExtension(inputFile.FullName, ".txt"));
if (!txtChapter.Exists) return null;
chapterInfo = new OGMParser().Parse(txtChapter.FullName).FirstOrDefault();
if (!FindChapterFile(task)) return null;
chapterInfo = new OGMParser().Parse(task.ChapterFileName).FirstOrDefault();
break;
case ChapterStatus.Maybe:
DirectoryInfo playlistDirectory =
Expand Down
3 changes: 3 additions & 0 deletions OKEGui/OKEGui/Task/TaskDetail.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ public class TaskDetail : TaskStatus
// Task所分解成的Job队列。
public Queue<Job> JobQueue = new Queue<Job>();

public string ChapterFileName;
public string ChapterLanguage;

// 输出文件轨道。MediaOutFile是主文件(mp4/mkv), MkaOutFile是外挂mka
public MediaFile MediaOutFile;
public MediaFile MkaOutFile;
Expand Down
3 changes: 2 additions & 1 deletion OKEGui/OKEGui/Worker/ExecuteTaskService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
}

FileInfo inputChapterFile =
new FileInfo(Path.ChangeExtension(task.InputFile, ".txt"));
new FileInfo(task.ChapterFileName);
FileInfo outputChapterFile =
new FileInfo(Path.ChangeExtension(task.Taskfile.WorkingPathPrefix, ".txt"));
if (inputChapterFile.Exists && !File.Exists(outputChapterFile.FullName))
Expand All @@ -293,6 +293,7 @@ private void WorkerDoWork(object sender, DoWorkEventArgs e)
outputChapterFile.Refresh();
OKEFile chapterFile = new OKEFile(outputChapterFile);
task.MediaOutFile.AddTrack(new ChapterTrack(chapterFile));
task.MediaOutFile.ChapterLanguage = task.ChapterLanguage;

// 用章节文件生成qpfile
string qpFileName = Path.ChangeExtension(task.Taskfile.WorkingPathPrefix, ".qpf");
Expand Down

0 comments on commit 130543c

Please sign in to comment.