forked from tsur/vtt-to-srt
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.es6
68 lines (58 loc) · 1.77 KB
/
index.es6
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
import through from 'through2';
import split from 'split2';
import pumpify from 'pumpify';
import { EOL } from 'os';
module.exports = function() {
let count = 0;
const reg = new RegExp(`(WEBVTT\s*(FILE)?.*)(${EOL})*`, 'g' )
const 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))
{
let vttComp = line.split('-->')
let 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))
{
let vttComp = line.split('-->')
let 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)
{
let vttLine = line.replace(reg , '')
}
else
{
let 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}${EOL}${vttLine}`;
} else {
vttLine = `${EOL}${++count}${EOL}{vttLine}`;
}
}
cb(null, vttLine);
};
return pumpify(split(), through.obj(write));
};