2020-08-04 14:20:52 +00:00
|
|
|
#pragma warning disable CS1591
|
|
|
|
|
2019-01-13 20:02:23 +00:00
|
|
|
using System;
|
2018-12-14 09:40:55 +00:00
|
|
|
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
|
|
|
|
{
|
|
|
|
public class AssParser : ISubtitleParser
|
|
|
|
{
|
|
|
|
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
|
|
|
|
|
2020-08-04 14:20:52 +00:00
|
|
|
/// <inheritdoc />
|
2018-12-14 09:40:55 +00:00
|
|
|
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
|
|
|
|
{
|
|
|
|
var trackInfo = new SubtitleTrackInfo();
|
2019-01-13 20:37:13 +00:00
|
|
|
var trackEvents = new List<SubtitleTrackEvent>();
|
2018-12-14 09:40:55 +00:00
|
|
|
var eventIndex = 1;
|
|
|
|
using (var reader = new StreamReader(stream))
|
|
|
|
{
|
|
|
|
string line;
|
2020-12-28 14:33:15 +00:00
|
|
|
while (!string.Equals(reader.ReadLine(), "[Events]", StringComparison.Ordinal))
|
2020-08-04 15:08:09 +00:00
|
|
|
{
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
var headers = ParseFieldHeaders(reader.ReadLine());
|
|
|
|
|
|
|
|
while ((line = reader.ReadLine()) != null)
|
|
|
|
{
|
|
|
|
cancellationToken.ThrowIfCancellationRequested();
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
if (string.IsNullOrWhiteSpace(line))
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
2020-05-25 21:52:51 +00:00
|
|
|
|
2020-07-22 11:34:51 +00:00
|
|
|
if (line[0] == '[')
|
2020-05-25 21:52:51 +00:00
|
|
|
{
|
2018-12-14 09:40:55 +00:00
|
|
|
break;
|
2020-05-25 21:52:51 +00:00
|
|
|
}
|
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
var subEvent = new SubtitleTrackEvent { Id = eventIndex.ToString(_usCulture) };
|
|
|
|
eventIndex++;
|
2020-12-28 14:33:15 +00:00
|
|
|
const string Dialogue = "Dialogue: ";
|
|
|
|
var sections = line.Substring(Dialogue.Length).Split(',');
|
2018-12-14 09:40:55 +00:00
|
|
|
|
|
|
|
subEvent.StartPositionTicks = GetTicks(sections[headers["Start"]]);
|
|
|
|
subEvent.EndPositionTicks = GetTicks(sections[headers["End"]]);
|
|
|
|
|
2020-12-28 14:33:15 +00:00
|
|
|
subEvent.Text = string.Join(',', sections[headers["Text"]..]);
|
2018-12-14 09:40:55 +00:00
|
|
|
RemoteNativeFormatting(subEvent);
|
|
|
|
|
|
|
|
subEvent.Text = subEvent.Text.Replace("\\n", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
|
2019-01-07 23:27:46 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
subEvent.Text = Regex.Replace(subEvent.Text, @"\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}", string.Empty, RegexOptions.IgnoreCase);
|
|
|
|
|
|
|
|
trackEvents.Add(subEvent);
|
|
|
|
}
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2020-12-28 14:33:15 +00:00
|
|
|
trackInfo.TrackEvents = trackEvents;
|
2018-12-14 09:40:55 +00:00
|
|
|
return trackInfo;
|
|
|
|
}
|
|
|
|
|
2020-07-22 11:34:51 +00:00
|
|
|
private long GetTicks(ReadOnlySpan<char> time)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
2019-01-13 20:46:33 +00:00
|
|
|
return TimeSpan.TryParseExact(time, @"h\:mm\:ss\.ff", _usCulture, out var span)
|
2019-01-13 19:26:04 +00:00
|
|
|
? span.Ticks : 0;
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
2020-12-28 14:33:15 +00:00
|
|
|
internal static Dictionary<string, int> ParseFieldHeaders(string line)
|
2019-01-13 19:26:04 +00:00
|
|
|
{
|
2020-12-28 14:33:15 +00:00
|
|
|
const string Format = "Format: ";
|
|
|
|
var fields = line.Substring(Format.Length).Split(',').Select(x => x.Trim()).ToList();
|
2018-12-14 09:40:55 +00:00
|
|
|
|
2020-08-04 15:08:09 +00:00
|
|
|
return new Dictionary<string, int>
|
|
|
|
{
|
|
|
|
{ "Start", fields.IndexOf("Start") },
|
|
|
|
{ "End", fields.IndexOf("End") },
|
|
|
|
{ "Text", fields.IndexOf("Text") }
|
|
|
|
};
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
private void RemoteNativeFormatting(SubtitleTrackEvent p)
|
|
|
|
{
|
2020-08-20 10:16:24 +00:00
|
|
|
int indexOfBegin = p.Text.IndexOf('{', StringComparison.Ordinal);
|
2018-12-14 09:40:55 +00:00
|
|
|
string pre = string.Empty;
|
2020-08-20 10:16:24 +00:00
|
|
|
while (indexOfBegin >= 0 && p.Text.IndexOf('}', StringComparison.Ordinal) > indexOfBegin)
|
2018-12-14 09:40:55 +00:00
|
|
|
{
|
|
|
|
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) + "}";
|
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2020-08-20 10:16:24 +00:00
|
|
|
int indexOfEnd = p.Text.IndexOf('}', StringComparison.Ordinal);
|
2018-12-14 09:40:55 +00:00
|
|
|
p.Text = p.Text.Remove(indexOfBegin, (indexOfEnd - indexOfBegin) + 1);
|
|
|
|
|
2020-08-20 10:16:24 +00:00
|
|
|
indexOfBegin = p.Text.IndexOf('{', StringComparison.Ordinal);
|
2018-12-14 09:40:55 +00:00
|
|
|
}
|
2020-06-15 21:43:52 +00:00
|
|
|
|
2018-12-14 09:40:55 +00:00
|
|
|
p.Text = pre + p.Text;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|