mirror of
https://github.com/jellyfin/jellyfin.git
synced 2025-08-05 13:47:05 +02:00
Merge pull request #12718 from IceStormNG/extract-mks-subtitles
Extract subtitles of MKS aux files if they're extractable.
This commit is contained in:
commit
5cae44fdf7
@ -489,10 +489,15 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
try
|
try
|
||||||
{
|
{
|
||||||
var subtitleStreams = mediaSource.MediaStreams
|
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)
|
foreach (var subtitleStream in subtitleStreams)
|
||||||
{
|
{
|
||||||
|
if (subtitleStream.IsExternal && !subtitleStream.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitleFileExtension(subtitleStream));
|
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitleFileExtension(subtitleStream));
|
||||||
|
|
||||||
var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false);
|
var releaser = await _semaphoreLocks.LockAsync(outputPath, cancellationToken).ConfigureAwait(false);
|
||||||
@ -510,6 +515,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
if (extractableStreams.Count > 0)
|
if (extractableStreams.Count > 0)
|
||||||
{
|
{
|
||||||
await ExtractAllExtractableSubtitlesInternal(mediaSource, extractableStreams, cancellationToken).ConfigureAwait(false);
|
await ExtractAllExtractableSubtitlesInternal(mediaSource, extractableStreams, cancellationToken).ConfigureAwait(false);
|
||||||
|
await ExtractAllExtractableSubtitlesMKS(mediaSource, extractableStreams, cancellationToken).ConfigureAwait(false);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
catch (Exception ex)
|
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 (string.IsNullOrEmpty(subtitleStream.Path) || !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(
|
private async Task ExtractAllExtractableSubtitlesInternal(
|
||||||
MediaSourceInfo mediaSource,
|
MediaSourceInfo mediaSource,
|
||||||
List<MediaStream> subtitleStreams,
|
List<MediaStream> subtitleStreams,
|
||||||
@ -536,6 +608,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
|
|
||||||
foreach (var subtitleStream in subtitleStreams)
|
foreach (var subtitleStream in subtitleStreams)
|
||||||
{
|
{
|
||||||
|
if (!string.IsNullOrEmpty(subtitleStream.Path) && subtitleStream.Path.EndsWith(".mks", StringComparison.OrdinalIgnoreCase))
|
||||||
|
{
|
||||||
|
_logger.LogDebug("Subtitle {Index} for file {InputPath} is part in an MKS file. Skipping", inputPath, subtitleStream.Index);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitleFileExtension(subtitleStream));
|
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, "." + GetExtractableSubtitleFileExtension(subtitleStream));
|
||||||
var outputCodec = IsCodecCopyable(subtitleStream.Codec) ? "copy" : "srt";
|
var outputCodec = IsCodecCopyable(subtitleStream.Codec) ? "copy" : "srt";
|
||||||
var streamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream);
|
var streamIndex = EncodingHelper.FindIndex(mediaSource.MediaStreams, subtitleStream);
|
||||||
@ -557,6 +635,20 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
outputPath);
|
outputPath);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (outputPaths.Count == 0)
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
await ExtractSubtitlesForFile(inputPath, args, outputPaths, cancellationToken).ConfigureAwait(false);
|
||||||
|
}
|
||||||
|
|
||||||
|
private async Task ExtractSubtitlesForFile(
|
||||||
|
string inputPath,
|
||||||
|
string args,
|
||||||
|
List<string> outputPaths,
|
||||||
|
CancellationToken cancellationToken)
|
||||||
|
{
|
||||||
int exitCode;
|
int exitCode;
|
||||||
|
|
||||||
using (var process = new Process
|
using (var process = new Process
|
||||||
|
Loading…
Reference in New Issue
Block a user