-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathvtt-composer.js
42 lines (32 loc) · 1.03 KB
/
vtt-composer.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
/*
* Basic json to vtt conversion
* broadly inspired by subtitles-parser node module
*/
function convert(vttDataStructure,cb){
var vttString = "WEBVTT\n\n"
for(var i = 0; i< vttDataStructure.length; i++){
vttString+=vttDataStructure[i].id+"\n"+fromSecondsWithDot(vttDataStructure[i].startTime)+" --> "+fromSecondsWithDot(vttDataStructure[i].endTime)+"\n"+vttDataStructure[i].text+"\n\n"
}
if(cb){cb(vttString)}
return vttString;
}
function fromSecondsWithDot(val) {
var measures = [ 3600000, 60000, 1000 ];
var time = [];
for (var i in measures) {
var res = (val / measures[i] >> 0).toString();
if (res.length < 2) res = '0' + res;
val %= measures[i];
time.push(res);
}
var ms = val.toString();
if (ms.length < 3) {
for (i = 0; i <= 3 - ms.length; i++) ms = '0' + ms;
}
return time.join(':') + '.' + ms;
};
module.exports = {
toVtt: function(dataJson,cb) {
return convert(dataJson,cb);
}
}