From 91aece5d25ae83f1548cacd2f0cfea0a67f0223a Mon Sep 17 00:00:00 2001 From: stevenaw Date: Thu, 3 Feb 2011 16:43:53 -0500 Subject: [PATCH] [#291] Remake branch --- parsers/parserVTT/data/data.vtt | 12 ++ parsers/parserVTT/data/unit.vtt | 43 ++++++ parsers/parserVTT/popcorn.parserVTT.html | 54 ++++++++ parsers/parserVTT/popcorn.parserVTT.js | 124 ++++++++++++++++++ parsers/parserVTT/popcorn.parserVTT.unit.html | 44 +++++++ parsers/parserVTT/popcorn.parserVTT.unit.js | 67 ++++++++++ 6 files changed, 344 insertions(+) create mode 100644 parsers/parserVTT/data/data.vtt create mode 100644 parsers/parserVTT/data/unit.vtt create mode 100644 parsers/parserVTT/popcorn.parserVTT.html create mode 100644 parsers/parserVTT/popcorn.parserVTT.js create mode 100644 parsers/parserVTT/popcorn.parserVTT.unit.html create mode 100644 parsers/parserVTT/popcorn.parserVTT.unit.js diff --git a/parsers/parserVTT/data/data.vtt b/parsers/parserVTT/data/data.vtt new file mode 100644 index 0000000..f2e62a0 --- /dev/null +++ b/parsers/parserVTT/data/data.vtt @@ -0,0 +1,12 @@ +1 +00:00:02.400 --> 00:00:07.200 +Senator, we're making +our final approach into Coruscant. + +2 +00:00:09.712 --> 00:00:13.399 +Very good, Lieutenant. + +Track-3 +00:00:15.542 --> 00:00:18.542 A:start D:vertical L:98% +It's a trap! \ No newline at end of file diff --git a/parsers/parserVTT/data/unit.vtt b/parsers/parserVTT/data/unit.vtt new file mode 100644 index 0000000..b7ead46 --- /dev/null +++ b/parsers/parserVTT/data/unit.vtt @@ -0,0 +1,43 @@ +1 +00:00:02.400 --> 00:00:07.200 +Senator, we're making +our final approach into Coruscant. + +2 +00:09.712-->00:13.399 +Very good, Lieutenant. + + + +3 +00:00:13.400 --> 00:00:20 +A bad cue, no milliseconds. Should be ignored + +--> +00:00:14.456--> 00:00:20.000 +A bad cue, bad id. Should be ignored + +4 +00:00:9.456--> 00:00:20.000 +A bad cue, seconds are single digit. Should be ignored + +5 +00:1:00.456--> 00:00:20.000 +A bad cue, minutes are single digit. Should be ignored + +6 +1:00:00.456--> 00:00:20.000 +A bad cue, hours are supplied as single digit. Should be ignored + +7 +00:00:00.46--> 00:00:20.000 +A bad cue, ms are not 3 digits. Should be ignored + +Track-3 +00:00:15.042 --> 00:00:18.042 A:start D:vertical L:98% +It's a trap! + + +ID9 +00:00:20.000--> 00:00:21.670 +This text is boldy italicized \ No newline at end of file diff --git a/parsers/parserVTT/popcorn.parserVTT.html b/parsers/parserVTT/popcorn.parserVTT.html new file mode 100644 index 0000000..69aba6f --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.html @@ -0,0 +1,54 @@ + + + + Popcorn 0.3 WebSRT/VTT parser Plug-in Demo + + + + + + + + +

Popcorn 0.3 WebSRT/VTT 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.542 to 18.542 seconds, "It's a trap!" is shown, with trap in italics

+
+ +
+ +

Subtitle Source

+ + + + + + diff --git a/parsers/parserVTT/popcorn.parserVTT.js b/parsers/parserVTT/popcorn.parserVTT.js new file mode 100644 index 0000000..6aee024 --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.js @@ -0,0 +1,124 @@ +// PARSER: 0.3 WebSRT/VTT + +(function ( Popcorn ) { + /** + * WebSRT/VTT popcorn parser plug-in + * Parses subtitle files in the WebSRT/VTT format. + * Styles which appear after timing information are ignored. + * Inline styling tags follow HTML conventions and are left in for the browser to handle + * TrackEvents (cues) which are malformed are ignored. + * Data parameter is given by Popcorn, will need a text. + * Text is the file contents to be parsed + * + * @param {Object} data + * + * Example: + Track-3 + 00:00:15.542 --> 00:00:18.542 A:start D:vertical L:98% + It's a trap! + */ + Popcorn.parser( "parseVTT", "VTT", function( data ) { + + // declare needed variables + var retObj = { + title: "", + remote: "", + data: [] + }, + subs = [], + i = 0, + len = 0, + idx = 0, + lines, + time, + text, + sub; + + // [HH:]MM:SS.mmm string to SS.mmm float + // Throws exception if invalid + var toSeconds = function( t_in ) { + var t = t_in.split( ":" ), + l = t_in.length, + time; + + // Invalid time string provided + if ( l !== 12 && l !== 9 ) { + throw "Bad cue"; + } + + l = t.length - 1; + + try { + time = parseInt( t[l-1], 10 )*60 + parseFloat( t[l], 10 ); + + // Hours were 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 ) { + sub = {}; + text = []; + + try { + sub.id = lines[i++]; + // Ignore if id contains "-->" + if ( !sub.id || sub.id.indexOf( "-->" ) !== -1 ) { + throw "Bad cue"; + } + + time = lines[i++].split( /[\t ]*-->[\t ]*/ ); + + sub.start = toSeconds(time[0]); + + // Filter out any trailing styling info + 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 lines together to one and build subtitle + 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 after a cue + while ( i < len && !lines[i] ) { + i++; + } + } + + retObj.data = subs; + return retObj; + }); + +})( Popcorn ); diff --git a/parsers/parserVTT/popcorn.parserVTT.unit.html b/parsers/parserVTT/popcorn.parserVTT.unit.html new file mode 100644 index 0000000..86ce76b --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.unit.html @@ -0,0 +1,44 @@ + + + + Popcorn 0.3 WebSRT/VTT parser Plug-in Test + + + + + + + + + + + +

Popcorn 0.3 WebSRT/VTT parser Plug-in Test

+

+
+

+
    +
    + + + + \ No newline at end of file diff --git a/parsers/parserVTT/popcorn.parserVTT.unit.js b/parsers/parserVTT/popcorn.parserVTT.unit.js new file mode 100644 index 0000000..2f41da3 --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.unit.js @@ -0,0 +1,67 @@ +test( "Popcorn 0.3 WebSRT/VTT Parser Plugin", function () { + + var count = 0, + numSubs = 0, + sub, + poppercorn = Popcorn( "#video" ), + expectedSubs = [ + { + id: "1", + text: "Senator, we're making
    our final approach into Coruscant.", + start: 2.4, + end: 7.2 + }, + { + id: "2", + text: "Very good, Lieutenant.", + start: 9.712, + end: 13.399 + }, + { + id: "Track-3", + text: "It's a trap!", + start: 15.042, + end: 18.042 + }, + { + id: "ID9", + text: "This text is boldy italicized", + start: 20.000, + end: 21.670 + } + ], + expects = expectedSubs.length*4 + 1; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + poppercorn.parseVTT( 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 = expectedSubs[numSubs++]; + + strictEqual( evt.id, sub.id, "Correctly parsed id" ); + plus(); + strictEqual( evt.text, sub.text, "Correctly parsed text of " + evt.id ); + plus(); + strictEqual( evt.start, sub.start, "Correctly parsed start at " + evt.id ); + plus(); + strictEqual( evt.end, sub.end, "Correctly parsed end at " + evt.id ); + plus(); + } + }); + + equals( expectedSubs.length, numSubs, "Parsed all subtitles" ); + plus(); + + }, 500); + +}); \ No newline at end of file -- 2.43.0