jellyfin/MediaBrowser.MediaEncoding/Subtitles/AssParser.cs

120 lines
5.1 KiB
C#
Raw Normal View History

2015-01-17 20:12:02 +00:00
using MediaBrowser.Model.Extensions;
2014-08-31 18:28:20 +00:00
using System;
2014-08-22 02:24:38 +00:00
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Threading;
namespace MediaBrowser.MediaEncoding.Subtitles
{
public class AssParser : ISubtitleParser
{
private readonly CultureInfo _usCulture = new CultureInfo("en-US");
public SubtitleTrackInfo Parse(Stream stream, CancellationToken cancellationToken)
{
var trackInfo = new SubtitleTrackInfo();
var eventIndex = 1;
using (var reader = new StreamReader(stream))
{
string line;
while (reader.ReadLine() != "[Events]")
{}
var headers = ParseFieldHeaders(reader.ReadLine());
while ((line = reader.ReadLine()) != null)
{
cancellationToken.ThrowIfCancellationRequested();
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
if(line.StartsWith("["))
break;
if(string.IsNullOrEmpty(line))
continue;
var subEvent = new SubtitleTrackEvent { Id = eventIndex.ToString(_usCulture) };
eventIndex++;
var sections = line.Substring(10).Split(',');
subEvent.StartPositionTicks = GetTicks(sections[headers["Start"]]);
subEvent.EndPositionTicks = GetTicks(sections[headers["End"]]);
2015-03-21 16:10:02 +00:00
2014-08-22 02:24:38 +00:00
subEvent.Text = string.Join(",", sections.Skip(headers["Text"]));
2015-03-26 04:44:24 +00:00
RemoteNativeFormatting(subEvent);
subEvent.Text = subEvent.Text.Replace("\\n", ParserValues.NewLine, StringComparison.OrdinalIgnoreCase);
2014-08-22 02:24:38 +00:00
subEvent.Text = Regex.Replace(subEvent.Text, @"\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}", string.Empty, RegexOptions.IgnoreCase);
trackInfo.TrackEvents.Add(subEvent);
}
}
return trackInfo;
}
long GetTicks(string time)
{
TimeSpan span;
return TimeSpan.TryParseExact(time, @"h\:mm\:ss\.ff", _usCulture, out span)
? span.Ticks: 0;
}
private Dictionary<string,int> ParseFieldHeaders(string line) {
var fields = line.Substring(8).Split(',').Select(x=>x.Trim()).ToList();
var result = new Dictionary<string, int> {
{"Start", fields.IndexOf("Start")},
{"End", fields.IndexOf("End")},
{"Text", fields.IndexOf("Text")}
};
return result;
}
2015-03-18 21:09:48 +00:00
/// <summary>
/// Credit: https://github.com/SubtitleEdit/subtitleedit/blob/master/src/Logic/SubtitleFormats/AdvancedSubStationAlpha.cs
/// </summary>
2015-03-21 16:10:02 +00:00
private void RemoteNativeFormatting(SubtitleTrackEvent p)
2015-03-18 21:09:48 +00:00
{
2015-03-21 16:10:02 +00:00
int indexOfBegin = p.Text.IndexOf('{');
string pre = string.Empty;
while (indexOfBegin >= 0 && p.Text.IndexOf('}') > indexOfBegin)
2015-03-18 21:09:48 +00:00
{
2015-03-21 16:10:02 +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))
2015-03-18 21:09:48 +00:00
{
2015-03-21 16:10:02 +00:00
pre = s.Substring(0, 6);
2015-03-18 21:09:48 +00:00
}
2015-03-21 16:10:02 +00:00
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('}');
p.Text = p.Text.Remove(indexOfBegin, (indexOfEnd - indexOfBegin) + 1);
indexOfBegin = p.Text.IndexOf('{');
2015-03-18 21:09:48 +00:00
}
2015-03-21 16:10:02 +00:00
p.Text = pre + p.Text;
2015-03-18 21:09:48 +00:00
}
2014-08-22 02:24:38 +00:00
}
}