2022-08-01 18:25:42 +00:00
|
|
|
using System;
|
|
|
|
using System.Collections.Generic;
|
2021-01-08 22:03:02 +00:00
|
|
|
using System.Globalization;
|
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
2021-06-19 16:02:33 +00:00
|
|
|
using Jellyfin.Extensions;
|
2021-01-08 22:03:02 +00:00
|
|
|
using MediaBrowser.Model.MediaInfo;
|
2021-01-08 22:21:50 +00:00
|
|
|
using Microsoft.Extensions.Logging;
|
2021-09-13 12:32:26 +00:00
|
|
|
using Nikse.SubtitleEdit.Core.Common;
|
2021-01-08 22:21:50 +00:00
|
|
|
using SubtitleFormat = Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat;
|
2021-01-08 22:03:02 +00:00
|
|
|
|
|
|
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// SubStation Alpha subtitle parser.
|
|
|
|
/// </summary>
|
2022-08-01 18:25:42 +00:00
|
|
|
public class SubtitleEditParser : ISubtitleParser
|
2021-01-08 22:03:02 +00:00
|
|
|
{
|
2022-08-01 18:25:42 +00:00
|
|
|
private readonly ILogger<SubtitleEditParser> _logger;
|
|
|
|
private readonly Dictionary<string, SubtitleFormat[]> _subtitleFormats;
|
2021-01-08 22:21:50 +00:00
|
|
|
|
|
|
|
/// <summary>
|
2022-08-01 18:25:42 +00:00
|
|
|
/// Initializes a new instance of the <see cref="SubtitleEditParser"/> class.
|
2021-01-08 22:21:50 +00:00
|
|
|
/// </summary>
|
|
|
|
/// <param name="logger">The logger.</param>
|
2022-08-01 18:25:42 +00:00
|
|
|
public SubtitleEditParser(ILogger<SubtitleEditParser> logger)
|
2021-01-08 22:21:50 +00:00
|
|
|
{
|
|
|
|
_logger = logger;
|
2022-08-01 18:25:42 +00:00
|
|
|
_subtitleFormats = GetSubtitleFormats()
|
|
|
|
.Where(subtitleFormat => !string.IsNullOrEmpty(subtitleFormat.Extension))
|
|
|
|
.GroupBy(subtitleFormat => subtitleFormat.Extension.TrimStart('.'), StringComparer.OrdinalIgnoreCase)
|
|
|
|
.ToDictionary(g => g.Key, g => g.ToArray(), StringComparer.OrdinalIgnoreCase);
|
2021-01-08 22:21:50 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 22:03:02 +00:00
|
|
|
/// <inheritdoc />
|
2022-08-01 18:25:42 +00:00
|
|
|
public SubtitleTrackInfo Parse(Stream stream, string fileExtension)
|
2021-01-08 22:03:02 +00:00
|
|
|
{
|
|
|
|
var subtitle = new Subtitle();
|
|
|
|
var lines = stream.ReadAllLines().ToList();
|
2022-08-01 18:25:42 +00:00
|
|
|
|
|
|
|
if (!_subtitleFormats.TryGetValue(fileExtension, out var subtitleFormats))
|
|
|
|
{
|
|
|
|
throw new ArgumentException($"Unsupported file extension: {fileExtension}", nameof(fileExtension));
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach (var subtitleFormat in subtitleFormats)
|
2021-01-08 22:21:50 +00:00
|
|
|
{
|
2022-08-01 18:25:42 +00:00
|
|
|
_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);
|
2021-01-08 22:21:50 +00:00
|
|
|
}
|
2021-01-08 22:03:02 +00:00
|
|
|
|
|
|
|
var trackInfo = new SubtitleTrackInfo();
|
|
|
|
int len = subtitle.Paragraphs.Count;
|
|
|
|
var trackEvents = new SubtitleTrackEvent[len];
|
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
{
|
|
|
|
var p = subtitle.Paragraphs[i];
|
|
|
|
trackEvents[i] = new SubtitleTrackEvent(p.Number.ToString(CultureInfo.InvariantCulture), p.Text)
|
|
|
|
{
|
|
|
|
StartPositionTicks = p.StartTime.TimeSpan.Ticks,
|
|
|
|
EndPositionTicks = p.EndTime.TimeSpan.Ticks
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
trackInfo.TrackEvents = trackEvents;
|
|
|
|
return trackInfo;
|
|
|
|
}
|
2022-08-01 18:25:42 +00:00
|
|
|
|
|
|
|
/// <inheritdoc />
|
|
|
|
public bool SupportsFileExtension(string fileExtension)
|
|
|
|
=> _subtitleFormats.ContainsKey(fileExtension);
|
|
|
|
|
2023-11-14 19:21:34 +00:00
|
|
|
private List<SubtitleFormat> GetSubtitleFormats()
|
2022-08-01 18:25:42 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
2021-01-08 22:03:02 +00:00
|
|
|
}
|
|
|
|
}
|