]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#296] Branch remade for SBV, SSA. TTML Parsing complete
authorstevenaw <sweerdenburg@gmail.com>
Tue, 8 Feb 2011 19:50:04 +0000 (14:50 -0500)
committerAnna Sobiepanek <annaopensource@gmail.com>
Tue, 8 Feb 2011 23:11:10 +0000 (18:11 -0500)
18 files changed:
parsers/parserSBV/data/data.sbv [new file with mode: 0644]
parsers/parserSBV/data/unit.sbv [new file with mode: 0644]
parsers/parserSBV/popcorn.parseSBV.html [new file with mode: 0644]
parsers/parserSBV/popcorn.parseSBV.js [new file with mode: 0644]
parsers/parserSBV/popcorn.parseSBV.unit.html [new file with mode: 0644]
parsers/parserSBV/popcorn.parseSBV.unit.js [new file with mode: 0644]
parsers/parserSSA/data/data.ssa [new file with mode: 0644]
parsers/parserSSA/data/unit.ssa [new file with mode: 0644]
parsers/parserSSA/popcorn.parseSSA.html [new file with mode: 0644]
parsers/parserSSA/popcorn.parseSSA.js [new file with mode: 0644]
parsers/parserSSA/popcorn.parseSSA.unit.html [new file with mode: 0644]
parsers/parserSSA/popcorn.parseSSA.unit.js [new file with mode: 0644]
parsers/parserTTML/data/data.ttml [new file with mode: 0644]
parsers/parserTTML/data/unit.ttml [new file with mode: 0644]
parsers/parserTTML/popcorn.parseTTML.html [new file with mode: 0644]
parsers/parserTTML/popcorn.parseTTML.js [new file with mode: 0644]
parsers/parserTTML/popcorn.parseTTML.unit.html [new file with mode: 0644]
parsers/parserTTML/popcorn.parseTTML.unit.js [new file with mode: 0644]

