-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
67 lines (66 loc) · 1.46 KB
/
index.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
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
var xml2js = require('xml2js')
var parseMs = require('parse-ms')
var leftPad = require('left-pad')
module.exports = function (timedTextXML, cb) {
xml2js.parseString(timedTextXML, function (err, result) {
if (err) {
return cb(err)
}
var timedtext = result.timedtext
switch (timedtext.$.format) {
case '3':
parseFormat3(timedtext.body[0].p, cb)
break
default:
cb(new Error([
'Unknown timedtext format',
'`' + timedText.$.format + '`.'
].join(' ')))
}
})
}
function mergeS(s) {
if (s) {
return s.map(function(it) {
return it._
}).join('')
}
else {
return ''
}
}
function parseFormat3(p, cb) {
var s = ''
try {
for (var i = 0; i < p.length; ++i) {
var t = parseInt(p[i].$.t, 10)
var d = parseInt(p[i].$.d, 10)
d = parseMs(t + d)
t = parseMs(t)
var startTime = [
leftPad(t.hours, 2, 0),
leftPad(t.minutes, 2, 0),
leftPad(t.seconds, 2, 0)
].join(':') + ',' + t.milliseconds
var endTime = [
leftPad(d.hours, 2, 0),
leftPad(d.minutes, 2, 0),
leftPad(d.seconds, 2, 0)
].join(':') + ',' + d.milliseconds
var subtitle = [
i,
[
startTime,
'-->',
endTime
].join(' '),
p[i]._? p[i]._: mergeS(p[i].s),
'\n'
].join('\n')
s += subtitle
}
} catch (e) {
return cb(e)
}
cb(null, s)
}