fix:修复bug

This commit is contained in:
fanxb 2022-11-07 12:59:18 +08:00
parent fee2f3100e
commit 5c5098284c

View File

@ -2,8 +2,9 @@ const fs = require('fs-extra');
const path = require('path'); const path = require('path');
const cmd = require('child_process'); const cmd = require('child_process');
const hardTypeSet = new Set("none", "qsv", "cuda"); const hardTypeSet = new Set(["none", "qsv", "cuda"]);
const supportVideoTypeSet = new Set("mkv", "mp4"); const supportVideoTypeSet = new Set(["mkv", "mp4"]);
const replaceTextArr = ['h264', 'H264', 'x264', 'X264'];
/** /**
* *
@ -15,19 +16,22 @@ const supportVideoTypeSet = new Set("mkv", "mp4");
*/ */
async function deal (basePath, maxBitRate = 2500, changeName = false, hardType) { async function deal (basePath, maxBitRate = 2500, changeName = false, hardType) {
let hwType = hardType == 'qsv' ? "-hwaccel qsv" : hardType == 'cuda' ? "-hwaccel cuda" : ""; let hwType = hardType == 'qsv' ? "-hwaccel qsv" : hardType == 'cuda' ? "-hwaccel cuda" : "";
let decodeType = hardType == 'qsv' ? "-c:v h264_qsv" : hardType == 'cuda' ? "-c:v hevc_qsv" : ""; let decodeType = hardType == 'qsv' ? "-c:v h264_qsv" : hardType == 'cuda' ? "-c:v h264_cuvid" : "";
let encodeType = hardType == 'qsv' ? "-c:v h264_cuvid" : hardType == 'cuda' ? "-c:v hevc_nvenc" : ""; let encodeType = hardType == 'qsv' ? "-c:v hevc_qsv" : hardType == 'cuda' ? "-c:v hevc_nvenc" : "";
if (!hardType && !hardTypeSet.has(hardType)) { if (hardType && !hardTypeSet.has(hardType)) {
throw new Error("不支持的加速方案:" + hardType + ",仅支持:空," + JSON.stringify(hardTypeSet)); throw new Error("不支持的加速方案:" + hardType + ",仅支持:空," + JSON.stringify(hardTypeSet));
} }
let fileList = fs.readdirSync(basePath); let fileList = fs.readdirSync(basePath);
for (let i in fileList) { for (let i in fileList) {
let name = fileList[i]; let name = fileList[i];
let filePath = path.join(basePath, name); let filePath = path.join(basePath, name);
if (fs.statSync(filePath).isDirectory) { if (!fs.existsSync(filePath)) {
continue;
}
if (fs.statSync(filePath).isDirectory()) {
//如果为文件夹递归处理 //如果为文件夹递归处理
deal(filePath, maxBitRate, hardType); await deal(filePath, maxBitRate, changeName, hardType);
} }
if (!supportVideoTypeSet.has(name.substring(name.lastIndexOf(".") + 1))) { if (!supportVideoTypeSet.has(name.substring(name.lastIndexOf(".") + 1))) {
continue; continue;
@ -50,12 +54,13 @@ async function deal (basePath, maxBitRate = 2500, changeName = false, hardType)
} }
bitRate = Math.round(parseInt(bitRate) / 1000); bitRate = Math.round(parseInt(bitRate) / 1000);
bitRate = bitRate > maxBitRate ? maxBitRate : bitRate; bitRate = bitRate > maxBitRate ? maxBitRate : bitRate;
let newName; let newName = null;
if (name.indexOf("h264") > -1) { replaceTextArr.forEach(item => {
newName = name.replace('h264', 'h265'); if (newName == null && name.indexOf(item) > -1) {
} else if (name.indexOf('H264') > -1) { newName = name.replace(item, 'h265');
newName = name.replace('H264', 'H265'); }
} else { })
if (newName == null) {
let index = name.lastIndexOf('.'); let index = name.lastIndexOf('.');
newName = name.substr(0, index) + ".h265" + name.substr(index); newName = name.substr(0, index) + ".h265" + name.substr(index);
} }
@ -73,7 +78,7 @@ async function deal (basePath, maxBitRate = 2500, changeName = false, hardType)
let namePart1 = name.substr(0, name.lastIndexOf('.')); let namePart1 = name.substr(0, name.lastIndexOf('.'));
let newNamePart1 = newName.substr(0, newName.lastIndexOf('.')); let newNamePart1 = newName.substr(0, newName.lastIndexOf('.'));
fileList.forEach(item => { fileList.forEach(item => {
if (item.startsWith(namePart1) && (item.endsWith('.nfo') || item.endsWith('.srt') || item.endsWith('.ass'))) { if (item.startsWith(namePart1) && item != name) {
let temp = item.replace(namePart1, newNamePart1); let temp = item.replace(namePart1, newNamePart1);
if (item != temp) { if (item != temp) {
fs.renameSync(path.join(basePath, item), path.join(basePath, temp)); fs.renameSync(path.join(basePath, item), path.join(basePath, temp));
@ -88,6 +93,6 @@ async function deal (basePath, maxBitRate = 2500, changeName = false, hardType)
} }
} }
(async () => { (async () => {
await deal("要处理的文件夹", 2500, true, "cuda"); await deal("Z:\\userData\\视频\\剧集\\美剧\\亿万.Billions", 2500, true, "cuda");
})(); })();