refactor:优化缓存

This commit is contained in:
fanxb 2022-11-10 21:51:28 +08:00
parent bb6f3dfe3d
commit 43066c972c

View File

@ -9,6 +9,11 @@ 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']; const replaceTextArr = ['h264', 'H264', 'x264', 'X264'];
let cache = {}; let cache = {};
let timer = setInterval(saveFile, 5000);
function saveFile () {
console.log("保存数据");
fs.writeFileSync(cachePath, JSON.stringify(cache));
}
@ -16,7 +21,11 @@ let cache = {};
if (fs.existsSync(cachePath)) { if (fs.existsSync(cachePath)) {
cache = JSON.parse(fs.readFileSync(cachePath, 'utf-8')); cache = JSON.parse(fs.readFileSync(cachePath, 'utf-8'));
} }
await deal("Z:\\userData\\视频\\动漫", 2500, true, "cuda"); await deal("Z:\\userData\\视频\\电影", 2500, true, "cuda");
if (timer) {
clearInterval(timer);
}
saveFile();
})(); })();
/** /**
@ -43,7 +52,7 @@ async function deal (basePath, maxBitRate = 2500, changeName = false, hardType)
continue; continue;
} }
if (fs.statSync(filePath).isDirectory()) { if ((await fs.stat(filePath)).isDirectory()) {
//如果为文件夹递归处理 //如果为文件夹递归处理
await deal(filePath, maxBitRate, changeName, hardType); await deal(filePath, maxBitRate, changeName, hardType);
} }
@ -94,9 +103,8 @@ async function deal (basePath, maxBitRate = 2500, changeName = false, hardType)
} else { } else {
console.log("转换倍率:" + cmdRes.stderr.substring(index - 20)); console.log("转换倍率:" + cmdRes.stderr.substring(index - 20));
} }
if (!fs.existsSync(newFilePath) || fs.statSync(newFilePath).size <= 10000) { if (!fs.existsSync(newFilePath) || fs.statSync(newFilePath).size <= 10000 || cmdRes.stderr.indexOf('Error') > -1) {
console.log("未知错误,文件转换失败"); throw new Error("未知错误,文件转换失败");
return;
} }
if (changeName) { if (changeName) {
//字幕nfo文件重命名 //字幕nfo文件重命名
@ -111,11 +119,10 @@ async function deal (basePath, maxBitRate = 2500, changeName = false, hardType)
} }
}) })
} }
fs.removeSync(filePath); await fs.remove(filePath);
if (!changeName) { if (!changeName) {
fs.moveSync(newFilePath, filePath); await fs.move(newFilePath, filePath);
} }
await fs.writeFile(cachePath, JSON.stringify(cache));
} }
} }