forked from maboa/hyperaudiopad
/
parserSRT.htm
196 lines (138 loc) · 5.76 KB
/
parserSRT.htm
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
<!DOCTYPE html>
<!-- BEGIN html -->
<html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en-US">
<!-- Based on an Orman Clark design (http://www.premiumpixels.com) -->
<!-- BEGIN head -->
<head>
<!-- Meta Tags -->
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<!-- Title -->
<title>SRT Parser</title>
<!-- Stylesheets -->
<!--<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Droid+Serif:regular,italic,bold,bolditalic" type="text/css" />
<link rel="stylesheet" href="http://fonts.googleapis.com/css?family=Droid+Sans:regular,bold" type="text/css" /> -->
<link rel="stylesheet" href="style.css" type="text/css" media="screen" />
<link rel="stylesheet" type="text/css" media="all" href="scrollpane.css" />
<!-- Move jQuery Local! -->
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript" src="js/popcorn.js"></script>
<!--<script type="text/javascript" src="js/popcorn.parserSRT.js"></script>-->
<script type="text/javascript">
$(document).ready(function(){
// stolen wholesale from popcorn.parserSRT.js
function parseSRT(data) {
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 outputString = "";
// Here is where the magic happens
// Split on line breaks
lines = data.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 />" );
// @secretrobotrons splitter
var swords = sub.text.split(' ');
var sduration = sub.end - sub.start;
var stimeStep = sduration/swords.length;
for (var si=0, sl=swords.length; si<sl; ++si) {
var stime = Math.round((sub.start + si*stimeStep) * 1000);
var stext = swords[si];
var ssafeText = stext.replace('"', '\\"');
outputString += '<span m="'+stime+'" oval="'+ssafeText+'">'+stext+'</span> '+'\n';
}
}
return outputString;
}
/*$.get('test.srt', function(data) {
console.log(data);
console.log(parseSRT(data));
});*/
$('#transform').click(function() {
var srt = $('#subtitles').val();
/*var regex = /<br\s*[\/]?>/gi;
srt = srt.replace(regex,'\n');
regex = />/gi;
srt = srt.replace(regex,'>');
//console.log(srt);*/
var ht = parseSRT(srt);
/*ht = ht.replace(/\r\n|\r|\n/gi, '<br/>'); */
$('#htranscript').val(ht);
//console.log($('#subtitles').text());
});
});
</script>
</head>
<body class="single single-post postid-4004 gecko layout-2cr">
<div id="container">
<div id="header" class="clearfix">
<div id="title">
<h1>SRT to Hypertranscript</h1>
</div><!--END #title-->
</div><!--END #header-->
<div id="content">
<div id="sidebar">
<p><a id="home" href="index.htm">✪ home</a></p>
<p>Paste your SRT file into the next pane ☞ <br/>and press the transform button below.</p>
<a id="transform" href="#">Transform</a>
</div><!-- end #sidebar -->
<h2 id="script-title" class="entry-title">SRT ⤵</h2>
<div id="original-hldr" style="background-color:#fff">
<textarea id="subtitles" class="entry-content" rows="40" cols="60"></textarea>
</div>
<div id="htranscript-hldr" style="background-color:#fff">
<textarea id="htranscript" class="entry-content" rows="40" cols="60"></textarea>
</div>
</div><!-- end #content -->
</div><!-- end #container -->
</html>