-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindSub.js
More file actions
247 lines (224 loc) · 7.92 KB
/
findSub.js
File metadata and controls
247 lines (224 loc) · 7.92 KB
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
import readline from 'readline';
import { t } from './i18n.js';
// 默认要查找并合成的字幕:简体中文 + 英语。可扩展为更多 code。
const DEFAULT_TARGETS = [
{ code: 'chi', label: '简体中文', finder: findChiSub },
{ code: 'eng', label: '英语', finder: findEngSub },
];
// 在同一进程内,一旦用户手动选择过一次字幕,后续文件都直接走手动选择流程
let alwaysManual = false;
export const findSub = async (subTitles) => {
// 如果已经进入“总是手动选择”模式,跳过倒计时和自动匹配
if (alwaysManual) {
return await manualSelectSubs(subTitles);
}
const prefix = t('autoCountdownPrefix');
const interrupted = await waitForInterruptWithCountdown(3000, prefix);
if (interrupted) {
console.log(t('interruptedManual'));
alwaysManual = true;
return await manualSelectSubs(subTitles);
}
console.log(t('autoFindingChiEng'));
const [target1, target2] = DEFAULT_TARGETS;
let sub1 = target1.finder(subTitles);
let sub2 = target2.finder(subTitles);
if (sub1) {
console.log(t('foundLangSub', { label: target1.label }), sub1.index);
} else {
console.log(t('notFoundLangSub', { label: target1.label }));
}
if (sub2) {
console.log(t('foundLangSub', { label: target2.label }), sub2.index);
} else {
console.log(t('notFoundLangSub', { label: target2.label }));
}
if (!sub1 || !sub2) {
console.log(t('availableSubtitleList'));
subTitles.forEach((s) => {
console.log(
t('subtitleListItem'),
s.index,
s.code,
s.name,
s.duration,
s.frames,
);
});
if (!sub1) sub1 = await promptForSubIndex(subTitles, target1.label);
if (!sub2) sub2 = await promptForSubIndex(subTitles, target2.label, sub1.index);
// 一旦出现手动输入索引,后续文件全部改为手动选择模式
alwaysManual = true;
}
console.log(
t('finalSelectedLang', { label: target1.label }),
sub1.index,
sub1.frames ?? '-',
);
console.log(
t('finalSelectedLang', { label: target2.label }),
sub2.index,
sub2.frames ?? '-',
);
console.log(t('duration'), sub1.duration);
return [sub1, sub2];
};
/**
* 等待指定毫秒,期间显示倒计时(数字每秒更新),任意键中断。
* @param {number} ms 总等待时间(毫秒)
* @param {string} prefix 倒计时前的提示文案(同一行)
* @returns {Promise<boolean>} 是否被按键中断
*/
const waitForInterruptWithCountdown = (ms, prefix) => {
return new Promise((resolve) => {
if (!process.stdin.isTTY) {
setTimeout(() => resolve(false), ms);
return;
}
const stdin = process.stdin;
const wasRaw = stdin.isRaw || false;
if (!wasRaw) stdin.setRawMode(true);
stdin.resume();
stdin.setEncoding('utf8');
const totalSec = Math.ceil(ms / 1000);
let leftSec = totalSec;
const formatCountdown = (sec) => {
// 使用高亮颜色和箭头让数字更醒目
const YELLOW = '\x1b[33m';
const BOLD = '\x1b[1m';
const RESET = '\x1b[0m';
return `${prefix} ${YELLOW}${BOLD}>>> ${sec} <<<${RESET}`;
};
const writeLine = () => {
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 1);
process.stdout.write(formatCountdown(leftSec));
};
writeLine();
let tickId;
tickId = setInterval(() => {
leftSec -= 1;
if (leftSec <= 0) {
clearInterval(tickId);
cleanup();
readline.cursorTo(process.stdout, 0);
readline.clearLine(process.stdout, 1);
process.stdout.write('\n');
resolve(false);
return;
}
writeLine();
}, 1000);
const cleanup = () => {
if (tickId) clearInterval(tickId);
stdin.removeListener('data', onKeyPress);
if (!wasRaw) stdin.setRawMode(false);
stdin.pause();
};
const onKeyPress = (key) => {
if (key === '\u0003') {
cleanup();
process.exit(0);
}
cleanup();
resolve(true);
};
stdin.on('data', onKeyPress);
});
};
const manualSelectSubs = async (subTitles) => {
console.log(t('manualAllAvailable'));
subTitles.forEach((s, idx) => {
console.log(
t('subtitleListItem'),
s.index,
s.code,
s.name,
s.duration,
s.frames,
);
});
const sub1 = await promptForSubIndex(subTitles, '第一个');
const sub2 = await promptForSubIndex(subTitles, '第二个', sub1.index);
console.log(t('manualSelectedFirst'), sub1.index, sub1.code, sub1.name);
console.log(t('manualSelectedSecond'), sub2.index, sub2.code, sub2.name);
console.log(t('duration'), sub1.duration);
return [sub1, sub2];
};
const promptForSubIndex = (subTitles, label, excludeIndex = null) => {
return new Promise((resolve) => {
const rl = readline.createInterface({ input: process.stdin, output: process.stdout });
const ask = () => {
rl.question(t('promptIndex', { label, excludeIndex }), (answer) => {
const trimmed = answer.trim();
if (trimmed === '') {
console.log(t('exited'));
rl.close();
process.exit(0);
}
const value = Number(trimmed);
if (!Number.isInteger(value)) {
console.log(t('invalidInteger'));
ask();
return;
}
const target = subTitles.find((s) => s.index === value);
if (!target) {
console.log(t('indexNotFound'));
ask();
return;
}
if (excludeIndex !== null && target.index === excludeIndex) {
console.log(t('sameAsFirst'));
ask();
return;
}
rl.close();
resolve({
index: target.index,
duration: target.duration,
frames: target.frames,
code: target.code,
name: target.name,
});
});
};
ask();
});
};
// ---------- 按 language code 的查找策略(可扩展) ----------
/**
* 简体中文字幕:code=chi,多条时优先 name 含「简体」或 "simplified"
*/
function findChiSub(subTitles) {
const list = subTitles.filter((s) => s.code === 'chi');
if (list.length === 0) return null;
if (list.length === 1) return toSub(list[0]);
const preferred = list.find((s) => s.name.includes('简体') || s.name.includes('simplified'));
return preferred ? toSub(preferred) : null;
}
/**
* 英语字幕:code=eng,过滤空字幕(帧数过少),多条时优先非 SDH
*/
function findEngSub(subTitles) {
const list = subTitles.filter((s) => s.code === 'eng');
if (list.length === 0) return null;
const nonEmpty = list.filter((s) => {
const frames = Number(s.frames);
if (!frames) return true;
return frames >= 100;
});
const pool = nonEmpty.length > 0 ? nonEmpty : list;
const nonSDH = pool.filter((s) => !s.name.includes('sdh'));
const final = nonSDH.length > 0 ? nonSDH : pool;
return final[0] ? toSub(final[0]) : null;
}
function toSub(s) {
return {
index: s.index,
duration: s.duration,
frames: s.frames,
code: s.code,
name: s.name,
};
}