Skip to content
This repository has been archived by the owner on Jun 29, 2018. It is now read-only.

Added basic SAMI/SMI parser #471

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 27 additions & 0 deletions parsers/parserSAMI/data/data.matchingtagsinbody.smi
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<SAMI>
<HEAD>
<STYLE TYPE="text/css">
<!--
P { font-family: Arial; font-weight: normal; color: white; background-color: black; text-align: center; }

#Source {color: red; background-color: blue; font-family: Courier; font-size: 12pt; font-weight: normal; text-align: left; }

.ENUSCC { name: English; lang: en-US ; SAMIType: CC ; }
-->
</STYLE>
</HEAD>
<BODY>
<SYNC Start=0>
<P Class=ENUSCC>SAMI 0000 text</P>
</SYNC>
<SYNC Start=1000>
<P Class=ENUSCC>SAMI 1000 text</P>
</SYNC>
<SYNC Start=2000>
<P Class=ENUSCC>SAMI 2000 text</P>
</SYNC>
<SYNC Start=3000>
<P Class=ENUSCC>SAMI 3000 text</P>
</SYNC>
</BODY>
</SAMI>
27 changes: 27 additions & 0 deletions parsers/parserSAMI/data/data.mixedmatchingtagsinbody.smi
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<SAMI>
<HEAD>
<STYLE TYPE="text/css">
<!--
P { font-family: Arial; font-weight: normal; color: white; background-color: black; text-align: center; }

#Source {color: red; background-color: blue; font-family: Courier; font-size: 12pt; font-weight: normal; text-align: left; }

.ENUSCC { name: English; lang: en-US ; SAMIType: CC ; }
-->
</STYLE>
</HEAD>
<BODY>
<SYNC Start=0>
<P Class=ENUSCC>SAMI 0000 text</P>
<P Class=ENUSCC>SAMI 0001 text</P>
</SYNC>
<SYNC Start=1000>
<P Class=ENUSCC>SAMI 1000 text
<SYNC Start=2000>
<P Class=ENUSCC>SAMI 2000 text</P>
</SYNC>
<SYNC Start=3000>
<P Class=ENUSCC>SAMI 3000 text</P>
</SYNC>
</BODY>
</SAMI>
23 changes: 23 additions & 0 deletions parsers/parserSAMI/data/data.nomatchingtagsinbody.smi
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<SAMI>
<HEAD>
<STYLE TYPE="text/css">
<!--
P { font-family: Arial; font-weight: normal; color: white; background-color: black; text-align: center; }

#Source {color: red; background-color: blue; font-family: Courier; font-size: 12pt; font-weight: normal; text-align: left; }

.ENUSCC { name: English; lang: en-US ; SAMIType: CC ; }
-->
</STYLE>
</HEAD>
<BODY>
<SYNC Start=0>
<P Class=ENUSCC>SAMI 0000 text
<SYNC Start=1000>
<P Class=ENUSCC>SAMI 1000 text
<SYNC Start=2000>
<P Class=ENUSCC>SAMI 2000 text
<SYNC Start=3000>
<P Class=ENUSCC>SAMI 3000 text
</BODY>
</SAMI>
57 changes: 57 additions & 0 deletions parsers/parserSAMI/popcorn.parserSAMI.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<!DOCTYPE html>
<html>
<head>
<title>Popcorn SAMI parser 1.0 Plug-in Demo</title>
<script src="../../popcorn.js"></script>
<script src="../../modules/parser/popcorn.parser.js"></script>
<script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
<script src="popcorn.parserSAMI.js"></script>
<script>
document.addEventListener('DOMContentLoaded', function () {
var p = Popcorn('#video')
.parseSAMI('data/data.matchingtagsinbody.smi')
.volume(0)
.play();
}, false);
</script>
<style type="text/css">
.videoContent
{
float:right;
padding: 5px;
}
</style>
</head>
<body>
<h1 id="qunit-header">Popcorn SAMI parser 1.0 Plug-in Demo</h1>

<div class="videoContent">
<video id="video"
controls
width= "310px"
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>Expected Output</h4>
<ul>
<li>From 0 to 1 seconds, "SAMI 0000 text" is shown in the video.</li>
<li>From 1 to 2 seconds, "SAMI 1000 text" is shown in the video.</li>
<li>From 2 to 3 seconds, "SAMI 2000 text" is shown in the video.</li>
<li>From 3 to 6 seconds, "SAMI 3000 text" is shown in the video.</li>
</ul>

