瀏覽代碼

Add subtitle parser errors to log if available (#12479)

Łukasz 9 月之前
父節點
當前提交
1451cbc39e

+ 1 - 0
CONTRIBUTORS.md

@@ -187,6 +187,7 @@
  - [HonestlyWhoKnows](https://github.com/honestlywhoknows)
  - [TheMelmacian](https://github.com/TheMelmacian)
  - [ItsAllAboutTheCode](https://github.com/ItsAllAboutTheCode)
+ - [pret0rian8](https://github.com/pret0rian)
 
 # Emby Contributors
 

+ 17 - 6
MediaBrowser.MediaEncoding/Subtitles/SubtitleEditParser.cs

@@ -54,12 +54,23 @@ namespace MediaBrowser.MediaEncoding.Subtitles
                 {
                     break;
                 }
-
-                _logger.LogError(
-                    "{ErrorCount} errors encountered while parsing '{FileExtension}' subtitle using the {SubtitleFormatParser} format parser",
-                    subtitleFormat.ErrorCount,
-                    fileExtension,
-                    subtitleFormat.Name);
+                else if (subtitleFormat.TryGetErrors(out var errors))
+                {
+                    _logger.LogError(
+                        "{ErrorCount} errors encountered while parsing '{FileExtension}' subtitle using the {SubtitleFormatParser} format parser, errors: {Errors}",
+                        subtitleFormat.ErrorCount,
+                        fileExtension,
+                        subtitleFormat.Name,
+                        errors);
+                }
+                else
+                {
+                    _logger.LogError(
+                        "{ErrorCount} errors encountered while parsing '{FileExtension}' subtitle using the {SubtitleFormatParser} format parser",
+                        subtitleFormat.ErrorCount,
+                        fileExtension,
+                        subtitleFormat.Name);
+                }
             }
 
             if (subtitle.Paragraphs.Count == 0)

+ 29 - 0
MediaBrowser.MediaEncoding/Subtitles/SubtitleFormatExtensions.cs

@@ -0,0 +1,29 @@
+using System.Diagnostics.CodeAnalysis;
+using Nikse.SubtitleEdit.Core.SubtitleFormats;
+
+namespace MediaBrowser.MediaEncoding.Subtitles;
+
+internal static class SubtitleFormatExtensions
+{
+    /// <summary>
+    /// Will try to find errors if supported by provider.
+    /// </summary>
+    /// <param name="format">The subtitle format.</param>
+    /// <param name="errors">The out errors value.</param>
+    /// <returns>True if errors are available for given format.</returns>
+    public static bool TryGetErrors(this SubtitleFormat format, [NotNullWhen(true)] out string? errors)
+    {
+        errors = format switch
+        {
+            SubStationAlpha ssa => ssa.Errors,
+            AdvancedSubStationAlpha assa => assa.Errors,
+            SubRip subRip => subRip.Errors,
+            MicroDvd microDvd => microDvd.Errors,
+            DCinemaSmpte2007 smpte2007 => smpte2007.Errors,
+            DCinemaSmpte2010 smpte2010 => smpte2010.Errors,
+            _ => null,
+        };
+
+        return !string.IsNullOrWhiteSpace(errors);
+    }
+}