aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCarsten Braun <carsten.braun@braun-cloud.de>2024-09-25 18:51:28 +0200
committerCarsten Braun <carsten.braun@braun-cloud.de>2025-06-07 21:51:08 +0200
commitb2e19c0306fb42616736b18777641e94edfc9d85 (patch)
tree10b3065631ca164ef69f66942041342af571f6f1
parent6d287d56278bfc96c77a46adbfcad81095074930 (diff)
Also extract subtitles of MKS aux files if they're extractable.
-rw-r--r--MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs85
1 files changed, 83 insertions, 2 deletions
diff --git a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs
index 777e33587..a9b41898b 100644
--- a/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs
+++ b/MediaBrowser.MediaEncoding/Subtitles/SubtitleEncoder.cs
@@ -489,10 +489,15 @@ namespace MediaBrowser.MediaEncoding.Subtitles
try
{
var subtitleStreams = mediaSource.MediaStreams
- .Where(stream => stream is { IsExtractableSubtitleStream: true, SupportsExternalStream: true, IsExternal: false });
+ .Where(stream => stream is { IsExtractableSubtitleStream: true, SupportsExternalStream: true });
foreach (var subtitleStream in subtitleStreams)
{
+ if (subtitleStream.IsExternal && !subtitleStream.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase))
+ {
+ continue;
+ }
+
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitleFileExtension(subtitleStream));
var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false);
@@ -510,6 +515,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
if (extractableStreams.Count > 0)
{
await ExtractAllExtractableSubtitlesInternal(mediaSource, extractableStreams, cancellationToken).ConfigureAwait(false);
+ await ExtractAllExtractableSubtitlesMKS(mediaSource, extractableStreams, cancellationToken).ConfigureAwait(false);
}
}
catch (Exception ex)
@@ -522,6 +528,72 @@ namespace MediaBrowser.MediaEncoding.Subtitles
}
}
+ private async Task ExtractAllExtractableSubtitlesMKS(
+ MediaSourceInfo mediaSource,
+ List<MediaStream> subtitleStreams,
+ CancellationToken cancellationToken)
+ {
+ var mksFiles = new List<string>();
+
+ foreach (var subtitleStream in subtitleStreams)
+ {
+ if (!subtitleStream.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase))
+ {
+ continue;
+ }
+
+ if (!mksFiles.Contains(subtitleStream.Path))
+ {
+ mksFiles.Add(subtitleStream.Path);
+ }
+ }
+
+ if (mksFiles.Count == 0)
+ {
+ return;
+ }
+
+ foreach (string mksFile in mksFiles)
+ {
+ var inputPath = _mediaEncoder.GetInputArgument(mksFile, mediaSource);
+ var outputPaths = new List<string>();
+ var args = string.Format(
+ CultureInfo.InvariantCulture,
+ "-i {0} -copyts",
+ inputPath);
+
+ foreach (var subtitleStream in subtitleStreams)
+ {
+ if (!subtitleStream.Path.Equals(mksFile, StringComparison.OrdinalIgnoreCase))
+ {
+ continue;
+ }
+
+ var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitleFileExtension(subtitleStream));
+ var outputCodec = IsCodecCopyable(subtitleStream.Codec) ? "copy" : "srt";
+ var streamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream);
+
+ if (streamIndex == -1)
+ {
+ _logger.LogError("Cannot find subtitle stream index for {InputPath} ({Index}), skipping this stream", inputPath, subtitleStream.Index);
+ continue;
+ }
+
+ Directory.CreateDirectory(Path.GetDirectoryName(outputPath) ?? throw new FileNotFoundException($"Calculated path ({outputPath}) is not valid."));
+
+ outputPaths.Add(outputPath);
+ args += string.Format(
+ CultureInfo.InvariantCulture,
+ " -map 0:{0} -an -vn -c:s {1} \"{2}\"",
+ streamIndex,
+ outputCodec,
+ outputPath);
+ }
+
+ await ExtractSubtitlesForFile(inputPath, args, outputPaths, cancellationToken).ConfigureAwait(false);
+ }
+ }
+
private async Task ExtractAllExtractableSubtitlesInternal(
MediaSourceInfo mediaSource,
List<MediaStream> subtitleStreams,
@@ -540,7 +612,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
var outputCodec = IsCodecCopyable(subtitleStream.Codec) ? "copy" : "srt";
var streamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream);
- if (streamIndex == -1)
+ if (streamIndex == -1 || subtitleStream.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase))
{
_logger.LogError("Cannot find subtitle stream index for {InputPath} ({Index}), skipping this stream", inputPath, subtitleStream.Index);
continue;
@@ -557,6 +629,15 @@ namespace MediaBrowser.MediaEncoding.Subtitles
outputPath);
}
+ await ExtractSubtitlesForFile(inputPath, args, outputPaths, cancellationToken).ConfigureAwait(false);
+ }
+
+ private async Task ExtractSubtitlesForFile(
+ string inputPath,
+ string args,
+ List<string> outputPaths,
+ CancellationToken cancellationToken)
+ {
int exitCode;
using (var process = new Process