<h4>Subtitle Source<h4>
<!--iframe id="srcDisplay" src="data/data.smi" style="width: 100%; height: 15em;" ></iframe-->
</body>
</html>
81 changes: 81 additions & 0 deletions parsers/parserSAMI/popcorn.parserSAMI.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// PARSER: SAMI 1.0
(function (Popcorn) {
/**
* SAMI popcorn parser plug-in
* Parses subtitle files in the SAMI format.
*
* @param {Object} data
*
*/
Popcorn.parser('parseSAMI', function (data) {
var subtitles = {
title: '',
remote: '',
data: []
},
_data,
_maxDisplayTime = 3;

if (!data.text) {
return subtitles;
}

_data = data.text.replace(/body/gi, 'BODY')
.replace(/<sync/gi, '<SYNC')
.replace(/start=/gi, 'START=')
.replace(/<p/gi, '<P')
.replace(/<\/p>/gi, '</P>')
.replace(/<\/sync>/gi, '</SYNC>');

_data = _data.substring(6 + _data.indexOf('<BODY>'), _data.indexOf('</BODY>'));

var nextStartPosition = _data.indexOf('START='),
nextEndPosition = -1,
nextLineStartPosition = -1,
nextEndPPosition = -1,
nextEndSyncPosition = -1,
nextStartSyncPosition = - 1,
startTime;
while (-1 !== nextStartPosition) {
nextStartPosition += 6;
nextEndPosition = _data.indexOf('>', nextStartPosition);
startTime = (_data.substring(nextStartPosition, nextEndPosition).replace(/"/g, '')) / 1000;

nextStartPosition = _data.indexOf('>', _data.indexOf('<P', nextEndPosition)) + 1;
nextEndPPosition = _data.indexOf('</P>', nextStartPosition);
nextEndSyncPosition = _data.indexOf('</SYNC>', nextStartPosition);
nextStartSyncPosition = _data.indexOf('<SYNC', nextStartPosition);
nextLineStartPosition = _data.indexOf('<P', nextStartPosition);

nextEndPosition = Math.min(-1 === nextEndPPosition ? Number.MAX_VALUE : nextEndPPosition,
Math.min(-1 === nextEndSyncPosition ? Number.MAX_VALUE : nextEndSyncPosition,
Math.min(-1 === nextStartSyncPosition ? Number.MAX_VALUE : nextStartSyncPosition, _data.length)));

if (-1 !== nextLineStartPosition && nextEndPosition > nextLineStartPosition) {
// More data lines before nextEndPosition - those will be discarded
nextEndPosition = nextLineStartPosition;
}

subtitles.data.push({
subtitle: {
id: subtitles.data.length,
text: (_data.substring(nextStartPosition, nextEndPosition) || '').replace(/(style([^>]+)"|<div([^>]+)>|<\/div>|(\r\n|\n|\r)|^[.\s]+|[.\s]+$)/gi, ''),
start: startTime,
end: null
}
});

nextStartPosition = _data.indexOf('START=', nextEndPosition);
}

for(var i = 0; subtitles.data.length > i; i++){
subtitles.data[i].subtitle.end = subtitles.data[i].subtitle.start + ((subtitles.data.length - 1 === i)
? _maxDisplayTime
: (_maxDisplayTime >= subtitles.data[1 + i].subtitle.start - subtitles.data[i].subtitle.start)
? subtitles.data[1 + i].subtitle.start - subtitles.data[i].subtitle.start
: _maxDisplayTime);
}

return subtitles;
});
})(Popcorn);
44 changes: 44 additions & 0 deletions parsers/parserSAMI/popcorn.parserSAMI.unit.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<!DOCTYPE html>
<html>
<head>
<title>Popcorn SAMI parser 1.0 Plug-in Test</title>
<link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
<script src="../../test/qunit/qunit.js"></script>
<script src="../../popcorn.js"></script>
<script src="../../modules/parser/popcorn.parser.js"></script>

<script src="../../plugins/subtitle/popcorn.subtitle.js"></script>
<script src="popcorn.parserSAMI.js"></script>
<script src="popcorn.parserSAMI.unit.js"></script>
<script src="../../test/inject.js"></script>
<script src="../../test/popcorn.inject.js"></script>
</head>
<body>
<h1 id="qunit-header">Popcorn SAMI parser 1.0 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"'>

<source id="webm"
src="../../test/trailer.webm"
type='video/webm; codecs="vp8, vorbis"'>

<p>Your user agent does not support the HTML5 Video element.</p>
</video>
</body>
</html>
Loading