mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-06 11:34:57 +02:00
Revert "Merge pull request #8087 from cvium/generic_subtitleparser"
This PR was causing breakage in installs - ref #8198 This reverts commit7323ccfc23
, reversing changes made to77a007a24d
.
This commit is contained in:
parent
dd97e6bc45
commit
494ed7e4d2
10 changed files with 119 additions and 98 deletions
|
@ -83,7 +83,6 @@ using MediaBrowser.Controller.SyncPlay;
|
|||
using MediaBrowser.Controller.TV;
|
||||
using MediaBrowser.LocalMetadata.Savers;
|
||||
using MediaBrowser.MediaEncoding.BdInfo;
|
||||
using MediaBrowser.MediaEncoding.Subtitles;
|
||||
using MediaBrowser.Model.Cryptography;
|
||||
using MediaBrowser.Model.Dlna;
|
||||
using MediaBrowser.Model.Globalization;
|
||||
|
@ -635,8 +634,7 @@ namespace Emby.Server.Implementations
|
|||
serviceCollection.AddSingleton<IAuthService, AuthService>();
|
||||
serviceCollection.AddSingleton<IQuickConnect, QuickConnectManager>();
|
||||
|
||||
serviceCollection.AddSingleton<ISubtitleParser, SubtitleEditParser>();
|
||||
serviceCollection.AddSingleton<ISubtitleEncoder, SubtitleEncoder>();
|
||||
serviceCollection.AddSingleton<ISubtitleEncoder, MediaBrowser.MediaEncoding.Subtitles.SubtitleEncoder>();
|
||||
|
||||
serviceCollection.AddSingleton<IAttachmentExtractor, MediaBrowser.MediaEncoding.Attachments.AttachmentExtractor>();
|
||||
|
||||
|
|
19
MediaBrowser.MediaEncoding/Subtitles/AssParser.cs
Normal file
19
MediaBrowser.MediaEncoding/Subtitles/AssParser.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
/// <summary>
|
||||
/// Advanced SubStation Alpha subtitle parser.
|
||||
/// </summary>
|
||||
public class AssParser : SubtitleEditParser<AdvancedSubStationAlpha>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="AssParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public AssParser(ILogger logger) : base(logger)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,6 +1,7 @@
|
|||
#pragma warning disable CS1591
|
||||
|
||||
using System.IO;
|
||||
using System.Threading;
|
||||
using MediaBrowser.Model.MediaInfo;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
|
@ -11,15 +12,8 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
/// Parses the specified stream.
|
||||
/// </summary>
|
||||
/// <param name="stream">The stream.</param>
|
||||
/// <param name="fileExtension">The file extension.</param>
|
||||
/// <param name="cancellationToken">The cancellation token.</param>
|
||||
/// <returns>SubtitleTrackInfo.</returns>
|
||||
SubtitleTrackInfo Parse(Stream stream, string fileExtension);
|
||||
|
||||
/// <summary>
|
||||
/// Determines whether the file extension is supported by the parser.
|
||||
/// </summary>
|
||||
/// <param name="fileExtension">The file extension.</param>
|
||||
/// <returns>A value indicating whether the file extension is supported.</returns>
|
||||
bool SupportsFileExtension(string fileExtension);
|
||||
SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken);
|
||||
}
|
||||
}
|
||||
|
|
19
MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs
Normal file
19
MediaBrowser.MediaEncoding/Subtitles/SrtParser.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
/// <summary>
|
||||
/// SubRip subtitle parser.
|
||||
/// </summary>
|
||||
public class SrtParser : SubtitleEditParser<SubRip>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SrtParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public SrtParser(ILogger logger) : base(logger)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
19
MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs
Normal file
19
MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs
Normal file
|
@ -0,0 +1,19 @@
|
|||
using Microsoft.Extensions.Logging;
|
||||
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
{
|
||||
/// <summary>
|
||||
/// SubStation Alpha subtitle parser.
|
||||
/// </summary>
|
||||
public class SsaParser : SubtitleEditParser<SubStationAlpha>
|
||||
{
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SsaParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public SsaParser(ILogger logger) : base(logger)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
|
@ -1,14 +1,12 @@
|
|||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using Jellyfin.Extensions;
|
||||
using MediaBrowser.Model.MediaInfo;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Nikse.SubtitleEdit.Core.Common;
|
||||
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
using SubtitleFormat = Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
|
@ -16,57 +14,31 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
/// <summary>
|
||||
/// SubStation Alpha subtitle parser.
|
||||
/// </summary>
|
||||
public class SubtitleEditParser : ISubtitleParser
|
||||
/// <typeparam name="T">The <see cref="SubtitleFormat" />.</typeparam>
|
||||
public abstract class SubtitleEditParser<T> : ISubtitleParser
|
||||
where T : SubtitleFormat, new()
|
||||
{
|
||||
private readonly ILogger<SubtitleEditParser> _logger;
|
||||
private readonly Dictionary<string, SubtitleFormat[]> _subtitleFormats;
|
||||
private readonly ILogger _logger;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SubtitleEditParser"/> class.
|
||||
/// Initializes a new instance of the <see cref="SubtitleEditParser{T}"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
public SubtitleEditParser(ILogger<SubtitleEditParser> logger)
|
||||
protected SubtitleEditParser(ILogger logger)
|
||||
{
|
||||
_logger = logger;
|
||||
_subtitleFormats = GetSubtitleFormats()
|
||||
.Where(subtitleFormat => !string.IsNullOrEmpty(subtitleFormat.Extension))
|
||||
.GroupBy(subtitleFormat => subtitleFormat.Extension.TrimStart('.'), StringComparer.OrdinalIgnoreCase)
|
||||
.ToDictionary(g => g.Key, g => g.ToArray(), StringComparer.OrdinalIgnoreCase);
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public SubtitleTrackInfo Parse(Stream stream, string fileExtension)
|
||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
||||
{
|
||||
var subtitle = new Subtitle();
|
||||
var subRip = new T();
|
||||
var lines = stream.ReadAllLines().ToList();
|
||||
|
||||
if (!_subtitleFormats.TryGetValue(fileExtension, out var subtitleFormats))
|
||||
subRip.LoadSubtitle(subtitle, lines, "untitled");
|
||||
if (subRip.ErrorCount > 0)
|
||||
{
|
||||
throw new ArgumentException($"Unsupported file extension: {fileExtension}", nameof(fileExtension));
|
||||
}
|
||||
|
||||
foreach (var subtitleFormat in subtitleFormats)
|
||||
{
|
||||
_logger.LogDebug(
|
||||
"Trying to parse '{FileExtension}' subtitle using the {SubtitleFormatParser} format parser",
|
||||
fileExtension,
|
||||
subtitleFormat.Name);
|
||||
subtitleFormat.LoadSubtitle(subtitle, lines, fileExtension);
|
||||
if (subtitleFormat.ErrorCount == 0)
|
||||
{
|
||||
break;
|
||||
}
|
||||
|
||||
_logger.LogError(
|
||||
"{ErrorCount} errors encountered while parsing '{FileExtension}' subtitle using the {SubtitleFormatParser} format parser",
|
||||
subtitleFormat.ErrorCount,
|
||||
fileExtension,
|
||||
subtitleFormat.Name);
|
||||
}
|
||||
|
||||
if (subtitle.Paragraphs.Count == 0)
|
||||
{
|
||||
throw new ArgumentException("Unsupported format: " + fileExtension);
|
||||
_logger.LogError("{ErrorCount} errors encountered while parsing subtitle", subRip.ErrorCount);
|
||||
}
|
||||
|
||||
var trackInfo = new SubtitleTrackInfo();
|
||||
|
@ -85,36 +57,5 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
trackInfo.TrackEvents = trackEvents;
|
||||
return trackInfo;
|
||||
}
|
||||
|
||||
/// <inheritdoc />
|
||||
public bool SupportsFileExtension(string fileExtension)
|
||||
=> _subtitleFormats.ContainsKey(fileExtension);
|
||||
|
||||
private IEnumerable<SubtitleFormat> GetSubtitleFormats()
|
||||
{
|
||||
var subtitleFormats = new List<SubtitleFormat>();
|
||||
var assembly = typeof(SubtitleFormat).Assembly;
|
||||
|
||||
foreach (var type in assembly.GetTypes())
|
||||
{
|
||||
if (!type.IsSubclassOf(typeof(SubtitleFormat)) || type.IsAbstract)
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
// It shouldn't be null, but the exception is caught if it is
|
||||
var subtitleFormat = (SubtitleFormat)Activator.CreateInstance(type, true)!;
|
||||
subtitleFormats.Add(subtitleFormat);
|
||||
}
|
||||
catch (Exception ex)
|
||||
{
|
||||
_logger.LogWarning(ex, "Failed to create instance of the subtitle format {SubtitleFormatType}", type.Name);
|
||||
}
|
||||
}
|
||||
|
||||
return subtitleFormats;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -35,7 +35,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
private readonly IMediaEncoder _mediaEncoder;
|
||||
private readonly IHttpClientFactory _httpClientFactory;
|
||||
private readonly IMediaSourceManager _mediaSourceManager;
|
||||
private readonly ISubtitleParser _subtitleParser;
|
||||
|
||||
/// <summary>
|
||||
/// The _semaphoreLocks.
|
||||
|
@ -49,8 +48,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
IFileSystem fileSystem,
|
||||
IMediaEncoder mediaEncoder,
|
||||
IHttpClientFactory httpClientFactory,
|
||||
IMediaSourceManager mediaSourceManager,
|
||||
ISubtitleParser subtitleParser)
|
||||
IMediaSourceManager mediaSourceManager)
|
||||
{
|
||||
_logger = logger;
|
||||
_appPaths = appPaths;
|
||||
|
@ -58,7 +56,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
_mediaEncoder = mediaEncoder;
|
||||
_httpClientFactory = httpClientFactory;
|
||||
_mediaSourceManager = mediaSourceManager;
|
||||
_subtitleParser = subtitleParser;
|
||||
}
|
||||
|
||||
private string SubtitleCachePath => Path.Combine(_appPaths.DataPath, "subtitles");
|
||||
|
@ -76,7 +73,8 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
|
||||
try
|
||||
{
|
||||
var trackInfo = _subtitleParser.Parse(stream, inputFormat);
|
||||
var reader = GetReader(inputFormat);
|
||||
var trackInfo = reader.Parse(stream, cancellationToken);
|
||||
|
||||
FilterEvents(trackInfo, startTimeTicks, endTimeTicks, preserveOriginalTimestamps);
|
||||
|
||||
|
@ -235,8 +233,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
var currentFormat = (Path.GetExtension(subtitleStream.Path) ?? subtitleStream.Codec)
|
||||
.TrimStart('.');
|
||||
|
||||
// Fallback to ffmpeg conversion
|
||||
if (!_subtitleParser.SupportsFileExtension(currentFormat))
|
||||
if (!TryGetReader(currentFormat, out _))
|
||||
{
|
||||
// Convert
|
||||
var outputPath = GetSubtitleCachePath(mediaSource, subtitleStream.Index, ".srt");
|
||||
|
@ -246,10 +243,44 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||
return new SubtitleInfo(outputPath, MediaProtocol.File, "srt", true);
|
||||
}
|
||||
|
||||
// It's possible that the subtitleStream and mediaSource don't share the same protocol (e.g. .STRM file with local subs)
|
||||
// It's possbile 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))
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
{
|
||||
using (var stream = File.OpenRead("Test Data/example.ass"))
|
||||
{
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "ass");
|
||||
var parsed = new AssParser(new NullLogger<AssParser>()).Parse(stream, CancellationToken.None);
|
||||
Assert.Single(parsed.TrackEvents);
|
||||
var trackEvent = parsed.TrackEvents[0];
|
||||
|
||||
|
|
|
@ -15,7 +15,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
{
|
||||
using (var stream = File.OpenRead("Test Data/example.srt"))
|
||||
{
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
|
||||
var parsed = new SrtParser(new NullLogger<SrtParser>()).Parse(stream, CancellationToken.None);
|
||||
Assert.Equal(2, parsed.TrackEvents.Count);
|
||||
|
||||
var trackEvent1 = parsed.TrackEvents[0];
|
||||
|
@ -37,7 +37,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
{
|
||||
using (var stream = File.OpenRead("Test Data/example2.srt"))
|
||||
{
|
||||
var parsed = new SubtitleEditParser(new NullLogger<SubtitleEditParser>()).Parse(stream, "srt");
|
||||
var parsed = new SrtParser(new NullLogger<SrtParser>()).Parse(stream, CancellationToken.None);
|
||||
Assert.Equal(2, parsed.TrackEvents.Count);
|
||||
|
||||
var trackEvent1 = parsed.TrackEvents[0];
|
||||
|
|
|
@ -13,7 +13,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
{
|
||||
public class SsaParserTests
|
||||
{
|
||||
private readonly SubtitleEditParser _parser = new SubtitleEditParser(new NullLogger<SubtitleEditParser>());
|
||||
private readonly SsaParser _parser = new SsaParser(new NullLogger<AssParser>());
|
||||
|
||||
[Theory]
|
||||
[MemberData(nameof(Parse_MultipleDialogues_TestData))]
|
||||
|
@ -21,7 +21,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
{
|
||||
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
|
||||
{
|
||||
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, "ssa");
|
||||
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, CancellationToken.None);
|
||||
|
||||
Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
|
||||
|
||||
|
@ -76,7 +76,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||
{
|
||||
using (var stream = File.OpenRead("Test Data/example.ssa"))
|
||||
{
|
||||
var parsed = _parser.Parse(stream, "ssa");
|
||||
var parsed = _parser.Parse(stream, CancellationToken.None);
|
||||
Assert.Single(parsed.TrackEvents);
|
||||
var trackEvent = parsed.TrackEvents[0];
|
||||
|
||||
|
|
Loading…
Reference in a new issue