Log subtitle errors

This commit is contained in:
Bond_009 2021-01-08 23:21:50 +01:00
parent ed8fce2dce
commit be965e35b6
8 changed files with 53 additions and 8 deletions

View File

@ -1,5 +1,6 @@
#nullable enable
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace MediaBrowser.MediaEncoding.Subtitles
@ -9,5 +10,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
/// </summary>
public class AssParser : SubtitleEditParser<AdvancedSubStationAlpha>
{
/// <summary>
/// Initializes a new instance of the <see cref="AssParser"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
public AssParser(ILogger logger) : base(logger)
{
}
}
}

View File

@ -1,5 +1,6 @@
#nullable enable
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace MediaBrowser.MediaEncoding.Subtitles
@ -9,5 +10,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
/// </summary>
public class SrtParser : SubtitleEditParser<SubRip>
{
/// <summary>
/// Initializes a new instance of the <see cref="SrtParser"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
public SrtParser(ILogger logger) : base(logger)
{
}
}
}

View File

@ -1,5 +1,6 @@
#nullable enable
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core.SubtitleFormats;
namespace MediaBrowser.MediaEncoding.Subtitles
@ -9,5 +10,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
/// </summary>
public class SsaParser : SubtitleEditParser<SubStationAlpha>
{
/// <summary>
/// Initializes a new instance of the <see cref="SsaParser"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
public SsaParser(ILogger logger) : base(logger)
{
}
}
}

View File

@ -6,17 +6,31 @@ using System.Linq;
using System.Threading;
using MediaBrowser.Common.Extensions;
using MediaBrowser.Model.MediaInfo;
using Microsoft.Extensions.Logging;
using Nikse.SubtitleEdit.Core;
using ILogger = Microsoft.Extensions.Logging.ILogger;
using SubtitleFormat = Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat;
namespace MediaBrowser.MediaEncoding.Subtitles
{
/// <summary>
/// SubStation Alpha subtitle parser.
/// </summary>
/// <typeparam name="T">The <see cref="Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat" />.</typeparam>
/// <typeparam name="T">The <see cref="SubtitleFormat" />.</typeparam>
public abstract class SubtitleEditParser<T> : ISubtitleParser
where T : Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat, new()
where T : SubtitleFormat, new()
{
private readonly ILogger _logger;
/// <summary>
/// Initializes a new instance of the <see cref="SubtitleEditParser{T}"/> class.
/// </summary>
/// <param name="logger">The logger.</param>
protected SubtitleEditParser(ILogger logger)
{
_logger = logger;
}
/// <inheritdoc />
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
{
@ -24,6 +38,10 @@ namespace MediaBrowser.MediaEncoding.Subtitles
var subRip = new T();
var lines = stream.ReadAllLines().ToList();
subRip.LoadSubtitle(subtitle, lines, "untitled");
if (subRip.ErrorCount > 0)
{
_logger.LogError("{ErrorCount} errors encountered while parsing subtitle.");
}
var trackInfo = new SubtitleTrackInfo();
int len = subtitle.Paragraphs.Count;

View File

@ -271,17 +271,17 @@ namespace MediaBrowser.MediaEncoding.Subtitles
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
{
return new SrtParser();
return new SrtParser(_logger);
}
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
{
return new SsaParser();
return new SsaParser(_logger);
}
if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
{
return new AssParser();
return new AssParser(_logger);
}
if (throwIfMissing)

View File

@ -3,6 +3,7 @@ using System.Globalization;
using System.IO;
using System.Threading;
using MediaBrowser.MediaEncoding.Subtitles;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
namespace Jellyfin.MediaEncoding.Subtitles.Tests
@ -14,7 +15,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
{
using (var stream = File.OpenRead("Test Data/example.ass"))
{
var parsed = new AssParser().Parse(stream, CancellationToken.None);
var parsed = new AssParser(new NullLogger<AssParser>()).Parse(stream, CancellationToken.None);
Assert.Single(parsed.TrackEvents);
var trackEvent = parsed.TrackEvents[0];

View File

@ -3,6 +3,7 @@ using System.Globalization;
using System.IO;
using System.Threading;
using MediaBrowser.MediaEncoding.Subtitles;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
namespace Jellyfin.MediaEncoding.Subtitles.Tests
@ -14,7 +15,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
{
using (var stream = File.OpenRead("Test Data/example.srt"))
{
var parsed = new SrtParser().Parse(stream, CancellationToken.None);
var parsed = new SrtParser(new NullLogger<SrtParser>()).Parse(stream, CancellationToken.None);
Assert.Equal(2, parsed.TrackEvents.Count);
var trackEvent1 = parsed.TrackEvents[0];

View File

@ -3,6 +3,7 @@ using System.Globalization;
using System.IO;
using System.Threading;
using MediaBrowser.MediaEncoding.Subtitles;
using Microsoft.Extensions.Logging.Abstractions;
using Xunit;
namespace Jellyfin.MediaEncoding.Subtitles.Tests
@ -14,7 +15,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
{
using (var stream = File.OpenRead("Test Data/example.ssa"))
{
var parsed = new SsaParser().Parse(stream, CancellationToken.None);
var parsed = new SsaParser(new NullLogger<SsaParser>()).Parse(stream, CancellationToken.None);
Assert.Single(parsed.TrackEvents);
var trackEvent = parsed.TrackEvents[0];