-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubtitles.mjs
199 lines (167 loc) · 5.49 KB
/
subtitles.mjs
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
// 00:03:02,100
export function parseTime(str) {
const parts = str.split(/[:,]/);
const hh = parseInt(parts[0], 10);
const mm = parseInt(parts[1], 10);
const ss = parseInt(parts[2], 10);
const frac = parseFloat('0.' + parts[3]);
return hh * 3600 + mm * 60 + ss + frac;
}
function parseTimeFree(str) {
const parts = str.split(/[:]/);
if (parts.length === 2) parts.unshift(0);
const hh = parseInt(parts[0], 10);
const mm = parseInt(parts[1], 10);
const ss = parseInt(parts[2], 10);
return hh * 3600 + mm * 60 + ss;
}
export function machineTime(secs) {
const hh = Math.floor(secs / 3600); secs -= hh * 3600;
const mm = Math.floor(secs / 60); secs -= mm * 60;
return `${hh.toString().padStart(2, '0')}:${mm.toString().padStart(2, '0')}:${secs.toFixed(3).padStart(6, '0').replace('.', ',')}`;
}
export function humanTime(secs) {
const hh = Math.floor(secs / 3600); secs -= hh * 3600;
const mm = Math.floor(secs / 60); secs -= mm * 60;
const ss = Math.floor(secs);
if (hh) return `${hh}:${mm.toString().padStart(2, '0')}:${ss.toString().padStart(2, '0')}`;
return `${mm}:${ss.toString().padStart(2, '0')}`;
}
export function parseSrt(str) {
str = str.replaceAll(/\r\n/mg, '\n').replaceAll(/\r/mg, '\n');
const lines = str.split('\n');
const result = [];
try {
while (true) {
let l = lines.shift();
const srtIndex = parseInt(l, 10); // 1
l = lines.shift();
const parts = l.split(' --> '); // 00:00:11,380 --> 00:00:16,620
const [start, end] = parts.map(parseTime);
let content = '';
while (true) {
l = lines.shift();
if (!l) break;
if (!content) content = l;
else content = content + '\n' + l;
}
const o = { start, end, content, srtIndex };
result.push(o);
}
} catch (err) {
//console.log('err', err);
}
return result;
}
export function serializeSrt(subs) {
fixSubtitles(subs);
let result = '';
let li = 0;
for (const sub of subs) {
const isLast = li === subs.length - 1;
result += sub.srtIndex + '\n';
result += machineTime(sub.start) + ' --> ' + machineTime(sub.end) + '\n';
result += sub.content + (isLast ? '\n' : '\n\n');
++li;
}
return result;
}
export function fixSubtitles(subs) {
let lastSub = {
srtIndex: 0,
start: 0,
end: 0,
content: '',
};
const stats = {
indexChanges: 0,
startShifts: 0,
};
for (const sub of subs) {
if (sub.srtIndex !== lastSub.srtIndex + 1) {
sub.srtIndex = lastSub.srtIndex + 1;
++stats.indexChanges;
//console.log(`fixed #${sub.srtIndex} srtIndex`);
}
const gap = sub.start - lastSub.end;
if (gap < 0) {
sub.start = lastSub.end;
++stats.startShifts;
//console.log(`moved #${sub.index} start to later`);
} else if (gap > 0.5) {
//console.log(`gap #${sub.index} ${gap.toFixed(1)}`);
}
if (sub.end - sub.start < 0.2) {
//console.log(`sub #${sub.index} is too short: ${sub.end - sub.start} "${sub.content}"`);
}
lastSub = sub;
}
console.log('stats', stats);
}
function adHocSubs(text, startIndex = 1) {
const lines = text.split('\n');
let idx = startIndex;
const subs = [];
let lastSub;
for (const line of lines) {
const spaceI = line.indexOf(' ');
const tS = parseTimeFree(line.slice(0, spaceI));
const lineContent = line.slice(spaceI + 1);
if (lastSub) lastSub.end = tS;
lastSub = {
srtIndex: idx++,
start: tS,
end: 0,
content: lineContent,
};
subs.push(lastSub);
}
if (lastSub) lastSub.end = lastSub.start + 1;
return serializeSrt(subs);
}
//window.adHocSubs = adHocSubs; // TODO TEMP
// copy( adHocSubs(``) )
export function splitText(text, ratio) {
const len = text.length;
let cutIndex = Math.round(ratio * len);
while (cutIndex > 0) {
if ([' ', '\n'].includes(text[cutIndex])) break;
--cutIndex;
}
if (cutIndex === 0) throw new Error('you need to increase the split ratio');
const content0 = text.slice(0, cutIndex);
const content1 = text.slice(cutIndex + 1);
return [content0, content1];
}
//////
export function lookForSpeaker(metadata, index) {
for (const [k, v] of Object.entries(metadata.speakers)) {
if (v.subtitles.includes(index)) return k;
}
}
export function setSubtitleSpeaker(metadata, index, name) {
for (const [k, v] of Object.entries(metadata.speakers)) {
const foundPos = v.subtitles.indexOf(index);
if (k === name) {
if (foundPos === -1) {
v.subtitles.push(index);
v.subtitles.sort((a, b) => a - b);
}
} else {
if (foundPos !== -1) {
v.subtitles.splice(foundPos, 1);
}
}
}
}
export function reassignSpeakerIndices(metadata, fn) {
for (const [_, v] of Object.entries(metadata.speakers)) {
v.subtitles = v.subtitles.map(fn).filter(i => i !== -1);
}
}
export function getSubtitleSpeakerPairs(subtitles, metadata) {
return subtitles.map((sub) => {
const speaker = lookForSpeaker(metadata, sub.srtIndex);
return [sub, speaker];
});
}