| | | 1 | | using System; |
| | | 2 | | using System.Collections.Generic; |
| | | 3 | | using System.Globalization; |
| | | 4 | | using System.IO; |
| | | 5 | | using System.Linq; |
| | | 6 | | using Jellyfin.Extensions; |
| | | 7 | | using MediaBrowser.Model.MediaInfo; |
| | | 8 | | using Microsoft.Extensions.Logging; |
| | | 9 | | using Nikse.SubtitleEdit.Core.Common; |
| | | 10 | | using SubtitleFormat = Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat; |
| | | 11 | | |
| | | 12 | | namespace MediaBrowser.MediaEncoding.Subtitles |
| | | 13 | | { |
| | | 14 | | /// <summary> |
| | | 15 | | /// SubStation Alpha subtitle parser. |
| | | 16 | | /// </summary> |
| | | 17 | | public class SubtitleEditParser : ISubtitleParser |
| | | 18 | | { |
| | | 19 | | private readonly ILogger<SubtitleEditParser> _logger; |
| | | 20 | | private readonly Dictionary<string, List<Type>> _subtitleFormatTypes; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Initializes a new instance of the <see cref="SubtitleEditParser"/> class. |
| | | 24 | | /// </summary> |
| | | 25 | | /// <param name="logger">The logger.</param> |
| | | 26 | | public SubtitleEditParser(ILogger<SubtitleEditParser> logger) |
| | | 27 | | { |
| | 26 | 28 | | _logger = logger; |
| | 26 | 29 | | _subtitleFormatTypes = GetSubtitleFormatTypes(); |
| | 26 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public SubtitleTrackInfo Parse(Stream stream, string fileExtension) |
| | | 34 | | { |
| | 5 | 35 | | var subtitle = new Subtitle(); |
| | 5 | 36 | | var lines = stream.ReadAllLines().ToList(); |
| | | 37 | | |
| | 5 | 38 | | if (!_subtitleFormatTypes.TryGetValue(fileExtension, out var subtitleFormatTypesForExtension)) |
| | | 39 | | { |
| | 0 | 40 | | throw new ArgumentException($"Unsupported file extension: {fileExtension}", nameof(fileExtension)); |
| | | 41 | | } |
| | | 42 | | |
| | 15 | 43 | | foreach (var subtitleFormatType in subtitleFormatTypesForExtension) |
| | | 44 | | { |
| | 5 | 45 | | var subtitleFormat = (SubtitleFormat)Activator.CreateInstance(subtitleFormatType, true)!; |
| | 5 | 46 | | _logger.LogDebug( |
| | 5 | 47 | | "Trying to parse '{FileExtension}' subtitle using the {SubtitleFormatParser} format parser", |
| | 5 | 48 | | fileExtension, |
| | 5 | 49 | | subtitleFormat.Name); |
| | 5 | 50 | | subtitleFormat.LoadSubtitle(subtitle, lines, fileExtension); |
| | 5 | 51 | | if (subtitleFormat.ErrorCount == 0) |
| | | 52 | | { |
| | 5 | 53 | | break; |
| | | 54 | | } |
| | 0 | 55 | | else if (subtitleFormat.TryGetErrors(out var errors)) |
| | | 56 | | { |
| | 0 | 57 | | _logger.LogError( |
| | 0 | 58 | | "{ErrorCount} errors encountered while parsing '{FileExtension}' subtitle using the {SubtitleFor |
| | 0 | 59 | | subtitleFormat.ErrorCount, |
| | 0 | 60 | | fileExtension, |
| | 0 | 61 | | subtitleFormat.Name, |
| | 0 | 62 | | errors); |
| | | 63 | | } |
| | | 64 | | else |
| | | 65 | | { |
| | 0 | 66 | | _logger.LogError( |
| | 0 | 67 | | "{ErrorCount} errors encountered while parsing '{FileExtension}' subtitle using the {SubtitleFor |
| | 0 | 68 | | subtitleFormat.ErrorCount, |
| | 0 | 69 | | fileExtension, |
| | 0 | 70 | | subtitleFormat.Name); |
| | | 71 | | } |
| | | 72 | | } |
| | | 73 | | |
| | 5 | 74 | | if (subtitle.Paragraphs.Count == 0) |
| | | 75 | | { |
| | 0 | 76 | | throw new ArgumentException("Unsupported format: " + fileExtension); |
| | | 77 | | } |
| | | 78 | | |
| | 5 | 79 | | var trackInfo = new SubtitleTrackInfo(); |
| | 5 | 80 | | int len = subtitle.Paragraphs.Count; |
| | 5 | 81 | | var trackEvents = new SubtitleTrackEvent[len]; |
| | 28 | 82 | | for (int i = 0; i < len; i++) |
| | | 83 | | { |
| | 9 | 84 | | var p = subtitle.Paragraphs[i]; |
| | 9 | 85 | | trackEvents[i] = new SubtitleTrackEvent(p.Number.ToString(CultureInfo.InvariantCulture), p.Text) |
| | 9 | 86 | | { |
| | 9 | 87 | | StartPositionTicks = p.StartTime.TimeSpan.Ticks, |
| | 9 | 88 | | EndPositionTicks = p.EndTime.TimeSpan.Ticks |
| | 9 | 89 | | }; |
| | | 90 | | } |
| | | 91 | | |
| | 5 | 92 | | trackInfo.TrackEvents = trackEvents; |
| | 5 | 93 | | return trackInfo; |
| | | 94 | | } |
| | | 95 | | |
| | | 96 | | /// <inheritdoc /> |
| | | 97 | | public bool SupportsFileExtension(string fileExtension) |
| | 0 | 98 | | => _subtitleFormatTypes.ContainsKey(fileExtension); |
| | | 99 | | |
| | | 100 | | private Dictionary<string, List<Type>> GetSubtitleFormatTypes() |
| | | 101 | | { |
| | 26 | 102 | | var subtitleFormatTypes = new Dictionary<string, List<Type>>(StringComparer.OrdinalIgnoreCase); |
| | 26 | 103 | | var assembly = typeof(SubtitleFormat).Assembly; |
| | | 104 | | |
| | 67340 | 105 | | foreach (var type in assembly.GetTypes()) |
| | | 106 | | { |
| | 33644 | 107 | | if (!type.IsSubclassOf(typeof(SubtitleFormat)) || type.IsAbstract) |
| | | 108 | | { |
| | | 109 | | continue; |
| | | 110 | | } |
| | | 111 | | |
| | | 112 | | try |
| | | 113 | | { |
| | 10348 | 114 | | var tempInstance = (SubtitleFormat)Activator.CreateInstance(type, true)!; |
| | 10343 | 115 | | var extension = tempInstance.Extension.TrimStart('.'); |
| | 10343 | 116 | | if (!string.IsNullOrEmpty(extension)) |
| | | 117 | | { |
| | | 118 | | // Store only the type, we will instantiate from it later |
| | 10343 | 119 | | if (!subtitleFormatTypes.TryGetValue(extension, out var subtitleFormatTypesForExtension)) |
| | | 120 | | { |
| | 2439 | 121 | | subtitleFormatTypes[extension] = [type]; |
| | | 122 | | } |
| | | 123 | | else |
| | | 124 | | { |
| | 7904 | 125 | | subtitleFormatTypesForExtension.Add(type); |
| | | 126 | | } |
| | | 127 | | } |
| | 10343 | 128 | | } |
| | 5 | 129 | | catch (Exception ex) |
| | | 130 | | { |
| | 5 | 131 | | _logger.LogWarning(ex, "Failed to create instance of the subtitle format {SubtitleFormatType}", type |
| | 5 | 132 | | } |
| | | 133 | | } |
| | | 134 | | |
| | 26 | 135 | | return subtitleFormatTypes; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | } |