1
0
Fork 0
forked from 0x2620/oxjs

Ox.parseSRT

This commit is contained in:
rolux 2011-05-13 00:12:41 +02:00
commit eef79f318a
4 changed files with 5709 additions and 40 deletions

View file

@ -3940,6 +3940,36 @@ Ox.parsePath = function(str) {
};
}
/*@
Ox.parseSRT <f> Parses an srt subtitle file
(str) -> <o> Parsed subtitles
in <n> In point (sec)
out <n> Out point (sec)
text <s> Text
str <s> Contents of an srt subtitle file
> Ox.parseSRT('1\n01:02:00,000 --> 01:02:03,400\nHello World')
[{'in': 3720, out: 3723.4, text: 'Hello World'}]
@*/
Ox.parseSRT = function(str) {
return str.replace(/\r\n/g, '\n').replace(/\n+$/, '').split('\n\n')
.map(function(block) {
var lines = block.split('\n'), points;
lines.shift();
points = lines.shift().split(' --> ').map(function(point) {
return point.replace(',', ':').split(':')
.reduce(function(prev, curr, i) {
return prev + parseInt(curr, 10) *
[3600, 60, 1, 0.001][i];
}, 0);
});
return {
'in': points[0],
out: points[1],
text: lines.join('\n')
};
});
};
/*@
Ox.repeat <f> Repeat a value multiple times
Works for arrays, numbers and strings