-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
57 lines (51 loc) · 1.57 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
var encoding = require('./encoding');
var reader = require('./reader');
function convert(buf, defaultCodepageOverride, next) {
// Handle defaultCodepageOverride being optional.
if (next == undefined) {
next = defaultCodepageOverride;
defaultCodepageOverride = undefined;
}
var outputLines = [
'WEBVTT',
'',
'NOTE Converted from .srt via srt2vtt: https://github.com/deestan/srt2vtt'
];
var r = reader(buf);
r.on('error', next);
r.on('subtitle', function(subtitle) {
outputLines.push('');
outputLines.push(subtitle.id);
outputLines.push(subtitle.cue);
outputLines = outputLines.concat(subtitle.lines);
});
r.on('end', function() {
var outLen = 0;
outputLines.forEach(function(line) { outLen += line.length + 1; });
var output = new Buffer(outLen);
for (var i = 0, pos = 0;
i < outputLines.length; i++) {
var lineBuf = outputLines[i];
if (!Buffer.isBuffer(lineBuf))
lineBuf = new Buffer(lineBuf);
lineBuf.copy(output, pos);
pos += outputLines[i].length;
output[pos] = 10;
pos += 1;
}
next(null, output);
});
r.start();
}
module.exports = function srt2vtt(buf, defaultCodepageOverride, next) {
// Handle defaultCodepageOverride being optional.
if (next == undefined) {
next = defaultCodepageOverride;
defaultCodepageOverride = undefined;
}
encoding.convertToUTF8(buf, defaultCodepageOverride, function(err, utf8buf) {
if (err) return next(err);
return convert(utf8buf, next);
});
};
module.exports.raw = convert;