mirror of
https://github.com/jellyfin/jellyfin.git
synced 2024-09-05 21:37:10 -04:00
Merge pull request #4984 from Bond-009/subtitleedit
This commit is contained in:
commit
23ff1fab46
@ -374,7 +374,7 @@ namespace Emby.Server.Implementations
|
|||||||
/// <summary>
|
/// <summary>
|
||||||
/// Creates an instance of type and resolves all constructor dependencies.
|
/// Creates an instance of type and resolves all constructor dependencies.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// /// <typeparam name="T">The type.</typeparam>
|
/// <typeparam name="T">The type.</typeparam>
|
||||||
/// <returns>T.</returns>
|
/// <returns>T.</returns>
|
||||||
public T CreateInstance<T>()
|
public T CreateInstance<T>()
|
||||||
=> ActivatorUtilities.CreateInstance<T>(ServiceProvider);
|
=> ActivatorUtilities.CreateInstance<T>(ServiceProvider);
|
||||||
|
51
MediaBrowser.Common/Extensions/StreamExtensions.cs
Normal file
51
MediaBrowser.Common/Extensions/StreamExtensions.cs
Normal file
@ -0,0 +1,51 @@
|
|||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using System.Collections.Generic;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Text;
|
||||||
|
|
||||||
|
namespace MediaBrowser.Common.Extensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Class BaseExtensions.
|
||||||
|
/// </summary>
|
||||||
|
public static class StreamExtensions
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Reads all lines in the <see cref="Stream" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">The <see cref="Stream" /> to read from.</param>
|
||||||
|
/// <returns>All lines in the stream.</returns>
|
||||||
|
public static string[] ReadAllLines(this Stream stream)
|
||||||
|
=> ReadAllLines(stream, Encoding.UTF8);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads all lines in the <see cref="Stream" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="stream">The <see cref="Stream" /> to read from.</param>
|
||||||
|
/// <param name="encoding">The character encoding to use.</param>
|
||||||
|
/// <returns>All lines in the stream.</returns>
|
||||||
|
public static string[] ReadAllLines(this Stream stream, Encoding encoding)
|
||||||
|
{
|
||||||
|
using (StreamReader reader = new StreamReader(stream, encoding))
|
||||||
|
{
|
||||||
|
return ReadAllLines(reader).ToArray();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Reads all lines in the <see cref="StreamReader" />.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="reader">The <see cref="StreamReader" /> to read from.</param>
|
||||||
|
/// <returns>All lines in the stream.</returns>
|
||||||
|
public static IEnumerable<string> ReadAllLines(this StreamReader reader)
|
||||||
|
{
|
||||||
|
string? line;
|
||||||
|
while ((line = reader.ReadLine()) != null)
|
||||||
|
{
|
||||||
|
yield return line;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -24,6 +24,7 @@
|
|||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<PackageReference Include="BDInfo" Version="0.7.6.1" />
|
<PackageReference Include="BDInfo" Version="0.7.6.1" />
|
||||||
|
<PackageReference Include="libse" Version="3.5.8" />
|
||||||
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
<PackageReference Include="Microsoft.Extensions.Http" Version="5.0.0" />
|
||||||
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
|
<PackageReference Include="System.Text.Encoding.CodePages" Version="5.0.0" />
|
||||||
<PackageReference Include="UTF.Unknown" Version="2.3.0" />
|
<PackageReference Include="UTF.Unknown" Version="2.3.0" />
|
||||||
|
@ -1,130 +1,21 @@
|
|||||||
#pragma warning disable CS1591
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.Collections.Generic;
|
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Linq;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using System.Threading;
|
|
||||||
using MediaBrowser.Model.MediaInfo;
|
|
||||||
|
|
||||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||||
{
|
{
|
||||||
public class AssParser : ISubtitleParser
|
/// <summary>
|
||||||
|
/// Advanced SubStation Alpha subtitle parser.
|
||||||
|
/// </summary>
|
||||||
|
public class AssParser : SubtitleEditParser<AdvancedSubStationAlpha>
|
||||||
{
|
{
|
||||||
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="AssParser"/> class.
|
||||||
/// <inheritdoc />
|
/// </summary>
|
||||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
/// <param name="logger">The logger.</param>
|
||||||
|
public AssParser(ILogger logger) : base(logger)
|
||||||
{
|
{
|
||||||
var trackInfo = new SubtitleTrackInfo();
|
|
||||||
var trackEvents = new List<SubtitleTrackEvent>();
|
|
||||||
var eventIndex = 1;
|
|
||||||
using (var reader = new StreamReader(stream))
|
|
||||||
{
|
|
||||||
string line;
|
|
||||||
while (!string.Equals(reader.ReadLine(), "[Events]", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
}
|
|
||||||
|
|
||||||
var headers = ParseFieldHeaders(reader.ReadLine());
|
|
||||||
|
|
||||||
while ((line = reader.ReadLine()) != null)
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(line))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (line[0] == '[')
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
var subEvent = new SubtitleTrackEvent { Id = eventIndex.ToString(_usCulture) };
|
|
||||||
eventIndex++;
|
|
||||||
const string Dialogue = "Dialogue: ";
|
|
||||||
var sections = line.Substring(Dialogue.Length).Split(',');
|
|
||||||
|
|
||||||
subEvent.StartPositionTicks = GetTicks(sections[headers["Start"]]);
|
|
||||||
subEvent.EndPositionTicks = GetTicks(sections[headers["End"]]);
|
|
||||||
|
|
||||||
subEvent.Text = string.Join(',', sections[headers["Text"]..]);
|
|
||||||
RemoteNativeFormatting(subEvent);
|
|
||||||
|
|
||||||
subEvent.Text = subEvent.Text.Replace("\\n", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
|
|
||||||
|
|
||||||
subEvent.Text = Regex.Replace(subEvent.Text, @"\{(\\[\w]+\(?([\w0-9]+,?)+\)?)+\}", string.Empty, RegexOptions.IgnoreCase);
|
|
||||||
|
|
||||||
trackEvents.Add(subEvent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trackInfo.TrackEvents = trackEvents;
|
|
||||||
return trackInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long GetTicks(ReadOnlySpan<char> time)
|
|
||||||
{
|
|
||||||
return TimeSpan.TryParseExact(time, @"h\:mm\:ss\.ff", _usCulture, out var span)
|
|
||||||
? span.Ticks : 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
internal static Dictionary<string, int> ParseFieldHeaders(string line)
|
|
||||||
{
|
|
||||||
const string Format = "Format: ";
|
|
||||||
var fields = line.Substring(Format.Length).Split(',').Select(x => x.Trim()).ToList();
|
|
||||||
|
|
||||||
return new Dictionary<string, int>
|
|
||||||
{
|
|
||||||
{ "Start", fields.IndexOf("Start") },
|
|
||||||
{ "End", fields.IndexOf("End") },
|
|
||||||
{ "Text", fields.IndexOf("Text") }
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
private void RemoteNativeFormatting(SubtitleTrackEvent p)
|
|
||||||
{
|
|
||||||
int indexOfBegin = p.Text.IndexOf('{', StringComparison.Ordinal);
|
|
||||||
string pre = string.Empty;
|
|
||||||
while (indexOfBegin >= 0 && p.Text.IndexOf('}', StringComparison.Ordinal) > indexOfBegin)
|
|
||||||
{
|
|
||||||
string s = p.Text.Substring(indexOfBegin);
|
|
||||||
if (s.StartsWith("{\\an1}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an2}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an3}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an4}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an5}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an6}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an7}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an8}", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an9}", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
pre = s.Substring(0, 6);
|
|
||||||
}
|
|
||||||
else if (s.StartsWith("{\\an1\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an2\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an3\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an4\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an5\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an6\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an7\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an8\\", StringComparison.Ordinal) ||
|
|
||||||
s.StartsWith("{\\an9\\", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
pre = s.Substring(0, 5) + "}";
|
|
||||||
}
|
|
||||||
|
|
||||||
int indexOfEnd = p.Text.IndexOf('}', StringComparison.Ordinal);
|
|
||||||
p.Text = p.Text.Remove(indexOfBegin, (indexOfEnd - indexOfBegin) + 1);
|
|
||||||
|
|
||||||
indexOfBegin = p.Text.IndexOf('{', StringComparison.Ordinal);
|
|
||||||
}
|
|
||||||
|
|
||||||
p.Text = pre + p.Text;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,102 +1,21 @@
|
|||||||
#pragma warning disable CS1591
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
|
||||||
using System.IO;
|
|
||||||
using System.Text.RegularExpressions;
|
|
||||||
using System.Threading;
|
|
||||||
using MediaBrowser.Model.MediaInfo;
|
|
||||||
using Microsoft.Extensions.Logging;
|
using Microsoft.Extensions.Logging;
|
||||||
|
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||||
|
|
||||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||||
{
|
{
|
||||||
public class SrtParser : ISubtitleParser
|
/// <summary>
|
||||||
|
/// SubRip subtitle parser.
|
||||||
|
/// </summary>
|
||||||
|
public class SrtParser : SubtitleEditParser<SubRip>
|
||||||
{
|
{
|
||||||
private readonly ILogger _logger;
|
/// <summary>
|
||||||
|
/// Initializes a new instance of the <see cref="SrtParser"/> class.
|
||||||
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger.</param>
|
||||||
public SrtParser(ILogger logger)
|
public SrtParser(ILogger logger) : base(logger)
|
||||||
{
|
{
|
||||||
_logger = logger;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
var trackInfo = new SubtitleTrackInfo();
|
|
||||||
var trackEvents = new List<SubtitleTrackEvent>();
|
|
||||||
using (var reader = new StreamReader(stream))
|
|
||||||
{
|
|
||||||
string line;
|
|
||||||
while ((line = reader.ReadLine()) != null)
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(line))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var subEvent = new SubtitleTrackEvent { Id = line };
|
|
||||||
line = reader.ReadLine();
|
|
||||||
|
|
||||||
if (string.IsNullOrWhiteSpace(line))
|
|
||||||
{
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
var time = Regex.Split(line, @"[\t ]*-->[\t ]*");
|
|
||||||
|
|
||||||
if (time.Length < 2)
|
|
||||||
{
|
|
||||||
// This occurs when subtitle text has an empty line as part of the text.
|
|
||||||
// Need to adjust the break statement below to resolve this.
|
|
||||||
_logger.LogWarning("Unrecognized line in srt: {0}", line);
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
subEvent.StartPositionTicks = GetTicks(time[0]);
|
|
||||||
var endTime = time[1].AsSpan();
|
|
||||||
var idx = endTime.IndexOf(' ');
|
|
||||||
if (idx > 0)
|
|
||||||
{
|
|
||||||
endTime = endTime.Slice(0, idx);
|
|
||||||
}
|
|
||||||
|
|
||||||
subEvent.EndPositionTicks = GetTicks(endTime);
|
|
||||||
var multiline = new List<string>();
|
|
||||||
while ((line = reader.ReadLine()) != null)
|
|
||||||
{
|
|
||||||
if (line.Length == 0)
|
|
||||||
{
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
multiline.Add(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
subEvent.Text = string.Join(ParserValues.NewLine, multiline);
|
|
||||||
subEvent.Text = subEvent.Text.Replace(@"\N", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
|
|
||||||
subEvent.Text = Regex.Replace(subEvent.Text, @"\{(?:\\[0-9]?[\w.-]+(?:\([^\)]*\)|&H?[0-9A-Fa-f]+&|))+\}", string.Empty, RegexOptions.IgnoreCase);
|
|
||||||
subEvent.Text = Regex.Replace(subEvent.Text, "<", "<", RegexOptions.IgnoreCase);
|
|
||||||
subEvent.Text = Regex.Replace(subEvent.Text, ">", ">", RegexOptions.IgnoreCase);
|
|
||||||
subEvent.Text = Regex.Replace(subEvent.Text, "<(\\/?(font|b|u|i|s))((\\s+(\\w|\\w[\\w\\-]*\\w)(\\s*=\\s*(?:\\\".*?\\\"|'.*?'|[^'\\\">\\s]+))?)+\\s*|\\s*)(\\/?)>", "<$1$3$7>", RegexOptions.IgnoreCase);
|
|
||||||
trackEvents.Add(subEvent);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
trackInfo.TrackEvents = trackEvents;
|
|
||||||
return trackInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
private long GetTicks(ReadOnlySpan<char> time)
|
|
||||||
{
|
|
||||||
return TimeSpan.TryParseExact(time, @"hh\:mm\:ss\.fff", _usCulture, out var span)
|
|
||||||
? span.Ticks
|
|
||||||
: (TimeSpan.TryParseExact(time, @"hh\:mm\:ss\,fff", _usCulture, out span)
|
|
||||||
? span.Ticks : 0);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,477 +1,21 @@
|
|||||||
using System;
|
#nullable enable
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
using Microsoft.Extensions.Logging;
|
||||||
using System.IO;
|
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||||
using System.Text;
|
|
||||||
using System.Threading;
|
|
||||||
using MediaBrowser.Model.MediaInfo;
|
|
||||||
|
|
||||||
namespace MediaBrowser.MediaEncoding.Subtitles
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||||
{
|
{
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// <see href="https://github.com/SubtitleEdit/subtitleedit/blob/a299dc4407a31796364cc6ad83f0d3786194ba22/src/Logic/SubtitleFormats/SubStationAlpha.cs">Credit</see>.
|
/// SubStation Alpha subtitle parser.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public class SsaParser : ISubtitleParser
|
public class SsaParser : SubtitleEditParser<SubStationAlpha>
|
||||||
{
|
{
|
||||||
/// <inheritdoc />
|
/// <summary>
|
||||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
/// Initializes a new instance of the <see cref="SsaParser"/> class.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="logger">The logger.</param>
|
||||||
|
public SsaParser(ILogger logger) : base(logger)
|
||||||
{
|
{
|
||||||
var trackInfo = new SubtitleTrackInfo();
|
|
||||||
var trackEvents = new List<SubtitleTrackEvent>();
|
|
||||||
|
|
||||||
using (var reader = new StreamReader(stream))
|
|
||||||
{
|
|
||||||
bool eventsStarted = false;
|
|
||||||
|
|
||||||
string[] format = "Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text".Split(',');
|
|
||||||
int indexLayer = 0;
|
|
||||||
int indexStart = 1;
|
|
||||||
int indexEnd = 2;
|
|
||||||
int indexStyle = 3;
|
|
||||||
int indexName = 4;
|
|
||||||
int indexEffect = 8;
|
|
||||||
int indexText = 9;
|
|
||||||
int lineNumber = 0;
|
|
||||||
|
|
||||||
var header = new StringBuilder();
|
|
||||||
|
|
||||||
string line;
|
|
||||||
|
|
||||||
while ((line = reader.ReadLine()) != null)
|
|
||||||
{
|
|
||||||
cancellationToken.ThrowIfCancellationRequested();
|
|
||||||
|
|
||||||
lineNumber++;
|
|
||||||
if (!eventsStarted)
|
|
||||||
{
|
|
||||||
header.AppendLine(line);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (string.Equals(line.Trim(), "[events]", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
eventsStarted = true;
|
|
||||||
}
|
|
||||||
else if (!string.IsNullOrEmpty(line) && line.Trim().StartsWith(';'))
|
|
||||||
{
|
|
||||||
// skip comment lines
|
|
||||||
}
|
|
||||||
else if (eventsStarted && line.Trim().Length > 0)
|
|
||||||
{
|
|
||||||
string s = line.Trim().ToLowerInvariant();
|
|
||||||
if (s.StartsWith("format:", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
if (line.Length > 10)
|
|
||||||
{
|
|
||||||
format = line.ToLowerInvariant().Substring(8).Split(',');
|
|
||||||
for (int i = 0; i < format.Length; i++)
|
|
||||||
{
|
|
||||||
if (string.Equals(format[i].Trim(), "layer", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
indexLayer = i;
|
|
||||||
}
|
|
||||||
else if (string.Equals(format[i].Trim(), "start", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
indexStart = i;
|
|
||||||
}
|
|
||||||
else if (string.Equals(format[i].Trim(), "end", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
indexEnd = i;
|
|
||||||
}
|
|
||||||
else if (string.Equals(format[i].Trim(), "text", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
indexText = i;
|
|
||||||
}
|
|
||||||
else if (string.Equals(format[i].Trim(), "effect", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
indexEffect = i;
|
|
||||||
}
|
|
||||||
else if (string.Equals(format[i].Trim(), "style", StringComparison.OrdinalIgnoreCase))
|
|
||||||
{
|
|
||||||
indexStyle = i;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (!string.IsNullOrEmpty(s))
|
|
||||||
{
|
|
||||||
string text = string.Empty;
|
|
||||||
string start = string.Empty;
|
|
||||||
string end = string.Empty;
|
|
||||||
string style = string.Empty;
|
|
||||||
string layer = string.Empty;
|
|
||||||
string effect = string.Empty;
|
|
||||||
string name = string.Empty;
|
|
||||||
|
|
||||||
string[] splittedLine;
|
|
||||||
|
|
||||||
if (s.StartsWith("dialogue:", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
splittedLine = line.Substring(10).Split(',');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
splittedLine = line.Split(',');
|
|
||||||
}
|
|
||||||
|
|
||||||
for (int i = 0; i < splittedLine.Length; i++)
|
|
||||||
{
|
|
||||||
if (i == indexStart)
|
|
||||||
{
|
|
||||||
start = splittedLine[i].Trim();
|
|
||||||
}
|
|
||||||
else if (i == indexEnd)
|
|
||||||
{
|
|
||||||
end = splittedLine[i].Trim();
|
|
||||||
}
|
|
||||||
else if (i == indexLayer)
|
|
||||||
{
|
|
||||||
layer = splittedLine[i];
|
|
||||||
}
|
|
||||||
else if (i == indexEffect)
|
|
||||||
{
|
|
||||||
effect = splittedLine[i];
|
|
||||||
}
|
|
||||||
else if (i == indexText)
|
|
||||||
{
|
|
||||||
text = splittedLine[i];
|
|
||||||
}
|
|
||||||
else if (i == indexStyle)
|
|
||||||
{
|
|
||||||
style = splittedLine[i];
|
|
||||||
}
|
|
||||||
else if (i == indexName)
|
|
||||||
{
|
|
||||||
name = splittedLine[i];
|
|
||||||
}
|
|
||||||
else if (i > indexText)
|
|
||||||
{
|
|
||||||
text += "," + splittedLine[i];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
try
|
|
||||||
{
|
|
||||||
trackEvents.Add(
|
|
||||||
new SubtitleTrackEvent
|
|
||||||
{
|
|
||||||
StartPositionTicks = GetTimeCodeFromString(start),
|
|
||||||
EndPositionTicks = GetTimeCodeFromString(end),
|
|
||||||
Text = GetFormattedText(text)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
catch
|
|
||||||
{
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// if (header.Length > 0)
|
|
||||||
// subtitle.Header = header.ToString();
|
|
||||||
|
|
||||||
// subtitle.Renumber(1);
|
|
||||||
}
|
|
||||||
|
|
||||||
trackInfo.TrackEvents = trackEvents.ToArray();
|
|
||||||
return trackInfo;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static long GetTimeCodeFromString(string time)
|
|
||||||
{
|
|
||||||
// h:mm:ss.cc
|
|
||||||
string[] timeCode = time.Split(':', '.');
|
|
||||||
return new TimeSpan(
|
|
||||||
0,
|
|
||||||
int.Parse(timeCode[0], CultureInfo.InvariantCulture),
|
|
||||||
int.Parse(timeCode[1], CultureInfo.InvariantCulture),
|
|
||||||
int.Parse(timeCode[2], CultureInfo.InvariantCulture),
|
|
||||||
int.Parse(timeCode[3], CultureInfo.InvariantCulture) * 10).Ticks;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static string GetFormattedText(string text)
|
|
||||||
{
|
|
||||||
text = text.Replace("\\n", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
|
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++) // just look ten times...
|
|
||||||
{
|
|
||||||
if (text.Contains(@"{\fn", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
int start = text.IndexOf(@"{\fn", StringComparison.Ordinal);
|
|
||||||
int end = text.IndexOf('}', start);
|
|
||||||
if (end > 0 && !text.Substring(start).StartsWith("{\\fn}", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
string fontName = text.Substring(start + 4, end - (start + 4));
|
|
||||||
string extraTags = string.Empty;
|
|
||||||
CheckAndAddSubTags(ref fontName, ref extraTags, out bool italic);
|
|
||||||
text = text.Remove(start, end - start + 1);
|
|
||||||
if (italic)
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font face=\"" + fontName + "\"" + extraTags + "><i>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font face=\"" + fontName + "\"" + extraTags + ">");
|
|
||||||
}
|
|
||||||
|
|
||||||
int indexOfEndTag = text.IndexOf("{\\fn}", start, StringComparison.Ordinal);
|
|
||||||
if (indexOfEndTag > 0)
|
|
||||||
{
|
|
||||||
text = text.Remove(indexOfEndTag, "{\\fn}".Length).Insert(indexOfEndTag, "</font>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text += "</font>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text.Contains(@"{\fs", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
int start = text.IndexOf(@"{\fs", StringComparison.Ordinal);
|
|
||||||
int end = text.IndexOf('}', start);
|
|
||||||
if (end > 0 && !text.Substring(start).StartsWith("{\\fs}", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
string fontSize = text.Substring(start + 4, end - (start + 4));
|
|
||||||
string extraTags = string.Empty;
|
|
||||||
CheckAndAddSubTags(ref fontSize, ref extraTags, out bool italic);
|
|
||||||
if (IsInteger(fontSize))
|
|
||||||
{
|
|
||||||
text = text.Remove(start, end - start + 1);
|
|
||||||
if (italic)
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font size=\"" + fontSize + "\"" + extraTags + "><i>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font size=\"" + fontSize + "\"" + extraTags + ">");
|
|
||||||
}
|
|
||||||
|
|
||||||
int indexOfEndTag = text.IndexOf("{\\fs}", start, StringComparison.Ordinal);
|
|
||||||
if (indexOfEndTag > 0)
|
|
||||||
{
|
|
||||||
text = text.Remove(indexOfEndTag, "{\\fs}".Length).Insert(indexOfEndTag, "</font>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text += "</font>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text.Contains(@"{\c", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
int start = text.IndexOf(@"{\c", StringComparison.Ordinal);
|
|
||||||
int end = text.IndexOf('}', start);
|
|
||||||
if (end > 0 && !text.Substring(start).StartsWith("{\\c}", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
string color = text.Substring(start + 4, end - (start + 4));
|
|
||||||
string extraTags = string.Empty;
|
|
||||||
CheckAndAddSubTags(ref color, ref extraTags, out bool italic);
|
|
||||||
|
|
||||||
color = color.Replace("&", string.Empty, StringComparison.Ordinal).TrimStart('H');
|
|
||||||
color = color.PadLeft(6, '0');
|
|
||||||
|
|
||||||
// switch to rrggbb from bbggrr
|
|
||||||
color = "#" + color.Remove(color.Length - 6) + color.Substring(color.Length - 2, 2) + color.Substring(color.Length - 4, 2) + color.Substring(color.Length - 6, 2);
|
|
||||||
color = color.ToLowerInvariant();
|
|
||||||
|
|
||||||
text = text.Remove(start, end - start + 1);
|
|
||||||
if (italic)
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font color=\"" + color + "\"" + extraTags + "><i>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font color=\"" + color + "\"" + extraTags + ">");
|
|
||||||
}
|
|
||||||
|
|
||||||
int indexOfEndTag = text.IndexOf("{\\c}", start, StringComparison.Ordinal);
|
|
||||||
if (indexOfEndTag > 0)
|
|
||||||
{
|
|
||||||
text = text.Remove(indexOfEndTag, "{\\c}".Length).Insert(indexOfEndTag, "</font>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text += "</font>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if (text.Contains(@"{\1c", StringComparison.Ordinal)) // "1" specifices primary color
|
|
||||||
{
|
|
||||||
int start = text.IndexOf(@"{\1c", StringComparison.Ordinal);
|
|
||||||
int end = text.IndexOf('}', start);
|
|
||||||
if (end > 0 && !text.Substring(start).StartsWith("{\\1c}", StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
string color = text.Substring(start + 5, end - (start + 5));
|
|
||||||
string extraTags = string.Empty;
|
|
||||||
CheckAndAddSubTags(ref color, ref extraTags, out bool italic);
|
|
||||||
|
|
||||||
color = color.Replace("&", string.Empty, StringComparison.Ordinal).TrimStart('H');
|
|
||||||
color = color.PadLeft(6, '0');
|
|
||||||
|
|
||||||
// switch to rrggbb from bbggrr
|
|
||||||
color = "#" + color.Remove(color.Length - 6) + color.Substring(color.Length - 2, 2) + color.Substring(color.Length - 4, 2) + color.Substring(color.Length - 6, 2);
|
|
||||||
color = color.ToLowerInvariant();
|
|
||||||
|
|
||||||
text = text.Remove(start, end - start + 1);
|
|
||||||
if (italic)
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font color=\"" + color + "\"" + extraTags + "><i>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text = text.Insert(start, "<font color=\"" + color + "\"" + extraTags + ">");
|
|
||||||
}
|
|
||||||
|
|
||||||
int indexOfEndTag = text.IndexOf("{\\1c}", start, StringComparison.Ordinal);
|
|
||||||
if (indexOfEndTag > 0)
|
|
||||||
{
|
|
||||||
text = text.Remove(indexOfEndTag, "{\\1c}".Length).Insert(indexOfEndTag, "</font>");
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
text += "</font>";
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
text = text.Replace(@"{\i1}", "<i>", StringComparison.Ordinal);
|
|
||||||
text = text.Replace(@"{\i0}", "</i>", StringComparison.Ordinal);
|
|
||||||
text = text.Replace(@"{\i}", "</i>", StringComparison.Ordinal);
|
|
||||||
if (CountTagInText(text, "<i>") > CountTagInText(text, "</i>"))
|
|
||||||
{
|
|
||||||
text += "</i>";
|
|
||||||
}
|
|
||||||
|
|
||||||
text = text.Replace(@"{\u1}", "<u>", StringComparison.Ordinal);
|
|
||||||
text = text.Replace(@"{\u0}", "</u>", StringComparison.Ordinal);
|
|
||||||
text = text.Replace(@"{\u}", "</u>", StringComparison.Ordinal);
|
|
||||||
if (CountTagInText(text, "<u>") > CountTagInText(text, "</u>"))
|
|
||||||
{
|
|
||||||
text += "</u>";
|
|
||||||
}
|
|
||||||
|
|
||||||
text = text.Replace(@"{\b1}", "<b>", StringComparison.Ordinal);
|
|
||||||
text = text.Replace(@"{\b0}", "</b>", StringComparison.Ordinal);
|
|
||||||
text = text.Replace(@"{\b}", "</b>", StringComparison.Ordinal);
|
|
||||||
if (CountTagInText(text, "<b>") > CountTagInText(text, "</b>"))
|
|
||||||
{
|
|
||||||
text += "</b>";
|
|
||||||
}
|
|
||||||
|
|
||||||
return text;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static bool IsInteger(string s)
|
|
||||||
=> int.TryParse(s, out _);
|
|
||||||
|
|
||||||
private static int CountTagInText(string text, string tag)
|
|
||||||
{
|
|
||||||
int count = 0;
|
|
||||||
int index = text.IndexOf(tag, StringComparison.Ordinal);
|
|
||||||
while (index >= 0)
|
|
||||||
{
|
|
||||||
count++;
|
|
||||||
if (index == text.Length)
|
|
||||||
{
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
index = text.IndexOf(tag, index + 1, StringComparison.Ordinal);
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
|
||||||
}
|
|
||||||
|
|
||||||
private static void CheckAndAddSubTags(ref string tagName, ref string extraTags, out bool italic)
|
|
||||||
{
|
|
||||||
italic = false;
|
|
||||||
int indexOfSPlit = tagName.IndexOf('\\', StringComparison.Ordinal);
|
|
||||||
if (indexOfSPlit > 0)
|
|
||||||
{
|
|
||||||
string rest = tagName.Substring(indexOfSPlit).TrimStart('\\');
|
|
||||||
tagName = tagName.Remove(indexOfSPlit);
|
|
||||||
|
|
||||||
for (int i = 0; i < 10; i++)
|
|
||||||
{
|
|
||||||
if (rest.StartsWith("fs", StringComparison.Ordinal) && rest.Length > 2)
|
|
||||||
{
|
|
||||||
indexOfSPlit = rest.IndexOf('\\', StringComparison.Ordinal);
|
|
||||||
string fontSize = rest;
|
|
||||||
if (indexOfSPlit > 0)
|
|
||||||
{
|
|
||||||
fontSize = rest.Substring(0, indexOfSPlit);
|
|
||||||
rest = rest.Substring(indexOfSPlit).TrimStart('\\');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rest = string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
extraTags += " size=\"" + fontSize.Substring(2) + "\"";
|
|
||||||
}
|
|
||||||
else if (rest.StartsWith("fn", StringComparison.Ordinal) && rest.Length > 2)
|
|
||||||
{
|
|
||||||
indexOfSPlit = rest.IndexOf('\\', StringComparison.Ordinal);
|
|
||||||
string fontName = rest;
|
|
||||||
if (indexOfSPlit > 0)
|
|
||||||
{
|
|
||||||
fontName = rest.Substring(0, indexOfSPlit);
|
|
||||||
rest = rest.Substring(indexOfSPlit).TrimStart('\\');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rest = string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
extraTags += " face=\"" + fontName.Substring(2) + "\"";
|
|
||||||
}
|
|
||||||
else if (rest.StartsWith("c", StringComparison.Ordinal) && rest.Length > 2)
|
|
||||||
{
|
|
||||||
indexOfSPlit = rest.IndexOf('\\', StringComparison.Ordinal);
|
|
||||||
string fontColor = rest;
|
|
||||||
if (indexOfSPlit > 0)
|
|
||||||
{
|
|
||||||
fontColor = rest.Substring(0, indexOfSPlit);
|
|
||||||
rest = rest.Substring(indexOfSPlit).TrimStart('\\');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rest = string.Empty;
|
|
||||||
}
|
|
||||||
|
|
||||||
string color = fontColor.Substring(2);
|
|
||||||
color = color.Replace("&", string.Empty, StringComparison.Ordinal).TrimStart('H');
|
|
||||||
color = color.PadLeft(6, '0');
|
|
||||||
// switch to rrggbb from bbggrr
|
|
||||||
color = "#" + color.Remove(color.Length - 6) + color.Substring(color.Length - 2, 2) + color.Substring(color.Length - 4, 2) + color.Substring(color.Length - 6, 2);
|
|
||||||
color = color.ToLowerInvariant();
|
|
||||||
|
|
||||||
extraTags += " color=\"" + color + "\"";
|
|
||||||
}
|
|
||||||
else if (rest.StartsWith("i1", StringComparison.Ordinal) && rest.Length > 1)
|
|
||||||
{
|
|
||||||
indexOfSPlit = rest.IndexOf('\\', StringComparison.Ordinal);
|
|
||||||
italic = true;
|
|
||||||
if (indexOfSPlit > 0)
|
|
||||||
{
|
|
||||||
rest = rest.Substring(indexOfSPlit).TrimStart('\\');
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
rest = string.Empty;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else if (rest.Length > 0 && rest.Contains('\\', StringComparison.Ordinal))
|
|
||||||
{
|
|
||||||
indexOfSPlit = rest.IndexOf('\\', StringComparison.Ordinal);
|
|
||||||
rest = rest.Substring(indexOfSPlit).TrimStart('\\');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
63
MediaBrowser.MediaEncoding/Subtitles/SubtitleEditParser.cs
Normal file
63
MediaBrowser.MediaEncoding/Subtitles/SubtitleEditParser.cs
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
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="SubtitleFormat" />.</typeparam>
|
||||||
|
public abstract class SubtitleEditParser<T> : ISubtitleParser
|
||||||
|
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)
|
||||||
|
{
|
||||||
|
var subtitle = new Subtitle();
|
||||||
|
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;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -27,7 +27,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
{
|
{
|
||||||
public class SubtitleEncoder : ISubtitleEncoder
|
public class SubtitleEncoder : ISubtitleEncoder
|
||||||
{
|
{
|
||||||
private readonly ILibraryManager _libraryManager;
|
|
||||||
private readonly ILogger<SubtitleEncoder> _logger;
|
private readonly ILogger<SubtitleEncoder> _logger;
|
||||||
private readonly IApplicationPaths _appPaths;
|
private readonly IApplicationPaths _appPaths;
|
||||||
private readonly IFileSystem _fileSystem;
|
private readonly IFileSystem _fileSystem;
|
||||||
@ -42,7 +41,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
new ConcurrentDictionary<string, SemaphoreSlim>();
|
new ConcurrentDictionary<string, SemaphoreSlim>();
|
||||||
|
|
||||||
public SubtitleEncoder(
|
public SubtitleEncoder(
|
||||||
ILibraryManager libraryManager,
|
|
||||||
ILogger<SubtitleEncoder> logger,
|
ILogger<SubtitleEncoder> logger,
|
||||||
IApplicationPaths appPaths,
|
IApplicationPaths appPaths,
|
||||||
IFileSystem fileSystem,
|
IFileSystem fileSystem,
|
||||||
@ -50,7 +48,6 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
IHttpClientFactory httpClientFactory,
|
IHttpClientFactory httpClientFactory,
|
||||||
IMediaSourceManager mediaSourceManager)
|
IMediaSourceManager mediaSourceManager)
|
||||||
{
|
{
|
||||||
_libraryManager = libraryManager;
|
|
||||||
_logger = logger;
|
_logger = logger;
|
||||||
_appPaths = appPaths;
|
_appPaths = appPaths;
|
||||||
_fileSystem = fileSystem;
|
_fileSystem = fileSystem;
|
||||||
@ -279,12 +276,12 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
|||||||
|
|
||||||
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
return new SsaParser();
|
return new SsaParser(_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(format, SubtitleFormat.ASS, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
return new AssParser();
|
return new AssParser(_logger);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (throwIfMissing)
|
if (throwIfMissing)
|
||||||
|
@ -1,10 +1,15 @@
|
|||||||
#nullable disable
|
|
||||||
#pragma warning disable CS1591
|
#pragma warning disable CS1591
|
||||||
|
|
||||||
namespace MediaBrowser.Model.MediaInfo
|
namespace MediaBrowser.Model.MediaInfo
|
||||||
{
|
{
|
||||||
public class SubtitleTrackEvent
|
public class SubtitleTrackEvent
|
||||||
{
|
{
|
||||||
|
public SubtitleTrackEvent(string id, string text)
|
||||||
|
{
|
||||||
|
Id = id;
|
||||||
|
Text = text;
|
||||||
|
}
|
||||||
|
|
||||||
public string Id { get; set; }
|
public string Id { get; set; }
|
||||||
|
|
||||||
public string Text { get; set; }
|
public string Text { get; set; }
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
#nullable enable
|
||||||
#pragma warning disable CS1591
|
#pragma warning disable CS1591
|
||||||
|
|
||||||
using System;
|
using System;
|
||||||
|
@ -3,6 +3,7 @@ using System.Globalization;
|
|||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using MediaBrowser.MediaEncoding.Subtitles;
|
using MediaBrowser.MediaEncoding.Subtitles;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
||||||
@ -14,25 +15,15 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||||||
{
|
{
|
||||||
using (var stream = File.OpenRead("Test Data/example.ass"))
|
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);
|
Assert.Single(parsed.TrackEvents);
|
||||||
var trackEvent = parsed.TrackEvents[0];
|
var trackEvent = parsed.TrackEvents[0];
|
||||||
|
|
||||||
Assert.Equal("1", trackEvent.Id);
|
Assert.Equal("1", trackEvent.Id);
|
||||||
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
|
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
|
||||||
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
|
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
|
||||||
Assert.Equal("Like an Angel with pity on nobody\r\nThe second line in subtitle", trackEvent.Text);
|
Assert.Equal("{\\pos(400,570)}Like an Angel with pity on nobody" + Environment.NewLine + "The second line in subtitle", trackEvent.Text);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
[Fact]
|
|
||||||
public void ParseFieldHeaders_Valid_Success()
|
|
||||||
{
|
|
||||||
const string Line = "Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text";
|
|
||||||
var headers = AssParser.ParseFieldHeaders(Line);
|
|
||||||
Assert.Equal(1, headers["Start"]);
|
|
||||||
Assert.Equal(2, headers["End"]);
|
|
||||||
Assert.Equal(9, headers["Text"]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -22,7 +22,7 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
|||||||
Assert.Equal("1", trackEvent1.Id);
|
Assert.Equal("1", trackEvent1.Id);
|
||||||
Assert.Equal(TimeSpan.Parse("00:02:17.440", CultureInfo.InvariantCulture).Ticks, trackEvent1.StartPositionTicks);
|
Assert.Equal(TimeSpan.Parse("00:02:17.440", CultureInfo.InvariantCulture).Ticks, trackEvent1.StartPositionTicks);
|
||||||
Assert.Equal(TimeSpan.Parse("00:02:20.375", CultureInfo.InvariantCulture).Ticks, trackEvent1.EndPositionTicks);
|
Assert.Equal(TimeSpan.Parse("00:02:20.375", CultureInfo.InvariantCulture).Ticks, trackEvent1.EndPositionTicks);
|
||||||
Assert.Equal("Senator, we're making\r\nour final approach into Coruscant.", trackEvent1.Text);
|
Assert.Equal("Senator, we're making" + Environment.NewLine + "our final approach into Coruscant.", trackEvent1.Text);
|
||||||
|
|
||||||
var trackEvent2 = parsed.TrackEvents[1];
|
var trackEvent2 = parsed.TrackEvents[1];
|
||||||
Assert.Equal("2", trackEvent2.Id);
|
Assert.Equal("2", trackEvent2.Id);
|
||||||
|
@ -1,37 +1,42 @@
|
|||||||
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Globalization;
|
||||||
using System.IO;
|
using System.IO;
|
||||||
using System.Text;
|
using System.Text;
|
||||||
using System.Threading;
|
using System.Threading;
|
||||||
using MediaBrowser.MediaEncoding.Subtitles;
|
using MediaBrowser.MediaEncoding.Subtitles;
|
||||||
using MediaBrowser.Model.MediaInfo;
|
using MediaBrowser.Model.MediaInfo;
|
||||||
|
using Microsoft.Extensions.Logging.Abstractions;
|
||||||
using Xunit;
|
using Xunit;
|
||||||
|
|
||||||
namespace Jellyfin.MediaEncoding.Tests
|
namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
||||||
{
|
{
|
||||||
public class SsaParserTests
|
public class SsaParserTests
|
||||||
{
|
{
|
||||||
// commonly shared invariant value between tests, assumes default format order
|
// commonly shared invariant value between tests, assumes default format order
|
||||||
private const string InvariantDialoguePrefix = "[Events]\nDialogue: ,0:00:00.00,0:00:00.01,,,,,,,";
|
private const string InvariantDialoguePrefix = "[Events]\nDialogue: ,0:00:00.00,0:00:00.01,,,,,,,";
|
||||||
|
|
||||||
private SsaParser parser = new SsaParser();
|
private readonly SsaParser _parser = new SsaParser(new NullLogger<AssParser>());
|
||||||
|
|
||||||
[Theory]
|
[Theory]
|
||||||
[InlineData("[EvEnTs]\nDialogue: ,0:00:00.00,0:00:00.01,,,,,,,text", "text")] // label casing insensitivity
|
[InlineData("[EvEnTs]\nDialogue: ,0:00:00.00,0:00:00.01,,,,,,,text", "text")] // label casing insensitivity
|
||||||
[InlineData("[Events]\n,0:00:00.00,0:00:00.01,,,,,,,labelless dialogue", "labelless dialogue")] // no "Dialogue:" label, it is optional
|
[InlineData("[Events]\n,0:00:00.00,0:00:00.01,,,,,,,labelless dialogue", "labelless dialogue")] // no "Dialogue:" label, it is optional
|
||||||
[InlineData("[Events]\nFormat: Text, Start, End, Layer, Effect, Style\nDialogue: reordered text,0:00:00.00,0:00:00.01", "reordered text")] // reordered formats
|
// TODO: Fix upstream
|
||||||
|
// [InlineData("[Events]\nFormat: Text, Start, End, Layer, Effect, Style\nDialogue: reordered text,0:00:00.00,0:00:00.01", "reordered text")] // reordered formats
|
||||||
[InlineData(InvariantDialoguePrefix + "Cased TEXT", "Cased TEXT")] // preserve text casing
|
[InlineData(InvariantDialoguePrefix + "Cased TEXT", "Cased TEXT")] // preserve text casing
|
||||||
[InlineData(InvariantDialoguePrefix + " text ", " text ")] // do not trim text
|
[InlineData(InvariantDialoguePrefix + " text ", " text ")] // do not trim text
|
||||||
[InlineData(InvariantDialoguePrefix + "text, more text", "text, more text")] // append excess dialogue values (> 10) to text
|
[InlineData(InvariantDialoguePrefix + "text, more text", "text, more text")] // append excess dialogue values (> 10) to text
|
||||||
[InlineData(InvariantDialoguePrefix + "start {\\fnFont Name}text{\\fn} end", "start <font face=\"Font Name\">text</font> end")] // font name
|
[InlineData(InvariantDialoguePrefix + "start {\\fnFont Name}text{\\fn} end", "start <font face=\"Font Name\">text</font> end")] // font name
|
||||||
[InlineData(InvariantDialoguePrefix + "start {\\fs10}text{\\fs} end", "start <font size=\"10\">text</font> end")] // font size
|
[InlineData(InvariantDialoguePrefix + "start {\\fs10}text{\\fs} end", "start <font size=\"10\">text</font> end")] // font size
|
||||||
[InlineData(InvariantDialoguePrefix + "start {\\c&H112233}text{\\c} end", "start <font color=\"#332211\">text</font> end")] // color
|
[InlineData(InvariantDialoguePrefix + "start {\\c&H112233}text{\\c} end", "start <font color=\"#332211\">text</font> end")] // color
|
||||||
[InlineData(InvariantDialoguePrefix + "start {\\1c&H112233}text{\\1c} end", "start <font color=\"#332211\">text</font> end")] // primay color
|
// TODO: Fix upstream
|
||||||
[InlineData(InvariantDialoguePrefix + "start {\\fnFont Name}text1 {\\fs10}text2{\\fs}{\\fn} {\\1c&H112233}text3{\\1c} end", "start <font face=\"Font Name\">text1 <font size=\"10\">text2</font></font> <font color=\"#332211\">text3</font> end")] // nested formatting
|
// [InlineData(InvariantDialoguePrefix + "start {\\1c&H112233}text{\\1c} end", "start <font color=\"#332211\">text</font> end")] // primay color
|
||||||
|
// [InlineData(InvariantDialoguePrefix + "start {\\fnFont Name}text1 {\\fs10}text2{\\fs}{\\fn} {\\1c&H112233}text3{\\1c} end", "start <font face=\"Font Name\">text1 <font size=\"10\">text2</font></font> <font color=\"#332211\">text3</font> end")] // nested formatting
|
||||||
public void Parse(string ssa, string expectedText)
|
public void Parse(string ssa, string expectedText)
|
||||||
{
|
{
|
||||||
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
|
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
|
||||||
{
|
{
|
||||||
SubtitleTrackInfo subtitleTrackInfo = parser.Parse(stream, CancellationToken.None);
|
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, CancellationToken.None);
|
||||||
SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[0];
|
SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[0];
|
||||||
Assert.Equal(expectedText, actual.Text);
|
Assert.Equal(expectedText, actual.Text);
|
||||||
}
|
}
|
||||||
@ -43,7 +48,7 @@ namespace Jellyfin.MediaEncoding.Tests
|
|||||||
{
|
{
|
||||||
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
|
using (Stream stream = new MemoryStream(Encoding.UTF8.GetBytes(ssa)))
|
||||||
{
|
{
|
||||||
SubtitleTrackInfo subtitleTrackInfo = parser.Parse(stream, CancellationToken.None);
|
SubtitleTrackInfo subtitleTrackInfo = _parser.Parse(stream, CancellationToken.None);
|
||||||
|
|
||||||
Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
|
Assert.Equal(expectedSubtitleTrackEvents.Count, subtitleTrackInfo.TrackEvents.Count);
|
||||||
|
|
||||||
@ -52,9 +57,10 @@ namespace Jellyfin.MediaEncoding.Tests
|
|||||||
SubtitleTrackEvent expected = expectedSubtitleTrackEvents[i];
|
SubtitleTrackEvent expected = expectedSubtitleTrackEvents[i];
|
||||||
SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[i];
|
SubtitleTrackEvent actual = subtitleTrackInfo.TrackEvents[i];
|
||||||
|
|
||||||
|
Assert.Equal(expected.Id, actual.Id);
|
||||||
|
Assert.Equal(expected.Text, actual.Text);
|
||||||
Assert.Equal(expected.StartPositionTicks, actual.StartPositionTicks);
|
Assert.Equal(expected.StartPositionTicks, actual.StartPositionTicks);
|
||||||
Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks);
|
Assert.Equal(expected.EndPositionTicks, actual.EndPositionTicks);
|
||||||
Assert.Equal(expected.Text, actual.Text);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -71,26 +77,39 @@ namespace Jellyfin.MediaEncoding.Tests
|
|||||||
",
|
",
|
||||||
new List<SubtitleTrackEvent>
|
new List<SubtitleTrackEvent>
|
||||||
{
|
{
|
||||||
new SubtitleTrackEvent
|
new SubtitleTrackEvent("1", "dialogue1")
|
||||||
{
|
{
|
||||||
StartPositionTicks = 11800000,
|
StartPositionTicks = 11800000,
|
||||||
EndPositionTicks = 18500000,
|
EndPositionTicks = 18500000
|
||||||
Text = "dialogue1"
|
|
||||||
},
|
},
|
||||||
new SubtitleTrackEvent
|
new SubtitleTrackEvent("2", "dialogue2")
|
||||||
{
|
{
|
||||||
StartPositionTicks = 21800000,
|
StartPositionTicks = 21800000,
|
||||||
EndPositionTicks = 28500000,
|
EndPositionTicks = 28500000
|
||||||
Text = "dialogue2"
|
|
||||||
},
|
},
|
||||||
new SubtitleTrackEvent
|
new SubtitleTrackEvent("3", "dialogue3")
|
||||||
{
|
{
|
||||||
StartPositionTicks = 31800000,
|
StartPositionTicks = 31800000,
|
||||||
EndPositionTicks = 38500000,
|
EndPositionTicks = 38500000
|
||||||
Text = "dialogue3"
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
[Fact]
|
||||||
|
public void Parse_Valid_Success()
|
||||||
|
{
|
||||||
|
using (var stream = File.OpenRead("Test Data/example.ssa"))
|
||||||
|
{
|
||||||
|
var parsed = _parser.Parse(stream, CancellationToken.None);
|
||||||
|
Assert.Single(parsed.TrackEvents);
|
||||||
|
var trackEvent = parsed.TrackEvents[0];
|
||||||
|
|
||||||
|
Assert.Equal("1", trackEvent.Id);
|
||||||
|
Assert.Equal(TimeSpan.Parse("00:00:01.18", CultureInfo.InvariantCulture).Ticks, trackEvent.StartPositionTicks);
|
||||||
|
Assert.Equal(TimeSpan.Parse("00:00:06.85", CultureInfo.InvariantCulture).Ticks, trackEvent.EndPositionTicks);
|
||||||
|
Assert.Equal("{\\pos(400,570)}Like an angel with pity on nobody", trackEvent.Text);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
20
tests/Jellyfin.MediaEncoding.Tests/Test Data/example.ssa
Normal file
20
tests/Jellyfin.MediaEncoding.Tests/Test Data/example.ssa
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
[Script Info]
|
||||||
|
; This is a Sub Station Alpha v4 script.
|
||||||
|
; For Sub Station Alpha info and downloads,
|
||||||
|
; go to http://www.eswat.demon.co.uk/
|
||||||
|
Title: Neon Genesis Evangelion - Episode 26 (neutral Spanish)
|
||||||
|
Original Script: RoRo
|
||||||
|
Script Updated By: version 2.8.01
|
||||||
|
ScriptType: v4.00
|
||||||
|
Collisions: Normal
|
||||||
|
PlayResY: 600
|
||||||
|
PlayDepth: 0
|
||||||
|
Timer: 100,0000
|
||||||
|
|
||||||
|
[V4 Styles]
|
||||||
|
Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding
|
||||||
|
Style: DefaultVCD, Arial,28,11861244,11861244,11861244,-2147483640,-1,0,1,1,2,2,30,30,30,0,0
|
||||||
|
|
||||||
|
[Events]
|
||||||
|
Format: Marked, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text
|
||||||
|
Dialogue: Marked=0,0:00:01.18,0:00:06.85,DefaultVCD, NTP,0000,0000,0000,,{\pos(400,570)}Like an angel with pity on nobody
|
Loading…
Reference in New Issue
Block a user