]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
[#290] Remake branch
authorstevenaw <sweerdenburg@gmail.com>
Thu, 3 Feb 2011 21:38:35 +0000 (16:38 -0500)
committerstevenaw <sweerdenburg@gmail.com>
Thu, 3 Feb 2011 21:38:35 +0000 (16:38 -0500)
parsers/parserSRT/data/data.srt [new file with mode: 0644]
parsers/parserSRT/data/unit.srt [new file with mode: 0644]
parsers/parserSRT/popcorn.parserSRT.html [new file with mode: 0644]
parsers/parserSRT/popcorn.parserSRT.js [new file with mode: 0644]
parsers/parserSRT/popcorn.parserSRT.unit.html [new file with mode: 0644]
parsers/parserSRT/popcorn.parserSRT.unit.js [new file with mode: 0644]

diff --git a/parsers/parserSRT/data/data.srt b/parsers/parserSRT/data/data.srt
new file mode 100644 (file)
index 0000000..d9f4945
--- /dev/null
@@ -0,0 +1,16 @@
+1
+00:00:02.400 --> 00:00:05.200
+[Background Music Playing]
+
+2
+00:00:06.000 --> 00:00:13,700
+<true> To say that 2 is greater than three (3 > 2) would be lying<true>
+
+3
+00:00:15,712 --> 00:00:17,399 X1:000 X2:000 Y1:050 Y2:100
+Oh my god, Watch out!
+It's coming!!
+
+4
+00:00:25,712 --> 00:00:30,399
+[Bird noises]
diff --git a/parsers/parserSRT/data/unit.srt b/parsers/parserSRT/data/unit.srt
new file mode 100644 (file)
index 0000000..093aa25
--- /dev/null
@@ -0,0 +1,40 @@
+1
+00:00:02.400 --> 00:00:05.200
+[Background Music Playing]
+
+2
+00:00:15,712 --> 00:00:17,399 X1:000 X2:000 Y1:050 Y2:100
+Oh my god, Watch out!
+It's coming!!
+
+3
+00:00:25,712 --> 00:00:30,399
+[Bird noises]
+
+4
+00:00:31,000 --> 00:00:31,999
+This text is <font color="red">RED</font> and has not been {\pos(142,120)}positioned.
+
+5
+00:00:32,000 --> 00:00:32,999
+This is a\nnew line, as is\Nthis
+
+6
+00:00:33,000 --> 00:00:33,999
+This contains nested <b>bold, <i>italic, <u>underline</u> and <s>strike-through</s></u></i></b> HTML tags
+
+7
+00:00:34,000 --> 00:00:34,999
+Unclosed but <b>supported HTML tags are left in, {\i1} SSA italics aren't
+
+8
+00:00:35,000 --> 00:00:35,999
+<ggg>Unsupported</ggg> HTML tags are escaped and left in, even if <hhh>not closed.
+
+9
+00:00:36,000 --> 00:00:36,999
+Multiple {\pos(142,120)\b1}SSA tags are stripped
+
+10
+00:00:37,000 --> 00:00:37,999
+Greater than (<) and less than (>) are shown
diff --git a/parsers/parserSRT/popcorn.parserSRT.html b/parsers/parserSRT/popcorn.parserSRT.html
new file mode 100644 (file)
index 0000000..3aa8575
--- /dev/null
@@ -0,0 +1,56 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 SRT parser Plug-in Demo</title>
+  
+  <script src="../../popcorn.js"></script>
+  <script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parserSRT.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 SRT parser Plug-in Demo</h1>
+  
+  <p>From 2.4 to 5.2 seconds, "[Background Music Playing]" is shown
+  <br />From 6 to 13.7 seconds, "&lt;true&gt; To say that 2 is greater than three (3 &gt; 2) would be lying&lt;true&gt;" is shown
+  <br />From 15.712 to 17.399 seconds, "Oh my god, Watch out! It's coming!!" is shown
+  <br />From 25.712 to 30.399 seconds, "[Bird noises]" is shown</p>
+  <div>
+    <video id='video'
+      controls
+      width="250px"
+      data-timeline-sources="data/data.srt"
+      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.srt"></iframe>
+  
+  <style>
+    .displays {
+      width: 300px;
+      height: 300px;
+    }
+  </style>
+
+</body>
+</html>
diff --git a/parsers/parserSRT/popcorn.parserSRT.js b/parsers/parserSRT/popcorn.parserSRT.js
new file mode 100644 (file)
index 0000000..2ab5f83
--- /dev/null
@@ -0,0 +1,119 @@
+// PARSER: 0.3 SRT
+
+(function (Popcorn) {
+  /**
+   * SRT popcorn parser plug-in 
+   * Parses subtitle files in the SRT format.
+   * Times are expected in HH:MM:SS,MIL format, though HH:MM:SS.MIL also supported
+   * Ignore styling, which may occur after the end time or in-text
+   * While not part of the "official" spec, majority of players support HTML and SSA styling tags
+   * SSA-style tags are stripped, HTML style tags are left for the browser to handle:
+   *    HTML: <font>, <b>, <i>, <u>, <s>
+   *    SSA:  \N or \n, {\cmdArg1}, {\cmd(arg1, arg2, ...)}
+   
+   * Data parameter is given by Popcorn, will need a text.
+   * Text is the file contents to be parsed
+   * 
+   * @param {Object} data
+   * 
+   * Example:
+    1
+    00:00:25,712 --> 00:00:30.399
+    This text is <font color="red">RED</font> and has not been {\pos(142,120)} positioned.
+    This takes \Nup three \nentire lines.
+    This contains nested <b>bold, <i>italic, <u>underline</u> and <s>strike-through</s></u></i></b> HTML tags
+    Unclosed but <b>supported tags are left in
+    <ggg>Unsupported</ggg> HTML tags are left in, even if <hhh>not closed.
+    SSA tags with {\i1} would open and close italicize {\i0}, but are stripped
+    Multiple {\pos(142,120)\b1}SSA tags are stripped
+   */
+  Popcorn.parser( "parseSRT", "SRT", function( data ) {
+
+    // declare needed variables
+    var retObj = {
+          title: "",
+          remote: "",
+          data: []
+        },
+        subs = [],
+        i = 0,
+        len = 0,
+        idx = 0,
+        lines,
+        time,
+        text,
+        sub;
+    
+    // Simple function to convert HH:MM:SS,MMM or HH:MM:SS.MMM to SS.MMM
+    // Assume valid, returns 0 on error
+    var toSeconds = function( t_in ) {
+      var t = t_in.split( ':' );
+      
+      try {
+        var s = t[2].split( ',' );
+        
+        // Just in case a . is decimal seperator
+        if ( s.length === 1 ) {
+          s = t[2].split( '.' );
+        }
+        
+        return parseFloat( t[0], 10 )*3600 + parseFloat( t[1], 10 )*60 + parseFloat( s[0], 10 ) + parseFloat( s[1], 10 )/1000;
+      } catch ( e ) {
+        return 0;
+      }
+    };
+    
+    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;
+    
+    for( i=0; i < len; i++ ) {
+      sub = {};
+      text = [];
+      
+      sub.id = parseInt( lines[i++], 10 );
+      
+      // Split on '-->' delimiter, trimming spaces as well
+      time = lines[i++].split( /[\t ]*-->[\t ]*/ );
+      
+      sub.start = toSeconds( time[0] );
+      
+      // So as to trim positioning information from end
+      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 into 1 line, SSA-style linebreaks
+      // Strip out other SSA-style tags
+      sub.text = text.join( "\\N" ).replace( /\{(\\[\w]+\(?([\w\d]+,?)+\)?)+\}/gi, "" );
+      
+      // Escape HTML entities
+      sub.text = sub.text.replace( /</g, "&lt;" ).replace( />/g, "&gt;" );
+      
+      // Unescape great than and less than when it makes a valid html tag of a supported style (font, b, u, s, i)
+      // Modified version of regex from Phil Haack's blog: http://haacked.com/archive/2004/10/25/usingregularexpressionstomatchhtml.aspx
+      // Later modified by kev: http://kevin.deldycke.com/2007/03/ultimate-regular-expression-for-html-tag-parsing-with-php/
+      sub.text = sub.text.replace( /&lt;(\/?(font|b|u|i|s))((\s+(\w|\w[\w\-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)(\/?)&gt;/gi, "<$1$3$7>" );
+      sub.text = sub.text.replace( /\\N/gi, "<br />" );
+      subs.push( createTrack( "subtitle", sub ) );
+    }
+    
+    retObj.data = subs;
+    return retObj;
+  });
+
+})( Popcorn );
diff --git a/parsers/parserSRT/popcorn.parserSRT.unit.html b/parsers/parserSRT/popcorn.parserSRT.unit.html
new file mode 100644 (file)
index 0000000..572a9f4
--- /dev/null
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn 0.3 SRT parser Plug-in Test</title>
+  <link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen"></link>
+  <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.parserSRT.js"></script>
+  <script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
+  <script src="popcorn.parserSRT.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn 0.3 SRT 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.srt"
+    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/parserSRT/popcorn.parserSRT.unit.js b/parsers/parserSRT/popcorn.parserSRT.unit.js
new file mode 100644 (file)
index 0000000..11474e1
--- /dev/null
@@ -0,0 +1,108 @@
+test("Popcorn 0.3 SRT Parser Plugin", function () {
+  
+  var expects = 0,
+      count = 0,
+      sub = {},
+      numSubs = 0,
+      poppercorn = Popcorn( "#video" ),
+      expectedSubs = [
+        {
+          id: 1,
+          start: 2.4,
+          end: 5.2,
+          text: "[Background Music Playing]"
+        },
+        {
+          id: 2,
+          start: 15.712,
+          end: 17.399,
+          text: "Oh my god, Watch out!<br />It's coming!!"
+        },
+        {
+          id: 3,
+          start: 25.712,
+          end: 30.399,
+          text: "[Bird noises]"
+        },
+        {
+          id: 4,
+          start: 31,
+          end: 31.999,
+          text: 'This text is <font color="red">RED</font> and has not been positioned.'
+        },
+        {
+          id: 5,
+          start: 32,
+          end: 32.999,
+          text: "This is a<br />new line, as is<br />this"
+        },
+        {
+          id: 6,
+          start: 33,
+          end: 33.999,
+          text: "This contains nested <b>bold, <i>italic, <u>underline</u> and <s>strike-through</s></u></i></b> HTML tags"
+        },
+        {
+          id: 7,
+          start: 34,
+          end: 34.999,
+          text: "Unclosed but <b>supported HTML tags are left in,  SSA italics aren't"
+        },
+        {
+          id: 8,
+          start: 35,
+          end: 35.999,
+          text: "&lt;ggg&gt;Unsupported&lt;/ggg&gt; HTML tags are escaped and left in, even if &lt;hhh&gt;not closed."
+        },
+        {
+          id: 9,
+          start: 36,
+          end: 36.999,
+          text: "Multiple SSA tags are stripped"
+        },
+        {
+          id: 10,
+          start: 37,
+          end: 37.999,
+          text: "Greater than (&lt;) and less than (&gt;) are shown"
+        }
+      ];
+      
+  function plus() {
+    if ( ++count === expects ) {
+      start();
+    }
+  }
+  
+  poppercorn.parseSRT( document.getElementById( 'video' ).getAttribute( 'data-timeline-sources' ) );
+  
+  expects = expectedSubs.length*4+1;
+  expect( expects );
+  stop( 5000 );
+  
+  // Allow load time
+  setTimeout(function () {
+    Popcorn.forEach( poppercorn.getTrackEvents(), function( evt ) {
+      if( evt._natives.type === "subtitle" ) {
+        sub = expectedSubs[numSubs++];
+        
+        equals( sub.id, evt.id, "Correct id" );
+        plus();
+        
+        equals( sub.start, evt.start, "Correct start" );
+        plus();
+        
+        equals( sub.end, evt.end, "Correct end" );
+        plus();
+        
+        equals( sub.text, evt.text, "Correct text" );
+        plus();
+      }
+    });
+    
+    equals( expectedSubs.length, numSubs, "Correctly parsed all subtitles" );
+    plus();
+
+  }, 500);
+  
+});
\ No newline at end of file