Use SubtitleEdit to parse subtitles
This commit is contained in:
parent
995b370017
commit
ed8fce2dce
|
@ -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,13 @@
|
||||||
#pragma warning disable CS1591
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||||
using System.Collections.Generic;
|
|
||||||
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");
|
|
||||||
|
|
||||||
/// <inheritdoc />
|
|
||||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
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,13 @@
|
||||||
#pragma warning disable CS1591
|
#nullable enable
|
||||||
|
|
||||||
using System;
|
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||||
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;
|
|
||||||
|
|
||||||
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;
|
|
||||||
|
|
||||||
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
||||||
|
|
||||||
public SrtParser(ILogger 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,13 @@
|
||||||
using System;
|
#nullable enable
|
||||||
using System.Collections.Generic;
|
|
||||||
using System.Globalization;
|
using Nikse.SubtitleEdit.Core.SubtitleFormats;
|
||||||
using System.IO;
|
|
||||||
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 />
|
|
||||||
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
|
||||||
{
|
|
||||||
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('\\');
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
45
MediaBrowser.MediaEncoding/Subtitles/SubtitleEditParser.cs
Normal file
45
MediaBrowser.MediaEncoding/Subtitles/SubtitleEditParser.cs
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#nullable enable
|
||||||
|
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using System.Linq;
|
||||||
|
using System.Threading;
|
||||||
|
using MediaBrowser.Common.Extensions;
|
||||||
|
using MediaBrowser.Model.MediaInfo;
|
||||||
|
using Nikse.SubtitleEdit.Core;
|
||||||
|
|
||||||
|
namespace MediaBrowser.MediaEncoding.Subtitles
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// SubStation Alpha subtitle parser.
|
||||||
|
/// </summary>
|
||||||
|
/// <typeparam name="T">The <see cref="Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat" />.</typeparam>
|
||||||
|
public abstract class SubtitleEditParser<T> : ISubtitleParser
|
||||||
|
where T : Nikse.SubtitleEdit.Core.SubtitleFormats.SubtitleFormat, new()
|
||||||
|
{
|
||||||
|
/// <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");
|
||||||
|
|
||||||
|
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;
|
||||||
|
@ -274,7 +271,7 @@ namespace MediaBrowser.MediaEncoding.Subtitles
|
||||||
|
|
||||||
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(format, SubtitleFormat.SRT, StringComparison.OrdinalIgnoreCase))
|
||||||
{
|
{
|
||||||
return new SrtParser(_logger);
|
return new SrtParser();
|
||||||
}
|
}
|
||||||
|
|
||||||
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
|
if (string.Equals(format, SubtitleFormat.SSA, StringComparison.OrdinalIgnoreCase))
|
||||||
|
|
|
@ -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;
|
||||||
|
|
|
@ -21,18 +21,8 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
||||||
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\nThe 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"]);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ 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
|
||||||
|
@ -15,14 +14,14 @@ namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
||||||
{
|
{
|
||||||
using (var stream = File.OpenRead("Test Data/example.srt"))
|
using (var stream = File.OpenRead("Test Data/example.srt"))
|
||||||
{
|
{
|
||||||
var parsed = new SrtParser(new NullLogger<SrtParser>()).Parse(stream, CancellationToken.None);
|
var parsed = new SrtParser().Parse(stream, CancellationToken.None);
|
||||||
Assert.Equal(2, parsed.TrackEvents.Count);
|
Assert.Equal(2, parsed.TrackEvents.Count);
|
||||||
|
|
||||||
var trackEvent1 = parsed.TrackEvents[0];
|
var trackEvent1 = parsed.TrackEvents[0];
|
||||||
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\nour 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);
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
using System;
|
||||||
|
using System.Globalization;
|
||||||
|
using System.IO;
|
||||||
|
using System.Threading;
|
||||||
|
using MediaBrowser.MediaEncoding.Subtitles;
|
||||||
|
using Xunit;
|
||||||
|
|
||||||
|
namespace Jellyfin.MediaEncoding.Subtitles.Tests
|
||||||
|
{
|
||||||
|
public class SsaParserTests
|
||||||
|
{
|
||||||
|
[Fact]
|
||||||
|
public void Parse_Valid_Success()
|
||||||
|
{
|
||||||
|
using (var stream = File.OpenRead("Test Data/example.ssa"))
|
||||||
|
{
|
||||||
|
var parsed = new SsaParser().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