Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# 依赖文件夹
node_modules/

# 编辑器和IDE配置
.vscode/
.idea/
10 changes: 8 additions & 2 deletions analyzeMedia.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,27 @@ const getFFprobePath = () => {
export const analyzeMedia = (file) => {
console.log(`获取字幕信息...`);
const ffprobePath = getFFprobePath();

/*
TODO: 读取帧率输出
ffprobe -v error -select_streams v -of default=noprint_wrappers=1:nokey=1 -show_entries stream=r_frame_rate input.mkv | awk -F/ '{ print ($1 / $2) }'
输出:23.976
*/

return new Promise((resolve, reject) => {
ffprobe(config.workdir + file, { path: ffprobePath })
.then(function (info) {
// console.log(info);
const subTitles = info.streams.filter((stream) => stream.codec_type === 'subtitle').map(stream => ({
index: stream.index,
code: stream.tags.language.toLowerCase(),
name: stream.tags.title ? stream.tags.title.toLowerCase() : '',
duration: Math.round(stream.duration),
frames: Number(stream.tags.NUMBER_OF_FRAMES) || 0
}));
console.log(`找到 ${subTitles.length} 条字幕。`);
resolve(subTitles);
})
.catch(function (err) {
// console.error(err);
reject(err);
});
});
Expand Down
8 changes: 4 additions & 4 deletions extractSub.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,19 @@ export const extractSub = (filename, targetSubs) => {
.outputOptions(['-map', `0:${targetSubs[1].index}`, '-c', 'copy'])
.run()
.on('start', function (str) {
console.log('转换任务开始~', str);
console.log('正在提取字幕文件...', str);
})
.on('progress', function (progress) {
const progressPercent = Math.round((timemarkToSeconds(progress.timemark) / duration) * 100);
readline.cursorTo(process.stdout, 0);
process.stdout.write(`进行中,完成${(progressPercent || 0)}%`);
process.stdout.write(`字幕提取中,进度:${(progressPercent || 0)}%`);
})
.on('end', function (str) {
console.log('转换任务完成!');
console.log('\n字幕提取完成。');
resolve([mainSrt, secondarySrt]);
})
.on('error', function (err) {
console.log('转换任务出错:', err);
console.log('字幕提取出错:', err);
reject(err);
});
});
Expand Down
34 changes: 22 additions & 12 deletions findSub.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,34 @@ const findChiSub = (subTitles) => {
* 9,subrip,eng
* 10,subrip,eng,SDH
* 23,subrip,eng,English[CC]
* 如果同时有SDH和非SDH版本,选非SDH版本。
* 选择策略:
* 1. 过滤掉空字幕(只有当 NUMBER_OF_FRAMES 存在且 < 10 时才过滤)
* 2. 如果同时有SDH和非SDH版本,选非SDH版本
* 3. 按原始顺序选择第一个可用的字幕
*/
const findEngSub = (subTitles) => {
const englishSubs = subTitles.filter(sub => sub.code === 'eng');

if (englishSubs.length === 0) return null;
if (englishSubs.length === 1) return {
index: englishSubs[0].index,
duration: englishSubs[0].duration,
};

// Filter out SDH subtitles if there are multiple English options
const nonSDHSubs = englishSubs.filter(sub =>
!sub.name.includes('sdh')
);

// Return first non-SDH sub if available, otherwise first English sub
const targetSub = nonSDHSubs[0] || englishSubs[0];
// 过滤掉空字幕
const nonEmpty = englishSubs.filter(sub => {
const frames = Number(sub.frames);
if (!frames) return true;
return frames >= 10;
});

const candidatePool = nonEmpty.length > 0 ? nonEmpty : englishSubs;

if (candidatePool.length === 0) return null;

// 多个英文字幕时,优先去除 SDH
const nonSDHSubs = candidatePool.filter(sub => !sub.name.includes('sdh'));
const finalPool = nonSDHSubs.length > 0 ? nonSDHSubs : candidatePool;

// 按原始顺序选择第一个可用的字幕
const targetSub = finalPool[0];

return targetSub ? {
index: targetSub.index,
duration: targetSub.duration
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "dual-subtitle",
"version": "0.3.1",
"version": "0.4.0",
"main": "index.js",
"type": "module",
"bin": {
Expand Down