diff --git a/parsers/parserSBV/data/data.sbv b/parsers/parserSBV/data/data.sbv
new file mode 100644 (file)
index 0000000..3b76ff7
--- /dev/null
@@ -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 (file)
index 0000000..3b76ff7
--- /dev/null
@@ -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 (file)
index 0000000..a2abdb7
--- /dev/null
@@ -0,0 +1,48 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 SBV parser Plug-in Demo</title>
+  
+  <script src="../../popcorn.js"></script>
+  <script src="../../test/jquery.js"></script>
+  <script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parseSBV.js"></script>
+  
+  <script>
+    document.addEventListener( 'DOMContentLoaded', function () {
+      var p = Popcorn( '#video' )
+      .volume( 0 )
+      .play();
+    }, false);
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.3 SBV parser Plug-in Demo</h1>
+  <p>From 2.4 to 7.2 seconds, "Senator, we're making our final approach into Coruscant." is shown
+  <br />From 9.712 to 13.399 seconds, "Very good, Lieutenant." is shown
+  <br />From 15.042 to 18.042 seconds, "It's a trap!" is shown</p>
+  <div>
+  <div>
+    <video id='video'
+      controls
+      width= '250px'
+      data-timeline-sources="data/data.SBV"
+      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>
+  
+  <h4>Subtitle Source<h4>
+  <iframe id="srcDisplay" src="data/data.SBV"></iframe>
+</body>
+</html>
diff --git a/parsers/parserSBV/popcorn.parseSBV.js b/parsers/parserSBV/popcorn.parseSBV.js
new file mode 100644 (file)
index 0000000..364fba8
--- /dev/null
@@ -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( "<br />" );
+        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 (file)
index 0000000..c9966ab
--- /dev/null
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 SBV 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="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parseSBV.js"></script>
+  <script src="popcorn.parseSBV.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.3 SBV 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'
+    data-timeline-sources="data/unit.SBV"
+    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>
diff --git a/parsers/parserSBV/popcorn.parseSBV.unit.js b/parsers/parserSBV/popcorn.parseSBV.unit.js
new file mode 100644 (file)
index 0000000..8ce17b6
--- /dev/null
@@ -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 (file)
index 0000000..3114a84
--- /dev/null
@@ -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 (file)
index 0000000..3114a84
--- /dev/null
@@ -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 (file)
index 0000000..183686f
--- /dev/null
@@ -0,0 +1,46 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 SSA/ASS parser Plug-in Demo</title>
+  
+  <script src="../../popcorn.js"></script>
+  <script src="../../test/jquery.js"></script>
+  <script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parseSSA.js"></script>
+
+  <script>
+    document.addEventListener( 'DOMContentLoaded', function () {
+      var p = Popcorn( '#video' )
+      .volume( 0 )
+      .play();
+    }, false);
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.3 SSA/ASS parser Plug-in Demo</h1>
+  <p>From 2.4 to 7.2 seconds, "Senator, we're making our final approach into Coruscant." is shown
+  <br />From 9.71 to 13.39 seconds, "Very good, Lieutenant." is shown
+  <br />From 15.04 to 18.04 seconds, "It's a trap!" is shown</p>
+  <div>
+    <video id='video'
+      controls
+      width= '250px'
+      data-timeline-sources="data/data.SSA"
+      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>\
+  <h4>Subtitle Source<h4>
+  <iframe id="srcDisplay" src="data/data.SSA"></iframe>
+</body>
+</html>
diff --git a/parsers/parserSSA/popcorn.parseSSA.js b/parsers/parserSSA/popcorn.parseSSA.js
new file mode 100644 (file)
index 0000000..1597358
--- /dev/null
@@ -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, "<br />" );
+      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 (file)
index 0000000..3f2438d
--- /dev/null
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 SSA/ASS 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="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parseSSA.js"></script>
+  <script src="popcorn.parseSSA.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.3 SSA/ASS 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'
+    data-timeline-sources="data/unit.SSA"
+    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>
diff --git a/parsers/parserSSA/popcorn.parseSSA.unit.js b/parsers/parserSSA/popcorn.parseSSA.unit.js
new file mode 100644 (file)
index 0000000..5ba1d76
--- /dev/null
@@ -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 <br />making our final <br />approach into Coruscant.",
+          start: 2.4,
+          end: 7.2
+        },
+        {
+          text: "Very good, Lieutenant.",
+          start: 9.71,
+          end: 13.39
+        },
+        {
+          text: "It's <br />a <br />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 (file)
index 0000000..a91ae11
--- /dev/null
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<tt xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xmlns="http://www.w3.org/2006/04/ttaf1">
+  <head>
+    <metadata xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
+      <ttm:title>Timed Text TTML Example</ttm:title>
+      <ttm:copyright>The Authors (c) 2006</ttm:copyright>
+    </metadata>
+    <styling xmlns:tts="http://www.w3.org/ns/ttml#styling">
+      <!-- s1 specifies default color, font, and text alignment -->
+      <style xml:id="s1"
+        tts:color="white"
+        tts:fontFamily="proportionalSansSerif"
+        tts:fontSize="22px"
+        tts:textAlign="center"
+      />
+      <!-- alternative using yellow text but otherwise the same as style s1 -->
+      <style xml:id="s2" style="s1" tts:color="yellow"/>
+      <!-- a style based on s1 but justified to the right -->
+      <style xml:id="s1Right" style="s1" tts:textAlign="end" />     
+      <!-- a style based on s2 but justified to the left -->
+      <style xml:id="s2Left" style="s2" tts:textAlign="start" />
+    </styling>
+    <layout xmlns:tts="http://www.w3.org/ns/ttml#styling">
+      <region xml:id="subtitleArea"
+        style="s1"
+        tts:extent="560px 62px"
+        tts:padding="5px 3px"
+        tts:backgroundColor="black"
+        tts:displayAlign="after"
+      />
+    </layout> 
+  </head>
+  <body region="subtitleArea">
+    <div>
+      <p xml:id="subtitle1" begin="0.76s" end="3.45s">
+        It seems a paradox, does it not,
+      </p>
+      <p xml:id="subtitle2" begin="5.0s" end="10.0s">
+        that the image formed on<br/>
+        the Retina should be inverted?
+      </p>
+      <p xml:id="subtitle3" begin="10.0s" end="16.0s" style="s2">
+        It is puzzling, why is it<br/>
+        we do not see things upside-down?
+      </p>
+      <p xml:id="subtitle4" begin="17.2s" end="23.0s">
+        You have never heard the Theory,<br/>
+        then, that the Brain also is inverted?
+      </p>
+      <p xml:id="subtitle5" begin="23.0s" end="27.0s" style="s2">
+        No indeed! What a beautiful fact!
+      </p>
+    </div>
+    <div begin="00:00:27">
+      <p xml:id="subtitle1" begin="0.76s" end="3.45s">
+        It seems a paradox, does it not,
+      </p>
+      <p xml:id="subtitle2" begin="5.0s" end="10.0s">
+        that the image formed on<br/>
+        the Retina should be inverted?
+      </p>
+      <p xml:id="subtitle3" begin="10.0s" end="16.0s" style="s2">
+        It is puzzling, why is it<br/>
+        we do not see things upside-down?
+      </p>
+      <p xml:id="subtitle4" begin="17.2s" end="23.0s">
+        You have never heard the Theory,<br/>
+        then, that the Brain also is inverted?
+      </p>
+      <p xml:id="subtitle5" begin="23.0s" end="27.0s" style="s2">
+        No indeed! What a beautiful fact!
+      </p>
+    </div>  
+  </body>
+</tt>
diff --git a/parsers/parserTTML/data/unit.ttml b/parsers/parserTTML/data/unit.ttml
new file mode 100644 (file)
index 0000000..606cdc3
--- /dev/null
@@ -0,0 +1,75 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<tt xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xmlns="http://www.w3.org/2006/04/ttaf1">
+  <head>
+    <metadata xmlns:ttm="http://www.w3.org/ns/ttml#metadata">
+      <ttm:title>Timed Text TTML Example</ttm:title>
+      <ttm:copyright>The Authors (c) 2006</ttm:copyright>
+    </metadata>
+    <styling xmlns:tts="http://www.w3.org/ns/ttml#styling">
+      <!-- s1 specifies default color, font, and text alignment -->
+      <style xml:id="s1"
+        tts:color="white"
+        tts:fontFamily="proportionalSansSerif"
+        tts:fontSize="22px"
+        tts:textAlign="center"
+      />
+      <!-- alternative using yellow text but otherwise the same as style s1 -->
+      <style xml:id="s2" style="s1" tts:color="yellow"/>
+      <!-- a style based on s1 but justified to the right -->
+      <style xml:id="s1Right" style="s1" tts:textAlign="end" />     
+      <!-- a style based on s2 but justified to the left -->
+      <style xml:id="s2Left" style="s2" tts:textAlign="start" />
+    </styling>
+    <layout xmlns:tts="http://www.w3.org/ns/ttml#styling">
+      <region xml:id="subtitleArea"
+        style="s1"
+        tts:extent="560px 62px"
+        tts:padding="5px 3px"
+        tts:backgroundColor="black"
+        tts:displayAlign="after"
+      />
+    </layout> 
+  </head>
+  <body region="subtitleArea">
+    <div>
+      <p xml:id="subtitle1" begin="0.76s" end="3.45s">
+        It seems a paradox, does it not,
+      </p>
+      <p xml:id="subtitle2" begin="5.0s" end="10.0s">
+        that the image formed on<br/>
+        the Retina should be inverted?
+      </p>
+      <p xml:id="subtitle3" begin="10.0s" end="16.0s" style="s2">
+        It is puzzling, why is it<br/>
+        we do not see things upside-down?
+      </p>
+      <p xml:id="subtitle4" begin="17.2s" end="23.0s">
+        You have never heard the Theory,<br/>
+        then, that the Brain also is inverted?
+      </p>
+      <p xml:id="subtitle5" begin="23.0s" end="27.0s" style="s2">
+        No indeed! What a beautiful fact!
+      </p>
+    </div>
+    <div begin="00:00:27">
+      <p xml:id="subtitle6" begin="0.76s" end="3.45s">
+        It seems a paradox, does it not,
+      </p>
+      <p xml:id="subtitle7" begin="0.1m" end="10s">
+        that the image formed on<br/>
+        the Retina should be inverted?
+      </p>
+      <p xml:id="subtitle8" begin="00:00:37" end="00:00:43" style="s2">
+        It is puzzling, why is it<br/>
+        we do not see things upside-down?
+      </p>
+      <p xml:id="subtitle9" begin="17.2s" end="23.0s">
+        You have never heard the Theory,<br/>
+        then, that the Brain also is inverted?
+      </p>
+      <p xml:id="subtitle10" begin="23.0s" end="27.0s" style="s2">
+        No indeed! What a beautiful fact!
+      </p>
+    </div>  
+  </body>
+</tt>
diff --git a/parsers/parserTTML/popcorn.parseTTML.html b/parsers/parserTTML/popcorn.parseTTML.html
new file mode 100644 (file)
index 0000000..c1842f7
--- /dev/null
@@ -0,0 +1,54 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 TTML parser Plug-in Demo</title>
+  
+  <script src="../../popcorn.js"></script>
+  <script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parseTTML.js"></script>
+
+  <script>
+    document.addEventListener( 'DOMContentLoaded', function () {
+      var p = Popcorn( '#video' )
+      .volume( 0 )
+      .play();
+    }, false);
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.3 TTML parser Plug-in Demo</h1>
+  <p>From 0.76 to 3.45 seconds, "It seems a paradox, does it not," is shown
+  <br />From 5 to 10 seconds, "that the image formed on the Retina should be inverted?" is shown
+  <br />From 10 to 16 seconds, "It is puzzling, why is it we do not see things upside-down?" is shown
+  <br />From 17.2 to 23 seconds, "You have never heard the Theory, then, that the Brain also is inverted?" is shown
+  <br />From 23 to 27 seconds, "No indeed! What a beautiful fact!" is shown
+  <br />From 27.76 to 30.45 seconds, "It seems a paradox, does it not," is shown
+  <br />From 32 to 37 seconds, "that the image formed on the Retina should be inverted?" is shown
+  <br />From 37 to 43 seconds, "It is puzzling, why is it we do not see things upside-down?" is shown
+  <br />From 44.2 to 50 seconds, "You have never heard the Theory, then, that the Brain also is inverted?" is shown
+  <br />From 50 to 54 seconds, "No indeed! What a beautiful fact!" is shown</p>
+  <div>
+    <video id='video'
+      controls
+      width= '250px'
+      data-timeline-sources="data/data.TTML"
+      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>
+
+  <h4>Subtitle Source<h4>
+  <iframe id="srcDisplay" src="data/data.TTML"></iframe>
+
+</body>
+</html>
diff --git a/parsers/parserTTML/popcorn.parseTTML.js b/parsers/parserTTML/popcorn.parseTTML.js
new file mode 100644 (file)
index 0000000..42e88fe
--- /dev/null
@@ -0,0 +1,171 @@
+// PARSER: 0.3 TTML
+
+(function (Popcorn) {
+
+  /**
+   * TTML popcorn parser plug-in 
+   * Parses subtitle files in the TTML format.
+   * Times may be absolute to the timeline or relative
+   *   Absolute times are ISO 8601 format (hh:mm:ss[.mmm])
+   *   Relative times are a fraction followed by a unit metric (d.ddu)
+   *     Relative times are relative to the time given on the parent node
+   * Styling information is ignored
+   * Data parameter is given by Popcorn, will need an xml.
+   * Xml is the file contents to be processed
+   * 
+   * @param {Object} data
+   * 
+   * Example:
+    <tt xmlns:tts="http://www.w3.org/2006/04/ttaf1#styling" xmlns="http://www.w3.org/2006/04/ttaf1">
+      <body region="subtitleArea">
+        <div>
+          <p xml:id="subtitle1" begin="0.76s" end="3.45s">
+            It seems a paradox, does it not,
+          </p>
+        </div>
+      </body>
+    </tt>
+   */
+  Popcorn.parser( "parseTTML", "TTML", function( data ) {
+
+    // declare needed variables
+    var returnData = {
+          title: "",
+          remote: "",
+          data: []
+        },
+        node,
+        numTracks = 0,
+        region;
+    
+    // Convert time expression to SS.mmm
+    // Expression may be absolute to timeline (hh:mm:ss.ms)
+    //   or relative ( fraction followedd by metric ) ex: 3.4s, 5.7m
+    // Returns -1 if invalid    
+    var toSeconds = function ( t_in, offset ) {
+      if ( !t_in ) {
+        return -1;
+      }
+      
+      var t = t_in.split( ":" ),
+          l = t.length - 1,
+          metric,
+          multiplier,
+          i;
+          
+      // Try clock time
+      if ( l >= 2 ) {
+        return parseInt( t[0], 10 )*3600 + parseInt( t[l-1], 10 )*60 + parseFloat( t[l], 10 );
+      }
+      
+      // Was not clock time, assume relative time
+      // Take metric from end of string (may not be single character)
+      // First find metric
+      for( i = t_in.length - 1; i >= 0; i-- ) {
+        if ( t_in[i] <= "9" && t_in[i] >= "0" ) {
+          break;
+        }
+      }
+      
+      // Point i at metric and normalize offsete time
+      i++;
+      metric = t_in.substr( i );
+      offset = offset || 0;
+      
+      // Determine multiplier for metric relative to seconds
+      if ( metric === "h" ) {
+        multiplier = 3600;
+      } else if ( metric === "m" ) {
+        multiplier = 60;
+      } else if ( metric === "s" ) {
+        multiplier = 1;
+      } else if ( metric === "ms" ) {
+        multiplier = 0.001;
+      } else {
+        return -1;
+      }
+      
+      // Valid multiplier
+      return parseFloat( t_in.substr( 0, i ) ) * multiplier + offset;
+    };
+
+    // creates an object of all atrributes keyd by name
+    var createTrack = function( name, attributes ) {
+      var track = {};
+      track[name] = attributes;
+      return track;
+    };
+    
+    // Parse a node for text content
+    var parseNode = function( node, timeOffset ) {
+      var sub = {};
+      
+      // Trim left and right whitespace from text and change non-explicit line breaks to spaces
+      sub.text = node.textContent.replace(/^[\s]+|[\s]+$/gm, "").replace(/(?:\r\n|\r|\n)/gm, "<br />");
+      sub.id = node.getAttribute( "xml:id" );
+      sub.start = toSeconds ( node.getAttribute( "begin" ), timeOffset );
+      sub.end = toSeconds( node.getAttribute( "end" ), timeOffset );
+      sub.target = region;
+      
+      if ( sub.end < 0 ) {
+        // No end given, infer duration if possible
+        // Otherwise, give end as MAX_VALUE
+        sub.end = toSeconds( node.getAttribute( "duration" ), 0 );
+        
+        if ( sub.end >= 0 ) {
+          sub.end += sub.start;
+        } else {
+          sub.end = Number.MAX_VALUE;
+        }
+      }
+      
+      return sub;
+    };
+    
+    // Parse the children of the given node
+    var parseChildren = function( node, timeOffset ) {
+      var currNode = node.firstChild,
+          sub,
+          newOffset;
+      
+      while ( currNode ) {
+        if ( currNode.nodeType === 1 ) {
+          if ( currNode.nodeName === "p" ) {
+            // p is a teextual node, process contents as subtitle
+            sub = parseNode( currNode, timeOffset );
+            returnData.data.push( createTrack( "subtitle", sub ) );
+            numTracks++;
+          } else if ( currNode.nodeName === "div" ) {
+            // div is container for subtitles, recurse
+            newOffset = toSeconds( currNode.getAttribute("begin") );
+            
+            if (newOffset < 0 ) {
+              newOffset = timeOffset;
+            }
+           
+            parseChildren( currNode, newOffset );
+          }
+        }
+        
+        currNode = currNode.nextSibling;
+      }
+    };
+    
+    // Null checks
+    if ( !data.xml || !data.xml.documentElement || !( node = data.xml.documentElement.firstChild ) ) {
+      return returnData;
+    }
+    
+    // Find body tag
+    while ( node.nodeName !== "body" ) {
+      node = node.nextSibling;
+    }
+    
+    region = "";
+    parseChildren( node, 0 );
+
+    return returnData;
+  });
+
+})( Popcorn );
+;
\ No newline at end of file
diff --git a/parsers/parserTTML/popcorn.parseTTML.unit.html b/parsers/parserTTML/popcorn.parseTTML.unit.html
new file mode 100644 (file)
index 0000000..bf281a4
--- /dev/null
@@ -0,0 +1,43 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 TTML 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="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parseTTML.js"></script>
+  <script src="popcorn.parseTTML.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.3 TTML 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'
+    data-timeline-sources="data/unit.TTML"
+    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>
diff --git a/parsers/parserTTML/popcorn.parseTTML.unit.js b/parsers/parserTTML/popcorn.parseTTML.unit.js
new file mode 100644 (file)
index 0000000..1930218
--- /dev/null
@@ -0,0 +1,103 @@
+test( "Popcorn 0.3 TTML Parser Plugin", function () {
+  var count = 0,
+      numSubs = 0,
+      sub,
+      poppercorn = Popcorn( "#video" ),
+      subs = [
+        {
+          id: "subtitle1",
+          text: "It seems a paradox, does it not,",
+          start: 0.76,
+          end: 3.45
+        },
+        {
+          id: "subtitle2",
+          text: "that the image formed on<br />the Retina should be inverted?",
+          start: 5,
+          end: 10
+        },
+        {
+          id: "subtitle3",
+          text: "It is puzzling, why is it<br />we do not see things upside-down?",
+          start: 10,
+          end: 16
+        },
+        {
+          id: "subtitle4",
+          text: "You have never heard the Theory,<br />then, that the Brain also is inverted?",
+          start: 17.2,
+          end: 23
+        },
+        {
+          id: "subtitle5",
+          text: "No indeed! What a beautiful fact!",
+          start: 23,
+          end: 27
+        },
+        {
+          id: "subtitle6",
+          text: "It seems a paradox, does it not,",
+          start: 27.76,
+          end: 30.45
+        },
+        {
+          id: "subtitle7",
+          text: "that the image formed on<br />the Retina should be inverted?",
+          start: 33,
+          end: 37
+        },
+        {
+          id: "subtitle8",
+          text: "It is puzzling, why is it<br />we do not see things upside-down?",
+          start: 37,
+          end: 43
+        },
+        {
+          id: "subtitle9",
+          text: "You have never heard the Theory,<br />then, that the Brain also is inverted?",
+          start: 44.2,
+          end: 50
+        },
+        {
+          id: "subtitle10",
+          text: "No indeed! What a beautiful fact!",
+          start: 50,
+          end: 54
+        }
+      ],
+      expects = subs.length*4 + 1;
+      
+  function plus() {
+    if ( ++count === expects ) {
+      start();
+    }
+  }
+  
+  poppercorn.parseTTML( 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++];
+        
+        strictEqual( evt.id, sub.id, "Correctly parsed id of " + evt.id );
+        plus();
+        strictEqual( evt.start, sub.start, "Correctly parsed start of " + evt.id + " at " + evt.start );
+        plus();
+        strictEqual( evt.text, sub.text, "Correctly parsed text of " + evt.id + " at " + evt.start );
+        plus();
+        strictEqual( evt.end, sub.end, "Correctly parsed end of " + evt.id + " at " + evt.start );
+        plus();
+      }
+    });
+    
+    strictEqual( subs.length, numSubs, "Parsed all subtitles" );
+    plus();
+
+  }, 500);
+  
+});