-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcheck.js
159 lines (137 loc) · 3.97 KB
/
check.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
var fs = require('fs');
const path = require('path');
const yargs = require('yargs');
function padZeros(t, z = 2) {
if (z === 2) {
return ('00' + t).slice(-2);
}
return ('000' + t).slice(-3);
}
function srtTimeStampFromMs(ms) {
const hours = Math.floor(ms / 1000 / 60 / 60);
ms = ms - hours * 1000 * 60 * 60;
const minutes = Math.floor(ms / 1000 / 60);
ms = ms - minutes * 1000 * 60;
const seconds = Math.floor(ms / 1000);
ms = ms - seconds * 1000;
return `${padZeros(hours)}:${padZeros(minutes)}:${padZeros(
seconds
)},${padZeros(ms, 3)}`;
}
function checkFile(file) {
console.log(`checking ${file}`);
const output = [];
var array = fs
.readFileSync(`${__dirname}/episodes/${file}`)
.toString()
.trim()
.split(/\r?\n\r?\n/);
const names = {};
let addedLines = 0;
for (let i = 0; i < array.length; i++) {
const element = array[i];
const lines = element.split('\n').map((l) => l.trim());
const ts = lines[1];
lines[0] = i + 1 + addedLines;
if (
!/^[0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9] --> [0-9][0-9]:[0-9][0-9]:[0-9][0-9],[0-9][0-9][0-9]$/.test(
ts
)
) {
console.error(`Incorrect timestamp formatting: ${ts}`);
output.push(lines.join('\n'));
} else {
let [start, end] = ts.split(' --> ').map((t) => {
const [hours, minutes, seconds, ms] = t.split(/:|,/);
return (
parseInt(hours) * 60 * 60 * 1000 +
parseInt(minutes) * 60 * 1000 +
parseInt(seconds) * 1000 +
parseInt(ms)
);
});
if (start > end) {
end = end + (start - end) + 1;
}
if (lines[2].indexOf(':') !== -1) {
const name = lines[2].split(':')[0];
if (!names[name]) {
names[name] = 1;
} else {
names[name] = names[name] + 1;
}
}
let text = lines.slice(2).join(' ').split(' ');
const textLines = [];
const maxLength = 32;
let curIndex = -1;
text.forEach((word) => {
if (
curIndex >= 0 &&
`${textLines[curIndex]} ${word}`.length <= maxLength
) {
textLines[curIndex] = `${textLines[curIndex]} ${word}`;
} else {
curIndex++;
textLines[curIndex] = word;
}
});
if (textLines.length > 2) {
const numChunks = Math.ceil(textLines.length / 2);
const eachChunkLength = Math.floor((end - start) / numChunks);
let curStart = start;
let baseIndex = lines[0];
for (let i = 0; i < numChunks; i++) {
if (i !== 0) {
addedLines++;
}
output.push(
[
baseIndex + i,
`${srtTimeStampFromMs(curStart)} --> ${srtTimeStampFromMs(
curStart + eachChunkLength
)}`,
...textLines.slice(i * 2, i * 2 + 2),
].join('\n')
);
curStart = curStart + eachChunkLength;
}
} else {
output.push(
[
lines[0],
`${srtTimeStampFromMs(start)} --> ${srtTimeStampFromMs(end)}`,
...textLines,
].join('\n')
);
}
}
}
const filteredNames = Object.keys(names).filter((n) => names[n] === 1);
if (filteredNames.length) {
console.log(`possible bad names / colon error:`);
console.log(filteredNames.map((n) => `- ${n}`).join('\n'));
}
fs.writeFileSync(`${__dirname}/episodes/${file}`, output.join('\n\n'));
}
const argv = yargs
.option('file', {
alias: 'f',
description: 'Select file',
type: 'string',
})
.help()
.alias('help', 'h').argv;
if (argv.file && fs.existsSync(`${__dirname}/episodes/${argv.file}`)) {
checkFile(argv.file);
} else {
console.log('checking all files');
const files = fs.readdirSync(`${__dirname}/episodes`, {
withFileTypes: true,
});
files.map((file) => {
if (file.isFile() && path.extname(file.name) === '.srt') {
checkFile(file.name);
}
});
}