-
Notifications
You must be signed in to change notification settings - Fork 238
Expand file tree
/
Copy pathttml2webvtt.js
More file actions
87 lines (70 loc) · 1.67 KB
/
ttml2webvtt.js
File metadata and controls
87 lines (70 loc) · 1.67 KB
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
(function($) {
AblePlayer.prototype.computeEndTime = function(startTime, durationTime) {
var SECONDS = 0;
var MINUTES = 1;
var HOURS = 2;
var startParts = startTime
.split(':')
.reverse()
.map(function(value) {
return parseFloat(value);
});
var durationParts = durationTime
.split(':')
.reverse()
.map(function(value) {
return parseFloat(value);
});
var endTime = startParts
.reduce(function(acc, val, index) {
var sum = val + durationParts[index];
if (index === SECONDS) {
if (sum > 60) {
durationParts[index + 1] += 1;
sum -= 60;
}
sum = sum.toFixed(3);
}
if (index === MINUTES) {
if (sum > 60) {
durationParts[index + 1] += 1;
sum -= 60;
}
}
if (sum < 10) {
sum = '0' + sum;
}
acc.push(sum);
return acc;
}, [])
.reverse()
.join(':');
return endTime;
};
AblePlayer.prototype.ttml2webvtt = function(contents) {
var thisObj = this;
var xml = thisObj.convert.xml2json(contents, {
ignoreComment: true,
alwaysChildren: true,
compact: true,
spaces: 2
});
var vttHeader = 'WEBVTT\n\n\n';
var captions = JSON.parse(xml).tt.body.div.p;
var vttCaptions = captions.reduce(function(acc, value, index) {
var text = value._text;
var isArray = Array.isArray(text);
var attributes = value._attributes;
var endTime = thisObj.computeEndTime(attributes.begin, attributes.dur);
var caption =
thisObj.computeEndTime(attributes.begin, '00:00:0') +
' --> ' +
endTime +
'\n' +
(isArray ? text.join('\n') : text) +
'\n\n';
return acc + caption;
}, vttHeader);
return vttCaptions;
};
})(jQuery);