Skip to content

Commit

Permalink
Text parsing completed. Styles remain [mozilla#291]
Browse files Browse the repository at this point in the history
  • Loading branch information
stevenaw committed Jan 26, 2011
1 parent ae99e65 commit 131ed1d
Show file tree
Hide file tree
Showing 6 changed files with 241 additions and 0 deletions.
8 changes: 8 additions & 0 deletions 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.
12 changes: 12 additions & 0 deletions 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!
41 changes: 41 additions & 0 deletions parsers/parserVTT/popcorn.parserVTT.html
@@ -0,0 +1,41 @@
<!DOCTYPE html>
<html>
<head>
<title>Popcorn 0.3 VTT parser Plug-in Demo</title>

<script src="../../popcorn.js"></script>
<script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
<script src="../../parsers/parserVTT/popcorn.parserVTT.js"></script>

</head>
<body>
<h1 id="qunit-header">Popcorn 0.3 VTT parser Plug-in Demo</h1>
<p></p>
<div>
<video id='video' data-timeline-sources="data/data.vtt"
controls
width= '250px'
poster="../../test/poster.png">

<source id='mp4'
src="../../test/trailer.mp4"
type='video/mp4; codecs="avc1, mp4a"'>

<source id='ogv'
src="../../test/trailer.ogv"
type='video/ogg; codecs="theora, vorbis"'>

<p>Your user agent does not support the HTML5 Video element.</p>

</video>
</div>

<style>
.displays {
width: 300px;
height: 300px;
}
</style>

</body>
</html>
75 changes: 75 additions & 0 deletions 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 );
43 changes: 43 additions & 0 deletions parsers/parserVTT/popcorn.parserVTT.unit.html
@@ -0,0 +1,43 @@
<!DOCTYPE html>
<html>
<head>
<title>Popcorn 0.3 VTT parser Plug-in Test</title>
<link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
<script src="../../test/qunit/qunit.js"></script>
<!--
do not move - this must be called immediately prior to
popcorn-api-draft.js
-->

<script src="../../popcorn.js"></script>

<script src="popcorn.parserVTT.js"></script>
<script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
<script src="popcorn.parserVTT.unit.js"></script>
</head>
<body>
<h1 id="qunit-header">Popcorn 0.3 VTT parser Plug-in Test</h1>
<h2 id="qunit-banner"></h2>
<div id="qunit-testrunner-toolbar"></div>
<h2 id="qunit-userAgent"></h2>
<ol id="qunit-tests"></ol>
<div id="qunit-fixture"> </div>

<video id='video'
controls
width= '250px'
poster="../../test/poster.png">

<source id='mp4'
src="../../test/trailer.mp4"
type='video/mp4; codecs="avc1, mp4a"'>

<source id='ogv'
src="../../test/trailer.ogv"
type='video/ogg; codecs="theora, vorbis"'>

<p>Your user agent does not support the HTML5 Video element.</p>

</video>
</body>
</html>
62 changes: 62 additions & 0 deletions 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);

});

0 comments on commit 131ed1d

Please sign in to comment.