aboutsummaryrefslogtreecommitdiff
path: root/MediaBrowser.MediaEncoding
diff options
context:
space:
mode:
authorLuis Miguel Almánzar <ruisu15@gmail.com>2014-05-11 00:23:16 -0400
committerLuis Miguel Almánzar <ruisu15@gmail.com>2014-05-11 00:23:16 -0400
commit8a8e272cb70f572079687d0a19bc4a27884c5b95 (patch)
tree25212dd1539c9ebc7574d06bda123812843455ff /MediaBrowser.MediaEncoding
parentf2237b858ad4defe47d7671cffdf3febeff3ad00 (diff)
implemented SSA Parser
Diffstat (limited to 'MediaBrowser.MediaEncoding')
-rw-r--r--MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs58
1 files changed, 55 insertions, 3 deletions
diff --git a/MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs b/MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs
index ca7e58371..093bf34c1 100644
--- a/MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs
+++ b/MediaBrowser.MediaEncoding/Subtitles/SsaParser.cs
@@ -1,17 +1,69 @@
using System;
using System.Collections.Generic;
+using System.Globalization;
using System.IO;
using System.Linq;
-using System.Text;
-using System.Threading.Tasks;
+using System.Text.RegularExpressions;
namespace MediaBrowser.MediaEncoding.Subtitles
{
public class SsaParser : ISubtitleParser
{
+ private readonly CultureInfo _usCulture = new CultureInfo("en-US");
+
public SubtitleTrackInfo Parse(Stream stream)
{
- throw new NotImplementedException();
+ 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)
+ {
+ 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"]]);
+ subEvent.Text = string.Join(",", sections.Skip(headers["Text"]));
+ subEvent.Text = Regex.Replace(subEvent.Text, "\\{(\\\\[\\w]+\\(?([\\w\\d]+,?)+\\)?)+\\}", string.Empty, RegexOptions.IgnoreCase);
+ subEvent.Text = Regex.Replace(subEvent.Text, @"\\N", "<br />", 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;
}
}
}