From: stevenaw Date: Wed, 26 Jan 2011 16:24:28 +0000 (-0500) Subject: Text parsing completed. Styles remain [#291] X-Git-Tag: v0.3~15^2~6^2~3 X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=131ed1db8c86e4ddd922d71d34a46f851bdb03db;p=popcorn-js.git Text parsing completed. Styles remain [#291] --- diff --git a/parsers/parserVTT/data/data.vtt b/parsers/parserVTT/data/data.vtt new file mode 100644 index 0000000..6ef05aa --- /dev/null +++ b/parsers/parserVTT/data/data.vtt @@ -0,0 +1,8 @@ +1 +00:00:02.400 --> 00:00:07.200 +Senator, we're making +our final approach into Coruscant. + +2 +00:00:9.712 --> 00:00:13.399 +Very good, Lieutenant. diff --git a/parsers/parserVTT/data/unit.vtt b/parsers/parserVTT/data/unit.vtt new file mode 100644 index 0000000..ee8c610 --- /dev/null +++ b/parsers/parserVTT/data/unit.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:9.712 --> 00:13.399 +Very good, Lieutenant. + +Track-3 +00:00:15.042 --> 00:00:18.042 A:start D:vertical L:98% +It's a trap! diff --git a/parsers/parserVTT/popcorn.parserVTT.html b/parsers/parserVTT/popcorn.parserVTT.html new file mode 100644 index 0000000..99d55da --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.html @@ -0,0 +1,41 @@ + + + + Popcorn 0.3 VTT parser Plug-in Demo + + + + + + + +

Popcorn 0.3 VTT parser Plug-in Demo

+

+
+ +
+ + + + + diff --git a/parsers/parserVTT/popcorn.parserVTT.js b/parsers/parserVTT/popcorn.parserVTT.js new file mode 100644 index 0000000..9350234 --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.js @@ -0,0 +1,75 @@ +// PARSER: 0.3 VTT + +(function (Popcorn) { + Popcorn.parser( "parseVTT", "VTT", function( data ) { + + // declare needed variables + var retObj = { + title: "", + remote: "", + data: [] + }, + subs = [], + lines, + i = 0, l = 0; + + // [HH]:MM:SS.mmm string to SS.mmm + var toSeconds = function(t_in) { + var t = t_in.split(':'); + var time, l = t.length-1; + + try { + time = parseInt(t[l-1], 10)*60 + parseFloat(t[l], 10); + + if (l === 2) // Hours given + time += parseInt(t[0], 10)*60*60; // hours => seconds + } 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 + lines = data.text.replace("\r",'').split("\n"); + l = lines.length; + + while (i < l) { + var sub = {}; + + sub.id = lines[i++].replace('\r', ''); + + var time = lines[i++].split(" --> "); + var text = []; + + try { + sub.start = toSeconds(time[0]); + + // Filter out any trailing styling info + time[1] = time[1].replace("\r", " "); + sub.end = toSeconds(time[1].substr(0, time[1].indexOf(" "))); + + while (lines[i] && lines[i] !== "\r") { + text.push(lines[i++].replace("\r", "")); + } + + sub.text = text.join(" "); + subs.push( createTrack("subtitle", sub) ); + } catch (e) { // Bad cue + while (i < l && !lines[i++] !== "\r"); // Advance to end of cue + } + + // Consume empty whitespace + while (i < l && !lines[i++] === "\r"); + } + + 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..b4204fe --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.unit.html @@ -0,0 +1,43 @@ + + + + Popcorn 0.3 VTT parser Plug-in Test + + + + + + + + + + + +

Popcorn 0.3 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..c1e3670 --- /dev/null +++ b/parsers/parserVTT/popcorn.parserVTT.unit.js @@ -0,0 +1,62 @@ +test("Popcorn 0.3 VTT Parser Plugin", function () { + + var count = 0, + numSubs = 0, + sub, + poppercorn = Popcorn( "#video" ), + subs = { // Expected values + "1": { + text: "Senator, we're making our final approach into Coruscant.", + start: 2.4, + end: 7.2 + }, + "2": { + text: "Very good, Lieutenant.", + start: 9.712, + end: 13.399 + }, + "Track-3": { + text: "It's a trap!", + start: 15.042, + end: 18.042 + } + }, + expectSubs = 3, + expects = expectSubs*4 + 1; + + function plus() { + if ( ++count === expects ) { + start(); + } + } + + poppercorn.parseVTT("data/unit.vtt"); + + expect(expects); + + stop( 5000 ); + + // Allow load time + setTimeout(function () { + Popcorn.forEach(poppercorn.getTrackEvents(), function(evt) { + if(evt._natives.type === "subtitle") { + numSubs++; + sub = subs[evt.id.toString()]; + + ok(!!sub , "Correctly parsed id of " + evt.id ); + plus(); + equals(evt.text, sub.text, "Correctly parsed text of " + evt.id ); + plus(); + equals(evt.start, sub.start, "Correctly parsed start at " + evt.id ); + plus(); + equals(evt.end, sub.end, "Correctly parsed end at " + evt.id ); + plus(); + } + }); + + equals(expectSubs, numSubs , "Parsed all subs" ); + plus(); + + }, 500); + +}); \ No newline at end of file