forked from riazXrazor/vtt-to-srt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
81 lines (63 loc) · 1.94 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
68
69
70
71
72
73
74
75
76
77
78
79
80
'use strict';
var _through = require('through2');
var _through2 = _interopRequireDefault(_through);
var _split = require('split2');
var _split2 = _interopRequireDefault(_split);
var _pumpify = require('pumpify');
var _pumpify2 = _interopRequireDefault(_pumpify);
var EOL = require('os').EOL;
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
module.exports = function () {
var count = 0;
var reg = new RegExp('(WEBVTT\s*(FILE)?.*)('+EOL+')*', 'g' )
var write = function write(line, enc, cb) {
if (!line.trim()) return cb();
if(line.match(/(\d{2}:\d{2})\.(\d{3}\s+)\-\-\>(\s+\d{2}:\d{2})\.(\d{3}\s*)/g))
{
var vttComp = line.split('-->')
var vttLine = vttComp.map(function(item){
item = item.replace('.',',');
if(item.split(":").length < 3)
{
item = '00:'+item.trim();
}
return item;
}).join('-->');
vttLine = vttLine+EOL
}
else if(line.match(/(\d{2}:\d{2}:\d{2})\.(\d{3}\s+)\-\-\>(\s+\d{2}:\d{2}:\d{2})\.(\d{3}\s*)/g))
{
var vttComp = line.split('-->')
var vttLine = vttComp.map(function(item){
item = item.replace('.',',');
if(item.split(":").length < 3)
{
item = '00:'+item.trim();
}
return item;
}).join('-->');
vttLine = EOL+vttLine+EOL
}
else if(line.match(reg))
{
var vttLine = line.replace(reg, '')
}
else
{
var vttLine = line+EOL;
}
if (!vttLine.trim()) return cb();
if (/^Kind:|^Language:/m.test(vttLine)) {
return cb();
}
if (/^[0-9]+:/m.test(vttLine)) {
if (count === 0) {
vttLine = ++count + vttLine;
} else {
vttLine = EOL + ++count + vttLine;
}
}
cb(null, vttLine);
};
return (0, _pumpify2.default)((0, _split2.default)(), _through2.default.obj(write));
};