forked from mozilla/popcorn-js
/
popcorn.parserSRT.js
executable file
·119 lines (99 loc) · 3.99 KB
/
popcorn.parserSRT.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
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", 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, "<" ).replace( />/g, ">" );
// 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( /<(\/?(font|b|u|i|s))((\s+(\w|\w[\w\-]*\w)(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)(\/?)>/gi, "<$1$3$7>" );
sub.text = sub.text.replace( /\\N/gi, "<br />" );
subs.push( createTrack( "subtitle", sub ) );
}
retObj.data = subs;
return retObj;
});
})( Popcorn );