mirror of
https://github.com/jellyfin/jellyfin.git
synced 2026-04-29 21:36:31 +01: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:
@@ -1,12 +1,14 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Globalization;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Reflection;
|
||||
using Jellyfin.Extensions;
|
||||
using MediaBrowser.Model.MediaInfo;
|
||||
using Microsoft.Extensions.Logging;
|
||||
using Nikse.SubtitleEdit.Core.Common;
|
||||
using ILogger = Microsoft.Extensions.Logging.ILogger;
|
||||
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||
using SubtitleFormat = Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat;
|
||||
|
||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
@@ -14,31 +16,57 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||
/// <summary>
|
||||
/// SubStation Alpha subtitle parser.
|
||||
/// </summary>
|
||||
/// <typeparam name="T">The <see cref="SubtitleFormat" />.</typeparam>
|
||||
public abstract class SubtitleEditParser<T> : ISubtitleParser
|
||||
where T : SubtitleFormat, new()
|
||||
public class SubtitleEditParser : ISubtitleParser
|
||||
{
|
||||
private readonly ILogger _logger;
|
||||
private readonly ILogger<SubtitleEditParser> _logger;
|
||||
private readonly Dictionary<string, SubtitleFormat[]> _subtitleFormats;
|
||||
|
||||
/// <summary>
|
||||
/// Initializes a new instance of the <see cref="SubtitleEditParser{T}"/> class.
|
||||
/// Initializes a new instance of the <see cref="SubtitleEditParser"/> class.
|
||||
/// </summary>
|
||||
/// <param name="logger">The logger.</param>
|
||||
protected SubtitleEditParser(ILogger logger)
|
||||
public SubtitleEditParser(ILogger<SubtitleEditParser> 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, CancellationToken cancellationToken)
|
||||
public SubtitleTrackInfo Parse(Stream stream, string fileExtension)
|
||||
{
|
||||
var subtitle = new Subtitle();
|
||||
var subRip = new T();
|
||||
var lines = stream.ReadAllLines().ToList();
|
||||
subRip.LoadSubtitle(subtitle, lines, "untitled");
|
||||
if (subRip.ErrorCount > 0)
|
||||
|
||||
if (!_subtitleFormats.TryGetValue(fileExtension, out var subtitleFormats))
|
||||
{
|
||||
_logger.LogError("{ErrorCount} errors encountered while parsing subtitle", subRip.ErrorCount);
|
||||
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);
|
||||
}
|
||||
|
||||
var trackInfo = new SubtitleTrackInfo();
|
||||
@@ -57,5 +85,36 @@ 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user