From 4130b45a66c354a7f9df8d966a5b32218a6dfb38 Mon Sep 17 00:00:00 2001 From: stevenaw Date: Thu, 3 Feb 2011 16:38:35 -0500 Subject: [PATCH] [#290] Remake branch --- parsers/parserSRT/data/data.srt | 16 +++ parsers/parserSRT/data/unit.srt | 40 ++++++ parsers/parserSRT/popcorn.parserSRT.html | 56 +++++++++ parsers/parserSRT/popcorn.parserSRT.js | 119 ++++++++++++++++++ parsers/parserSRT/popcorn.parserSRT.unit.html | 44 +++++++ parsers/parserSRT/popcorn.parserSRT.unit.js | 108 ++++++++++++++++ 6 files changed, 383 insertions(+) create mode 100644 parsers/parserSRT/data/data.srt create mode 100644 parsers/parserSRT/data/unit.srt create mode 100644 parsers/parserSRT/popcorn.parserSRT.html create mode 100644 parsers/parserSRT/popcorn.parserSRT.js create mode 100644 parsers/parserSRT/popcorn.parserSRT.unit.html create mode 100644 parsers/parserSRT/popcorn.parserSRT.unit.js diff --git a/parsers/parserSRT/data/data.srt b/parsers/parserSRT/data/data.srt new file mode 100644 index 0000000..d9f4945 --- /dev/null +++ b/parsers/parserSRT/data/data.srt @@ -0,0 +1,16 @@ +1 +00:00:02.400 --> 00:00:05.200 +[Background Music Playing] + +2 +00:00:06.000 --> 00:00:13,700 + To say that 2 is greater than three (3 > 2) would be lying + +3 +00:00:15,712 --> 00:00:17,399 X1:000 X2:000 Y1:050 Y2:100 +Oh my god, Watch out! +It's coming!! + +4 +00:00:25,712 --> 00:00:30,399 +[Bird noises] diff --git a/parsers/parserSRT/data/unit.srt b/parsers/parserSRT/data/unit.srt new file mode 100644 index 0000000..093aa25 --- /dev/null +++ b/parsers/parserSRT/data/unit.srt @@ -0,0 +1,40 @@ +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 diff --git a/parsers/parserSRT/popcorn.parserSRT.html b/parsers/parserSRT/popcorn.parserSRT.html new file mode 100644 index 0000000..3aa8575 --- /dev/null +++ b/parsers/parserSRT/popcorn.parserSRT.html @@ -0,0 +1,56 @@ + + + + Popcorn 0.3 SRT parser Plug-in Demo + + + + + + + + +

Popcorn 0.3 SRT parser Plug-in Demo

+ +

From 2.4 to 5.2 seconds, "[Background Music Playing]" is shown +
From 6 to 13.7 seconds, "<true> To say that 2 is greater than three (3 > 2) would be lying<true>" is shown +
From 15.712 to 17.399 seconds, "Oh my god, Watch out! It's coming!!" is shown +
From 25.712 to 30.399 seconds, "[Bird noises]" is shown

+
+ +
+ +

Subtitle Source

+ + + + + + diff --git a/parsers/parserSRT/popcorn.parserSRT.js b/parsers/parserSRT/popcorn.parserSRT.js new file mode 100644 index 0000000..2ab5f83 --- /dev/null +++ b/parsers/parserSRT/popcorn.parserSRT.js @@ -0,0 +1,119 @@ +// PARSER: 0.3 SRT + +(function (Popcorn) { + /** + * SRT popcorn parser plug-in + * Parses subtitle files in the SRT format. + * Times are expected in HH:MM:SS,MIL format, though HH:MM:SS.MIL also supported + * Ignore styling, which may occur after the end time or in-text + * While not part of the "official" spec, majority of players support HTML and SSA styling tags + * SSA-style tags are stripped, HTML style tags are left for the browser to handle: + * HTML: , , , , + * SSA: \N or \n, {\cmdArg1}, {\cmd(arg1, arg2, ...)} + + * Data parameter is given by Popcorn, will need a text. + * Text is the file contents to be parsed + * + * @param {Object} data + * + * Example: + 1 + 00:00:25,712 --> 00:00:30.399 + This text is RED and has not been {\pos(142,120)} positioned. + This takes \Nup three \nentire lines. + This contains nested bold, italic, underline and strike-through HTML tags + Unclosed but supported tags are left in + Unsupported HTML tags are left in, even if not closed. + SSA tags with {\i1} would open and close italicize {\i0}, but are stripped + Multiple {\pos(142,120)\b1}SSA tags are stripped + */ + Popcorn.parser( "parseSRT", "SRT", function( data ) { + + // declare needed variables + var retObj = { + title: "", + remote: "", + data: [] + }, + subs = [], + i = 0, + len = 0, + idx = 0, + lines, + time, + text, + sub; + + // Simple function to convert HH:MM:SS,MMM or HH:MM:SS.MMM to SS.MMM + // Assume valid, returns 0 on error + var toSeconds = function( t_in ) { + var t = t_in.split( ':' ); + + try { + var s = t[2].split( ',' ); + + // Just in case a . is decimal seperator + if ( s.length === 1 ) { + s = t[2].split( '.' ); + } + + return parseFloat( t[0], 10 )*3600 + parseFloat( t[1], 10 )*60 + parseFloat( s[0], 10 ) + parseFloat( s[1], 10 )/1000; + } catch ( e ) { + return 0; + } + }; + + var createTrack = function( name, attributes ) { + var track = {}; + track[name] = attributes; + return track; + }; + + // Here is where the magic happens + // Split on line breaks + lines = data.text.split( /(?:\r\n|\r|\n)/gm ); + len = lines.length; + + for( i=0; i < len; i++ ) { + sub = {}; + text = []; + + sub.id = parseInt( lines[i++], 10 ); + + // Split on '-->' delimiter, trimming spaces as well + time = lines[i++].split( /[\t ]*-->[\t ]*/ ); + + sub.start = toSeconds( time[0] ); + + // So as to trim positioning information from end + idx = time[1].indexOf( " " ); + if ( idx !== -1) { + time[1] = time[1].substr( 0, idx ); + } + sub.end = toSeconds( time[1] ); + + // Build single line of text from multi-line subtitle in file + while ( i < len && lines[i] ) { + text.push( lines[i++] ); + } + + // Join into 1 line, SSA-style linebreaks + // Strip out other SSA-style tags + sub.text = text.join( "\\N" ).replace( /\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}/gi, "" ); + + // Escape HTML entities + sub.text = sub.text.replace( //g, ">" ); + + // Unescape great than and less than when it makes a valid html tag of a supported style (font, b, u, s, i) + // Modified version of regex from Phil Haack's blog: http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx + // Later modified by kev: http://kevin.deldycke.com/2007/03/ultimate-regular-expression-for-html-tag-parsing-with-php/ + sub.text = sub.text.replace( /<(\/?(font|b|u|i|s))((\s+(\w|\w[\w\-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)(\/?)>/gi, "<$1$3$7>" ); + sub.text = sub.text.replace( /\\N/gi, "
" ); + subs.push( createTrack( "subtitle", sub ) ); + } + + retObj.data = subs; + return retObj; + }); + +})( Popcorn ); diff --git a/parsers/parserSRT/popcorn.parserSRT.unit.html b/parsers/parserSRT/popcorn.parserSRT.unit.html new file mode 100644 index 0000000..572a9f4 --- /dev/null +++ b/parsers/parserSRT/popcorn.parserSRT.unit.html @@ -0,0 +1,44 @@ + + + + Popcorn 0.3 SRT parser Plug-in Test + + + + + + + + + + + +

Popcorn 0.3 SRT parser Plug-in Test

+

+
+

+
    +
    + + + + \ No newline at end of file diff --git a/parsers/parserSRT/popcorn.parserSRT.unit.js b/parsers/parserSRT/popcorn.parserSRT.unit.js new file mode 100644 index 0000000..11474e1 --- /dev/null +++ b/parsers/parserSRT/popcorn.parserSRT.unit.js @@ -0,0 +1,108 @@ +test("Popcorn 0.3 SRT Parser Plugin", function () { + + var expects = 0, + count = 0, + sub = {}, + numSubs = 0, + poppercorn = Popcorn( "#video" ), + expectedSubs = [ + { + id: 1, + start: 2.4, + end: 5.2, + text: "[Background Music Playing]" + }, + { + id: 2, + start: 15.712, + end: 17.399, + text: "Oh my god, Watch out!
    It's coming!!" + }, + { + id: 3, + start: 25.712, + end: 30.399, + text: "[Bird noises]" + }, + { + id: 4, + start: 31, + end: 31.999, + text: 'This text is RED and has not been positioned.' + }, + { + id: 5, + start: 32, + end: 32.999, + text: "This is a
    new line, as is
    this" + }, + { + id: 6, + start: 33, + end: 33.999, + text: "This contains nested bold, italic, underline and strike-through HTML tags" + }, + { + id: 7, + start: 34, + end: 34.999, + text: "Unclosed but supported HTML tags are left in, SSA italics aren't" + }, + { + id: 8, + start: 35, + end: 35.999, + text: "<ggg>Unsupported</ggg> HTML tags are escaped and left in, even if <hhh>not closed." + }, + { + id: 9, + start: 36, + end: 36.999, + text: "Multiple SSA tags are stripped" + }, + { + id: 10, + start: 37, + end: 37.999, + text: "Greater than (<) and less than (>) are shown" + } + ]; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + poppercorn.parseSRT( document.getElementById( 'video' ).getAttribute( 'data-timeline-sources' ) ); + + expects = expectedSubs.length*4+1; + expect( expects ); + stop( 5000 ); + + // Allow load time + setTimeout(function () { + Popcorn.forEach( poppercorn.getTrackEvents(), function( evt ) { + if( evt._natives.type === "subtitle" ) { + sub = expectedSubs[numSubs++]; + + equals( sub.id, evt.id, "Correct id" ); + plus(); + + equals( sub.start, evt.start, "Correct start" ); + plus(); + + equals( sub.end, evt.end, "Correct end" ); + plus(); + + equals( sub.text, evt.text, "Correct text" ); + plus(); + } + }); + + equals( expectedSubs.length, numSubs, "Correctly parsed all subtitles" ); + plus(); + + }, 500); + +}); \ No newline at end of file -- 2.43.0