Created
February 13, 2023 01:34
-
-
Save mpr0xy/f1982eafc24a8af505c1848dd39d149a to your computer and use it in GitHub Desktop.
批量合并视频和字幕
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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("./第六季")); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
代码结构比较混乱,每个可能变动的点应该抽取成单独的函数来编写。下次得注意。