Skip to content

Instantly share code, notes, and snippets.

@mpr0xy
Created February 13, 2023 01:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mpr0xy/f1982eafc24a8af505c1848dd39d149a to your computer and use it in GitHub Desktop.
Save mpr0xy/f1982eafc24a8af505c1848dd39d149a to your computer and use it in GitHub Desktop.
批量合并视频和字幕
const path = require("path");
const { execSync } = require("child_process");
const fs = require("fs");
const makeNewMkv = (dirPath, mkvPath, assPath) => {
const mkvName = path.basename(mkvPath).match(/S[\d]{2}E[\d]{2}/)[0];
console.log("mkvName", mkvName);
if (fs.existsSync(path.resolve(dirPath, mkvName + ".mkv"))) {
console.log(`${mkvName}已存在`);
return;
}
const stdout = execSync(
`ffmpeg -i "${mkvPath}" -i "${assPath}" -map 0:v -map 0:a -map 1:s -c copy ${mkvName}.mkv`,
{
cwd: dirPath,
}
);
console.log(stdout.toString());
};
const main = async (dirPath) => {
// 读取dirPath下所有的文件
//
const allFiles = fs.readdirSync(dirPath);
allFiles.forEach((file) => {
const filePath = path.resolve(dirPath, file);
const fileStat = fs.statSync(filePath);
// const fileBaseName = path.basename(filePath);
if (fileStat.isFile() && path.extname(filePath) === ".mkv") {
console.log("mkv:", path.basename(filePath, ".mkv") + ".简体&英文");
const assFile = allFiles.find((file) => {
console.log("ass:", path.basename(file, ".ass"));
return (
path.basename(file, ".ass") ===
path.basename(filePath, ".mkv") + ".简体&英文"
);
});
if (!assFile) {
return;
}
console.log(assFile);
makeNewMkv(dirPath, filePath, path.resolve(dirPath, assFile));
// process.exit(0);
}
});
};
// main(path.resolve("./第三季"));
// main(path.resolve("./第四季"));
// main(path.resolve("./第五季"));
main(path.resolve("./第六季"));
@mpr0xy
Copy link
Author

mpr0xy commented Feb 13, 2023

代码结构比较混乱,每个可能变动的点应该抽取成单独的函数来编写。下次得注意。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment