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