]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Text parsing completed. Styles remain [#291]
authorstevenaw <sweerdenburg@gmail.com>
Wed, 26 Jan 2011 16:24:28 +0000 (11:24 -0500)
committerstevenaw <sweerdenburg@gmail.com>
Wed, 26 Jan 2011 16:24:28 +0000 (11:24 -0500)
parsers/parserVTT/data/data.vtt [new file with mode: 0644]
parsers/parserVTT/data/unit.vtt [new file with mode: 0644]
parsers/parserVTT/popcorn.parserVTT.html [new file with mode: 0644]
parsers/parserVTT/popcorn.parserVTT.js [new file with mode: 0644]
parsers/parserVTT/popcorn.parserVTT.unit.html [new file with mode: 0644]
parsers/parserVTT/popcorn.parserVTT.unit.js [new file with mode: 0644]

diff --git a/parsers/parserVTT/data/data.vtt b/parsers/parserVTT/data/data.vtt
new file mode 100644 (file)
index 0000000..6ef05aa
--- /dev/null
@@ -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 (file)
index 0000000..ee8c610
--- /dev/null
@@ -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 (file)
index 0000000..99d55da
--- /dev/null
@@ -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>
diff --git a/parsers/parserVTT/popcorn.parserVTT.js b/parsers/parserVTT/popcorn.parserVTT.js
new file mode 100644 (file)
index 0000000..9350234
--- /dev/null
@@ -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 (file)
index 0000000..b4204fe
--- /dev/null
@@ -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>
\ 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 (file)
index 0000000..c1e3670
--- /dev/null
@@ -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