Parse a SRT file with jQuery Javascript

Clash Royale CLAN TAG#URR8PPPParse a SRT file with jQuery Javascript
I'm trying to parse .srt but I get an internal error and I can't figure out what is it.
.srt
Here is my code:
var subtitles;
jQuery.get('SB_LKRG-eng.srt', function(data) {
//alert(data);
function strip(s) {
return s.replace(/^s+|s+$/g,"");
}
srt = data.replace(/rn|r|n/g, 'n');
//alert(srt);
srt = strip(srt);
//alert(srt);
var srt_ = srt.split('nn');
alert(srt_);
var cont = 0;
for(s in srt_) {
st = srt_[s].split('n');
alert(st);
if(st.length >=2) {
n = st[0];
i = strip(st[1].split(' --> ')[0]);
o = strip(st[1].split(' --> ')[1]);
t = st[2];
if(st.length > 2) {
for(j=3; j<st.length;j++)
t += 'n'+st[j];
}
subtitles[cont].number = n;
subtitles[cont].start = i;
subtitles[cont].end = o;
subtitles[cont].text = t;
//alert(subtitles[cont].start);
}
cont++;
}
});
I can extract the first 4 subtitles and then the code stops and breaks exception: TypeError, I can't understand why...
Here a sample of the subtitles file:
exception: TypeError
1
00:00:01,000 --> 00:00:04,000
Descargados de www.AllSubs.org
2
00:00:49,581 --> 00:00:52,049
Bueno, tienes que escapar, tengo que ir a jugar
3
00:00:52,084 --> 00:00:55,178
Tengo que encontrar un día que está lleno de nada más que sol
4
00:00:55,220 --> 00:00:57,552
Crucero por la calle, moviéndose al compás
5
00:00:57,589 --> 00:01:00,683
Todos los que conoces está teniendo nada más que diversión
6
00:01:00,726 --> 00:01:03,251
Deja todo detrás de ti
7
00:01:03,295 --> 00:01:06,128
Siente esas palmeras soplan
8
00:01:06,165 --> 00:01:09,157
La gente en el norte no puede encontrar
9
00:01:09,201 --> 00:01:11,829
Están fuera de palear la nieve
10
00:01:11,870 --> 00:01:14,998
El tiempo para moverse, pero no seas lento
11
00:01:15,040 --> 00:01:17,941
En sus marcas, prepárate para ir
Part of the code is from: http://v2v.cc/~j/jquery.srt/jquery.srt.js
http://v2v.cc/~j/jquery.srt/jquery.srt.js
Can anyone help me?
Thank you
.srt
@hindmost I have posted a sample of the
.srt file, thanks!– Sergi
Oct 15 '15 at 10:26
.srt
@LGSon I have edited the post because this function is not important for my problem, thanks!!
– Sergi
Oct 15 '15 at 10:28
Shouldn't the
cont++; be inside the for loop?– LGSon
Oct 15 '15 at 10:30
cont++;
for
Sorry, I ment inside the
if(st.length >=2) { statement?– LGSon
Oct 15 '15 at 10:32
if(st.length >=2) {
2 Answers
2
var PF_SRT = function() {
//SRT format
var pattern = /(d+)n([d:,]+)s+-{2}>s+([d:,]+)n([sS]*?(?=n{2}|$))/gm;
var _regExp;
var init = function() {
_regExp = new RegExp(pattern);
};
var parse = function(f) {
if (typeof(f) != "string")
throw "Sorry, Parser accept string only.";
var result = ;
if (f == null)
return _subtitles;
f = f.replace(/rn|r|n/g, 'n')
while ((matches = pattern.exec(f)) != null) {
result.push(toLineObj(matches));
}
return result;
}
var toLineObj = function(group) {
return {
line: group[1],
startTime: group[2],
endTime: group[3],
text: group[4]
};
}
init();
return {
parse: parse
}
}();
jQuery.get('demo.srt')
.done(function(text) {
try {
//Array with {line, startTime, endTime, text}
var result = PF_SRT.parse(text);
} catch (e) {
//handle parsing error
}
});
Demo
https://jsfiddle.net/5v7wz4bq/
When I run your code in this fiddle demo, jsfiddle.net/876sscf7/4, it errors out in the middle of the first iteration. Could you explain why your demo doesn't ? ... Btw, when I add my fix for the inner split, then it works jsfiddle.net/876sscf7/5
– LGSon
Oct 15 '15 at 12:02
Your fiddle makes st[1].split(' --> ')[1] empty variable, because script on line2 should perform a global match, find all matches rather than stopping after the first match. jsfiddle.net/21kd95cj
– Parfait
Oct 16 '15 at 2:38
Wow .... I see that now ... one of my typical quickfix error .. thanks for your time
– LGSon
Oct 16 '15 at 6:26
your code is very good. but on some
.srt files I see some rows only have time without text and your code stop on this row! can you help me? jsfiddle.net/21kd95cj/4 on row 8 thanks– mghhgm
2 days ago
.srt
on row 8
@mghhgm Try this jsfiddle.net/5v7wz4bq
– Parfait
11 mins ago
Here is one problem:
o = strip(st[1].split(' --> ')[1]);
At this line, when there isn't any ' --> ' to split, the returned length of the array is 1, which errors when you ask for array item 2.
And here is another:
subtitles[cont].number = n;
....
Neither is the subtitles declared, nor its properties .number, ... etc.
subtitles
.number
Update
Here is a sample that works (switched the jQuery "read srt file" part for the data)
var data = document.getElementById("data").innerHTML;
data = data.replace(/>/g,">");
function strip(s) {
return s.replace(/^s+|s+$/g,"");
}
srt = data.replace(/rn|r|n/g, 'n');
srt = strip(srt);
var srt_ = srt.split('nn');
var cont = 0;
var subtitles = ;
for(s in srt_) {
st = srt_[s].split('n');
if(st.length >=2) {
var st2 = st[1].split(' --> ');
var t = st[2];
if(st.length > 2) {
for(j=3; j < st.length;j++)
t += 'n'+st[j];
}
subtitles[cont] = { number : st[0],
start : st2[0],
end : st2[1],
text : t
}
console.log(subtitles[cont].number + ": " + subtitles[cont].text);
document.body.innerHTML += subtitles[cont].number + ": " + subtitles[cont].text + "<br>";
cont++;
}
}
<div id="data" style="display:none">1
00:00:01,000 --> 00:00:04,000
Descargados de www.AllSubs.org
2
00:00:49,581 --> 00:00:52,049
Bueno, tienes que escapar, tengo que ir a jugar
3
00:00:52,084 --> 00:00:55,178
Tengo que encontrar un día que está lleno de nada más que sol
4
00:00:55,220 --> 00:00:57,552
Crucero por la calle, moviéndose al compás
5
00:00:57,589 --> 00:01:00,683
Todos los que conoces está teniendo nada más que diversión
6
00:01:00,726 --> 00:01:03,251
Deja todo detrás de ti
7
00:01:03,295 --> 00:01:06,128
Siente esas palmeras soplan
8
00:01:06,165 --> 00:01:09,157
La gente en el norte no puede encontrar
9
00:01:09,201 --> 00:01:11,829
Están fuera de palear la nieve
10
00:01:11,870 --> 00:01:14,998
El tiempo para moverse, pero no seas lento
11
00:01:15,040 --> 00:01:17,941
En sus marcas, prepárate para ir
</div>
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Could you post the content of
.srtfile (or its part)?– hindmost
Oct 15 '15 at 10:22