From c1dad9b030e4ea94c2c5c21078b3d8a56478c20f Mon Sep 17 00:00:00 2001 From: stevenaw Date: Tue, 8 Feb 2011 14:50:04 -0500 Subject: [PATCH] [#296] Branch remade for SBV, SSA. TTML Parsing complete --- parsers/parserSBV/data/data.sbv | 8 + parsers/parserSBV/data/unit.sbv | 8 + parsers/parserSBV/popcorn.parseSBV.html | 48 +++++ parsers/parserSBV/popcorn.parseSBV.js | 100 ++++++++++ parsers/parserSBV/popcorn.parseSBV.unit.html | 44 +++++ parsers/parserSBV/popcorn.parseSBV.unit.js | 58 ++++++ parsers/parserSSA/data/data.ssa | 23 +++ parsers/parserSSA/data/unit.ssa | 23 +++ parsers/parserSSA/popcorn.parseSSA.html | 46 +++++ parsers/parserSSA/popcorn.parseSSA.js | 132 ++++++++++++++ parsers/parserSSA/popcorn.parseSSA.unit.html | 43 +++++ parsers/parserSSA/popcorn.parseSSA.unit.js | 57 ++++++ parsers/parserTTML/data/data.ttml | 75 ++++++++ parsers/parserTTML/data/unit.ttml | 75 ++++++++ parsers/parserTTML/popcorn.parseTTML.html | 54 ++++++ parsers/parserTTML/popcorn.parseTTML.js | 171 ++++++++++++++++++ .../parserTTML/popcorn.parseTTML.unit.html | 43 +++++ parsers/parserTTML/popcorn.parseTTML.unit.js | 103 +++++++++++ 18 files changed, 1111 insertions(+) create mode 100644 parsers/parserSBV/data/data.sbv create mode 100644 parsers/parserSBV/data/unit.sbv create mode 100644 parsers/parserSBV/popcorn.parseSBV.html create mode 100644 parsers/parserSBV/popcorn.parseSBV.js create mode 100644 parsers/parserSBV/popcorn.parseSBV.unit.html create mode 100644 parsers/parserSBV/popcorn.parseSBV.unit.js create mode 100644 parsers/parserSSA/data/data.ssa create mode 100644 parsers/parserSSA/data/unit.ssa create mode 100644 parsers/parserSSA/popcorn.parseSSA.html create mode 100644 parsers/parserSSA/popcorn.parseSSA.js create mode 100644 parsers/parserSSA/popcorn.parseSSA.unit.html create mode 100644 parsers/parserSSA/popcorn.parseSSA.unit.js create mode 100644 parsers/parserTTML/data/data.ttml create mode 100644 parsers/parserTTML/data/unit.ttml create mode 100644 parsers/parserTTML/popcorn.parseTTML.html create mode 100644 parsers/parserTTML/popcorn.parseTTML.js create mode 100644 parsers/parserTTML/popcorn.parseTTML.unit.html create mode 100644 parsers/parserTTML/popcorn.parseTTML.unit.js diff --git a/parsers/parserSBV/data/data.sbv b/parsers/parserSBV/data/data.sbv new file mode 100644 index 0000000..3b76ff7 --- /dev/null +++ b/parsers/parserSBV/data/data.sbv @@ -0,0 +1,8 @@ +0:00:02.400,0:00:07.200 +Senator, we're making our final approach into Coruscant. + +0:00:09.712,0:00:13.399 +Very good, Lieutenant. + +0:00:15.042,0:00:18.042 +It's a trap! diff --git a/parsers/parserSBV/data/unit.sbv b/parsers/parserSBV/data/unit.sbv new file mode 100644 index 0000000..3b76ff7 --- /dev/null +++ b/parsers/parserSBV/data/unit.sbv @@ -0,0 +1,8 @@ +0:00:02.400,0:00:07.200 +Senator, we're making our final approach into Coruscant. + +0:00:09.712,0:00:13.399 +Very good, Lieutenant. + +0:00:15.042,0:00:18.042 +It's a trap! diff --git a/parsers/parserSBV/popcorn.parseSBV.html b/parsers/parserSBV/popcorn.parseSBV.html new file mode 100644 index 0000000..a2abdb7 --- /dev/null +++ b/parsers/parserSBV/popcorn.parseSBV.html @@ -0,0 +1,48 @@ + + + + Popcorn 0.3 SBV parser Plug-in Demo + + + + + + + + + +

Popcorn 0.3 SBV parser Plug-in Demo

+

From 2.4 to 7.2 seconds, "Senator, we're making our final approach into Coruscant." is shown +
From 9.712 to 13.399 seconds, "Very good, Lieutenant." is shown +
From 15.042 to 18.042 seconds, "It's a trap!" is shown

+
+
+ +
+ +

Subtitle Source

+ + + diff --git a/parsers/parserSBV/popcorn.parseSBV.js b/parsers/parserSBV/popcorn.parseSBV.js new file mode 100644 index 0000000..364fba8 --- /dev/null +++ b/parsers/parserSBV/popcorn.parseSBV.js @@ -0,0 +1,100 @@ +// PARSER: 0.1 SBV + +(function (Popcorn) { + + /** + * SBV popcorn parser plug-in + * Parses subtitle files in the SBV format. + * Times are expected in H:MM:SS.MIL format, with hours optional + * Subtitles which don't match expected format are ignored + * Data parameter is given by Popcorn, will need a text. + * Text is the file contents to be parsed + * + * @param {Object} data + * + * Example: + 0:00:02.400,0:00:07.200 + Senator, we're making our final approach into Coruscant. + */ + Popcorn.parser( "parseSBV", "SBV", function( data ) { + + // declare needed variables + var retObj = { + title: "", + remote: "", + data: [] + }, + subs = [], + lines, + i = 0, + len = 0, + idx = 0; + + // [H:]MM:SS.MIL string to SS.MIL + // Will thrown exception on bad time format + var toSeconds = function( t_in ) { + var t = t_in.split( ":" ), + l = t.length-1, + time; + + try { + time = parseInt( t[l-1], 10 )*60 + parseFloat( t[l], 10 ); + + // Hours optionally given + if ( l === 2 ) { + time += parseInt( t[0], 10 )*3600; + } + } catch ( e ) { + throw "Bad cue"; + } + + return time; + }; + + 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; + + while ( i < len ) { + var sub = {}, + text = [], + time = lines[i++].split( "," ); + + try { + sub.start = toSeconds( time[0] ); + sub.end = toSeconds( time[1] ); + + // Gather all lines of text + while ( i < len && lines[i] ) { + text.push( lines[i++] ); + } + + // Join line breaks in text + sub.text = text.join( "
" ); + subs.push( createTrack( "subtitle", sub ) ); + } catch ( e ) { + // Bad cue, advance to end of cue + while ( i < len && lines[i] ) { + i++; + } + } + + // Consume empty whitespace + while ( i < len && !lines[i] ) { + i++; + } + } + + retObj.data = subs; + + return retObj; + }); + +})( Popcorn ); diff --git a/parsers/parserSBV/popcorn.parseSBV.unit.html b/parsers/parserSBV/popcorn.parseSBV.unit.html new file mode 100644 index 0000000..c9966ab --- /dev/null +++ b/parsers/parserSBV/popcorn.parseSBV.unit.html @@ -0,0 +1,44 @@ + + + + Popcorn 0.3 SBV parser Plug-in Test + + + + + + + + + + + +

Popcorn 0.3 SBV parser Plug-in Test

+

+
+

+
    +
    + + + + diff --git a/parsers/parserSBV/popcorn.parseSBV.unit.js b/parsers/parserSBV/popcorn.parseSBV.unit.js new file mode 100644 index 0000000..8ce17b6 --- /dev/null +++ b/parsers/parserSBV/popcorn.parseSBV.unit.js @@ -0,0 +1,58 @@ +test( "Popcorn 0.3 SBV Parser Plugin", function () { + + var count = 0, + numSubs = 0, + sub, + poppercorn = Popcorn( "#video" ), + subs = [ + { + text: "Senator, we're making our final approach into Coruscant.", + start: 2.4, + end: 7.2 + }, + { + text: "Very good, Lieutenant.", + start: 9.712, + end: 13.399 + }, + { + text: "It's a trap!", + start: 15.042, + end: 18.042 + } + ], + expects = subs.length*3 + 1; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + poppercorn.parseSBV( document.getElementById( "video" ).getAttribute( "data-timeline-sources" ) ); + + expect(expects); + + stop( 5000 ); + + // Allow load time + setTimeout(function () { + Popcorn.forEach( poppercorn.getTrackEvents(), function(evt) { + if( evt._natives.type === "subtitle" ) { + sub = subs[numSubs++]; + + equals( evt.start, sub.start, "Correctly parsed start of " + evt.start ); + plus(); + equals( evt.text, sub.text, "Correctly parsed text of " + evt.start ); + plus(); + equals( evt.end, sub.end, "Correctly parsed end at " + evt.start ); + plus(); + } + }); + + equals( subs.length, numSubs, "Parsed all subtitles" ); + plus(); + + }, 500); + +}); diff --git a/parsers/parserSSA/data/data.ssa b/parsers/parserSSA/data/data.ssa new file mode 100644 index 0000000..3114a84 --- /dev/null +++ b/parsers/parserSSA/data/data.ssa @@ -0,0 +1,23 @@ +[Script Info] +Title: Testing subtitles for the SSA Format + +[V4 Styles] +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding +Style: Default,Arial,20,65535,65535,65535,-2147483640,-1,0,1,3,0,2,30,30,30,0,0 +Style: Titre_episode,Akbar,140,15724527,65535,65535,986895,-1,0,1,1,0,3,30,30,30,0,0 +Style: Wolf main,Wolf_Rain,56,15724527,15724527,15724527,4144959,0,0,1,1,2,2,5,5,30,0,0 + + + +[Events] +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text +Dialogue: 0,0:00:02.40,0:00:07.20,Default,,0000,0000,0000,,Senator, {\kf89}we're \Nmaking our final \napproach into Coruscant. +Dialogue: 0,0:00:09.71,0:00:13.39,Default,,0000,0000,0000,,{\pos(400,570)}Very good, Lieutenant. +Dialogue: 0,0:00:15.04,0:00:18.04,Default,,0000,0000,0000,,It's \Na \ntrap! + + +[Pictures] +This section will be ignored + +[Fonts] +This section will be ignored \ No newline at end of file diff --git a/parsers/parserSSA/data/unit.ssa b/parsers/parserSSA/data/unit.ssa new file mode 100644 index 0000000..3114a84 --- /dev/null +++ b/parsers/parserSSA/data/unit.ssa @@ -0,0 +1,23 @@ +[Script Info] +Title: Testing subtitles for the SSA Format + +[V4 Styles] +Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding +Style: Default,Arial,20,65535,65535,65535,-2147483640,-1,0,1,3,0,2,30,30,30,0,0 +Style: Titre_episode,Akbar,140,15724527,65535,65535,986895,-1,0,1,1,0,3,30,30,30,0,0 +Style: Wolf main,Wolf_Rain,56,15724527,15724527,15724527,4144959,0,0,1,1,2,2,5,5,30,0,0 + + + +[Events] +Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text +Dialogue: 0,0:00:02.40,0:00:07.20,Default,,0000,0000,0000,,Senator, {\kf89}we're \Nmaking our final \napproach into Coruscant. +Dialogue: 0,0:00:09.71,0:00:13.39,Default,,0000,0000,0000,,{\pos(400,570)}Very good, Lieutenant. +Dialogue: 0,0:00:15.04,0:00:18.04,Default,,0000,0000,0000,,It's \Na \ntrap! + + +[Pictures] +This section will be ignored + +[Fonts] +This section will be ignored \ No newline at end of file diff --git a/parsers/parserSSA/popcorn.parseSSA.html b/parsers/parserSSA/popcorn.parseSSA.html new file mode 100644 index 0000000..183686f --- /dev/null +++ b/parsers/parserSSA/popcorn.parseSSA.html @@ -0,0 +1,46 @@ + + + + Popcorn 0.3 SSA/ASS parser Plug-in Demo + + + + + + + + + +

    Popcorn 0.3 SSA/ASS parser Plug-in Demo

    +

    From 2.4 to 7.2 seconds, "Senator, we're making our final approach into Coruscant." is shown +
    From 9.71 to 13.39 seconds, "Very good, Lieutenant." is shown +
    From 15.04 to 18.04 seconds, "It's a trap!" is shown

    +
    + +
    \ +

    Subtitle Source

    + + + diff --git a/parsers/parserSSA/popcorn.parseSSA.js b/parsers/parserSSA/popcorn.parseSSA.js new file mode 100644 index 0000000..1597358 --- /dev/null +++ b/parsers/parserSSA/popcorn.parseSSA.js @@ -0,0 +1,132 @@ +// PARSER: 0.3 SSA/ASS + +(function (Popcorn) { + /** + * SSA/ASS popcorn parser plug-in + * Parses subtitle files in the identical SSA and ASS formats. + * Style information is ignored, and may be found in these + * formats: (\N \n {\pos(400,570)} {\kf89}) + * Out of the [Script Info], [V4 Styles], [Events], [Pictures], + * and [Fonts] sections, only [Events] is processed. + * Data parameter is given by Popcorn, will need a text. + * Text is the file contents to be parsed + * + * @param {Object} data + * + * Example: + [Script Info] + Title: Testing subtitles for the SSA Format + [V4 Styles] + Format: Name, Fontname, Fontsize, PrimaryColour, SecondaryColour, TertiaryColour, BackColour, Bold, Italic, BorderStyle, Outline, Shadow, Alignment, MarginL, MarginR, MarginV, AlphaLevel, Encoding + Style: Default,Arial,20,65535,65535,65535,-2147483640,-1,0,1,3,0,2,30,30,30,0,0 + [Events] + Format: Layer, Start, End, Style, Name, MarginL, MarginR, MarginV, Effect, Text + Dialogue: 0,0:00:02.40,0:00:07.20,Default,,0000,0000,0000,,Senator, {\kf89}we're \Nmaking our final \napproach into Coruscant. + Dialogue: 0,0:00:09.71,0:00:13.39,Default,,0000,0000,0000,,{\pos(400,570)}Very good, Lieutenant. + Dialogue: 0,0:00:15.04,0:00:18.04,Default,,0000,0000,0000,,It's \Na \ntrap! + * + */ + + // Register for SSA extensions + Popcorn.parser( "parseSSA", "SSA", function( data ) { + + // declare needed variables + var retObj = { + title: "", + remote: "", + data: [] + }, + subs = [], + startIdx, + endIdx, + textIdx, + lines, + fields, + numFields, + sub, + text, + i = 0, + j = 0, + len = 0, + fieldLen = 0; + + // h:mm:ss.cc (centisec) string to SS.mmm + // Returns -1 if invalid + var toSeconds = function( t_in ) { + var t = t_in.split( ":" ), + l = t.length - 1; + + // Not all there + if ( t_in.length !== 10 ) { + return -1; + } + + return parseInt( t[0], 10 )*3600 + parseInt( t[l-1], 10 )*60 + parseFloat( t[l], 10 ); + }; + + 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; + + // Ignore non-textual info + while ( i < len && lines[i] !== "[Events]" ) { + i++; + } + + fields = lines[++i].substr( 8 ).split( ", " ); // Trim 'Format: ' off front, split on delim + numFields = fields.length; + + //Find where in Dialogue string the start, end and text info is + for ( ; j < numFields; j++ ) { + if ( fields[j] === "Start" ) { + startIdx = j; + } else if ( fields[j] === "End" ) { + endIdx = j; + } else if ( fields[j] === "Text" ) { + textIdx = j; + } + } + + while ( ++i < len && lines[i] && lines[i][0] !== "[" ) { + sub = {}; + + // Trim beginning 'Dialogue: ' and split on delim + fields = lines[i].substr( 10 ).split( "," ); + + sub.start = toSeconds( fields[startIdx] ); + sub.end = toSeconds( fields[endIdx] ); + + // Invalid time, skip + if ( sub.start === -1 || sub.end === -1 ) { + continue; + } + + if ( ( fieldLen = fields.length ) === numFields ) { + sub.text = fields[textIdx]; + } else { + // There were commas in the text which were split, append back together into one line + text = []; + + for( j = textIdx; j < fieldLen; j++ ) { + text.push( fields[j] ); + } + sub.text = text.join( "," ); + } + + // Eliminate advanced styles and convert forced line breaks + sub.text = sub.text.replace( /\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}/gi, "" ).replace( /\\N/gi, "
    " ); + subs.push( createTrack( "subtitle", sub ) ); + } + + retObj.data = subs; + return retObj; + }); + +})( Popcorn ); diff --git a/parsers/parserSSA/popcorn.parseSSA.unit.html b/parsers/parserSSA/popcorn.parseSSA.unit.html new file mode 100644 index 0000000..3f2438d --- /dev/null +++ b/parsers/parserSSA/popcorn.parseSSA.unit.html @@ -0,0 +1,43 @@ + + + + Popcorn 0.3 SSA/ASS parser Plug-in Test + + + + + + + + + + + +

    Popcorn 0.3 SSA/ASS parser Plug-in Test

    +

    +
    +

    +
      +
      + + + + diff --git a/parsers/parserSSA/popcorn.parseSSA.unit.js b/parsers/parserSSA/popcorn.parseSSA.unit.js new file mode 100644 index 0000000..5ba1d76 --- /dev/null +++ b/parsers/parserSSA/popcorn.parseSSA.unit.js @@ -0,0 +1,57 @@ +test( "Popcorn 0.3 SSA/ASS Parser Plugin", function () { + + var count = 0, + numSubs = 0, + sub, + poppercorn = Popcorn( "#video" ), + subs = [ + { + text: "Senator, we're
      making our final
      approach into Coruscant.", + start: 2.4, + end: 7.2 + }, + { + text: "Very good, Lieutenant.", + start: 9.71, + end: 13.39 + }, + { + text: "It's
      a
      trap!", + start: 15.04, + end: 18.04 + } + ], + expects = subs.length*3 + 1; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + poppercorn.parseSSA( document.getElementById( "video" ).getAttribute( "data-timeline-sources" ) ); + + expect( expects ); + stop( 5000 ); + + // Allow load time + setTimeout(function () { + Popcorn.forEach( poppercorn.getTrackEvents(), function(evt) { + if( evt._natives.type === "subtitle" ) { + sub = subs[numSubs++]; + + equals( evt.start, sub.start, "Correctly parsed start of " + evt.start ); + plus(); + equals( evt.text, sub.text, "Correctly parsed text of " + evt.start ); + plus(); + equals( evt.end, sub.end, "Correctly parsed end at " + evt.start ); + plus(); + } + }); + + equals( subs.length, numSubs, "Parsed all subtitles" ); + plus(); + + }, 500); + +}); diff --git a/parsers/parserTTML/data/data.ttml b/parsers/parserTTML/data/data.ttml new file mode 100644 index 0000000..a91ae11 --- /dev/null +++ b/parsers/parserTTML/data/data.ttml @@ -0,0 +1,75 @@ + + + + + Timed Text TTML Example + The Authors (c) 2006 + + + +