From f2237b858ad4defe47d7671cffdf3febeff3ad00 Mon Sep 17 00:00:00 2001 From: Luis Miguel Almánzar Date: Sat, 10 May 2014 22:29:34 -0400 Subject: implemented SRT Parser --- .../MediaEncoding/Subtitles/SrtParserTests.cs | 108 +++++++++++++++++++++ .../MediaEncoding/Subtitles/TestSubtitles/unit.srt | 44 +++++++++ 2 files changed, 152 insertions(+) create mode 100644 MediaBrowser.Tests/MediaEncoding/Subtitles/SrtParserTests.cs create mode 100644 MediaBrowser.Tests/MediaEncoding/Subtitles/TestSubtitles/unit.srt (limited to 'MediaBrowser.Tests/MediaEncoding') diff --git a/MediaBrowser.Tests/MediaEncoding/Subtitles/SrtParserTests.cs b/MediaBrowser.Tests/MediaEncoding/Subtitles/SrtParserTests.cs new file mode 100644 index 000000000..0d86fbdcd --- /dev/null +++ b/MediaBrowser.Tests/MediaEncoding/Subtitles/SrtParserTests.cs @@ -0,0 +1,108 @@ +using System; +using System.Collections.Generic; +using System.IO; +using MediaBrowser.MediaEncoding.Subtitles; +using Microsoft.VisualStudio.TestTools.UnitTesting; + +namespace MediaBrowser.Tests.MediaEncoding.Subtitles { + + [TestClass] + public class SrtParserTests { + + [TestMethod] + public void TestParse() { + + var expectedSubs = + new SubtitleTrackInfo { + TrackEvents = new List { + new SubtitleTrackEvent { + Id = "1", + StartPositionTicks = 24000000, + EndPositionTicks = 52000000, + Text = + "[Background Music Playing]" + }, + new SubtitleTrackEvent { + Id = "2", + StartPositionTicks = 157120000, + EndPositionTicks = 173990000, + Text = + "Oh my god, Watch out!
It's coming!!" + }, + new SubtitleTrackEvent { + Id = "3", + StartPositionTicks = 257120000, + EndPositionTicks = 303990000, + Text = "[Bird noises]" + }, + new SubtitleTrackEvent { + Id = "4", + StartPositionTicks = 310000000, + EndPositionTicks = 319990000, + Text = + "This text is RED and has not been positioned." + }, + new SubtitleTrackEvent { + Id = "5", + StartPositionTicks = 320000000, + EndPositionTicks = 329990000, + Text = + "This is a
new line, as is
this" + }, + new SubtitleTrackEvent { + Id = "6", + StartPositionTicks = 330000000, + EndPositionTicks = 339990000, + Text = + "This contains nested bold, italic, underline and strike-through HTML tags" + }, + new SubtitleTrackEvent { + Id = "7", + StartPositionTicks = 340000000, + EndPositionTicks = 349990000, + Text = + "Unclosed but supported HTML tags are left in, SSA italics aren't" + }, + new SubtitleTrackEvent { + Id = "8", + StartPositionTicks = 350000000, + EndPositionTicks = 359990000, + Text = + "<ggg>Unsupported</ggg> HTML tags are escaped and left in, even if <hhh>not closed." + }, + new SubtitleTrackEvent { + Id = "9", + StartPositionTicks = 360000000, + EndPositionTicks = 369990000, + Text = + "Multiple SSA tags are stripped" + }, + new SubtitleTrackEvent { + Id = "10", + StartPositionTicks = 370000000, + EndPositionTicks = 379990000, + Text = + "Greater than (<) and less than (>) are shown" + } + } + }; + + var sut = new SrtParser(); + + var stream = File.OpenRead(@"MediaEncoding\Subtitles\TestSubtitles\unit.srt"); + + var result = sut.Parse(stream); + + Assert.IsNotNull(result); + Assert.AreEqual(expectedSubs.TrackEvents.Count,result.TrackEvents.Count); + for (int i = 0; i < expectedSubs.TrackEvents.Count; i++) + { + Assert.AreEqual(expectedSubs.TrackEvents[i].Id, result.TrackEvents[i].Id); + Assert.AreEqual(expectedSubs.TrackEvents[i].StartPositionTicks, result.TrackEvents[i].StartPositionTicks); + Assert.AreEqual(expectedSubs.TrackEvents[i].EndPositionTicks, result.TrackEvents[i].EndPositionTicks); + Assert.AreEqual(expectedSubs.TrackEvents[i].Text, result.TrackEvents[i].Text); + } + + } + } +} \ No newline at end of file diff --git a/MediaBrowser.Tests/MediaEncoding/Subtitles/TestSubtitles/unit.srt b/MediaBrowser.Tests/MediaEncoding/Subtitles/TestSubtitles/unit.srt new file mode 100644 index 000000000..5f6e5636e --- /dev/null +++ b/MediaBrowser.Tests/MediaEncoding/Subtitles/TestSubtitles/unit.srt @@ -0,0 +1,44 @@ + + +1 +00:00:02.400 --> 00:00:05.200 +[Background Music Playing] + +2 +00:00:15,712 --> 00:00:17,399 X1:000 X2:000 Y1:050 Y2:100 +Oh my god, Watch out! +It's coming!! + +3 +00:00:25,712 --> 00:00:30,399 +[Bird noises] + +4 +00:00:31,000 --> 00:00:31,999 +This text is RED and has not been {\pos(142,120)}positioned. + +5 +00:00:32,000 --> 00:00:32,999 +This is a\nnew line, as is\Nthis + +6 +00:00:33,000 --> 00:00:33,999 +This contains nested bold, italic, underline and strike-through HTML tags + +7 +00:00:34,000 --> 00:00:34,999 +Unclosed but supported HTML tags are left in, {\i1} SSA italics aren't + +8 +00:00:35,000 --> 00:00:35,999 +Unsupported HTML tags are escaped and left in, even if not closed. + +9 +00:00:36,000 --> 00:00:36,999 +Multiple {\pos(142,120)\b1}SSA tags are stripped + +10 +00:00:37,000 --> 00:00:37,999 +Greater than (<) and less than (>) are shown + + -- cgit v1.2.3