-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
executable file
·55 lines (51 loc) · 1.56 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
#!/usr/bin/env node
const subtitleParse = require('subtitles-parser');
const fs = require('fs');
const srcFileName = process.argv[2];
const destFileName = process.argv[3];
function removeBetweenTwoChars(str, char1, char2) {
var cpos = str.indexOf(char1),
spos = str.indexOf(char2);
if (cpos > -1 && spos > cpos) {
var strippedString = str.substr(0, cpos-1)+str.substr(spos+1);
return removeBetweenTwoChars(
strippedString,
char1,
char2
);
}
return str;
}
if (process.argv.length === 4 && srcFileName && destFileName) {
try {
let contents = fs.readFileSync(srcFileName).toString();
if (contents) {
let data = subtitleParse.fromSrt(contents);
let newData = [];
data.forEach((title, index) => {
if (title.text.indexOf('♪') === -1) {
if (title.text.indexOf('[') === -1) {
newData.push(title);
} else {
title.text = removeBetweenTwoChars(title.text, '[', ']');
if (title.text) {
newData.push(title);
}
}
}
});
let output = subtitleParse.toSrt(newData);
fs.writeFileSync(destFileName, output);
} else {
console.error('Empty source file `'+ srcFileName +'`.');
}
} catch (error) {
if (error.code == 'ENOENT') {
console.error('Subtitle file: `' + srcFileName + '` not found.');
} else {
console.error('Something happened.', error);
}
}
} else {
console.error('Use: `npm run remove source.srt destination.srt `')
}