mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-03-17 15:46:22 +00:00
Backport pull request #8087 from jellyfin/release-10.8.z
feat: make subtitleeditparser generic
Authored-by: Claus Vium <cvium@users.noreply.github.com>
Merged-by: Bond-009 <bond.009@outlook.com>
Original-merge: 7323ccfc23
This commit is contained in:
@@ -35,6 +35,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
private readonly IMediaEncoder _mediaEncoder;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IMediaSourceManager _mediaSourceManager;
|
||||
private readonly ISubtitleParser _subtitleParser;
|
||||
|
||||
/// <summary>
|
||||
/// The _semaphoreLocks.
|
||||
@@ -48,7 +49,8 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
IFileSystem fileSystem,
|
||||
IMediaEncoder mediaEncoder,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IMediaSourceManager mediaSourceManager)
|
||||
IMediaSourceManager mediaSourceManager,
|
||||
ISubtitleParser subtitleParser)
|
||||
{
|
||||
_logger = logger;
|
||||
_appPaths = appPaths;
|
||||
@@ -56,6 +58,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
_mediaEncoder = mediaEncoder;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_mediaSourceManager = mediaSourceManager;
|
||||
_subtitleParser = subtitleParser;
|
||||
}
|
||||
|
||||
private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles");
|
||||
@@ -73,8 +76,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
|
||||
try
|
||||
{
|
||||
var reader = GetReader(inputFormat);
|
||||
var trackInfo = reader.Parse(stream, cancellationToken);
|
||||
var trackInfo = _subtitleParser.Parse(stream, inputFormat);
|
||||
|
||||
FilterEvents(trackInfo, startTimeTicks, endTimeTicks, preserveOriginalTimestamps);
|
||||
|
||||
@@ -233,7 +235,8 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
var currentFormat = (Path.GetExtension(subtitleStream.Path) ?? subtitleStream.Codec)
|
||||
.TrimStart('.');
|
||||
|
||||
if (!TryGetReader(currentFormat, out _))
|
||||
// Fallback to ffmpeg conversion
|
||||
if (!_subtitleParser.SupportsFileExtension(currentFormat))
|
||||
{
|
||||
// Convert
|
||||
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, ".srt");
|
||||
@@ -243,44 +246,10 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
return new SubtitleInfo(outputPath, MediaProtocol.File, "srt", true);
|
||||
}
|
||||
|
||||
// It's possbile that the subtitleStream and mediaSource don't share the same protocol (e.g. .STRM file with local subs)
|
||||
// It's possible that the subtitleStream and mediaSource don't share the same protocol (e.g. .STRM file with local subs)
|
||||
return new SubtitleInfo(subtitleStream.Path, _mediaSourceManager.GetPathProtocol(subtitleStream.Path), currentFormat, true);
|
||||
}
|
||||
|
||||
private bool TryGetReader(string format, [NotNullWhen(true)] out ISubtitleParser? value)
|
||||
{
|
||||
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = new SrtParser(_logger);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = new SsaParser(_logger);
|
||||
return true;
|
||||
}
|
||||
|
||||
if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
|
||||
{
|
||||
value = new AssParser(_logger);
|
||||
return true;
|
||||
}
|
||||
|
||||
value = null;
|
||||
return false;
|
||||
}
|
||||
|
||||
private ISubtitleParser GetReader(string format)
|
||||
{
|
||||
if (TryGetReader(format, out var reader))
|
||||
{
|
||||
return reader;
|
||||
}
|
||||
|
||||
throw new ArgumentException("Unsupported format: " + format);
|
||||
}
|
||||
|
||||
private bool TryGetWriter(string format, [NotNullWhen(true)] out ISubtitleWriter? value)
|
||||
{
|
||||
if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
|
||||
|
||||
Reference in New Issue
Block a user