diff --git a/apps/tools.js b/apps/tools.js index 8614b73..58f58e7 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -640,11 +640,21 @@ export class tools extends plugin { const cleanedTiktokUrl = parsedUrl.toString(); // 下载逻辑 const path = this.getCurDownloadPath(e); - await checkAndRemoveFile(path + "/temp.mp4"); - const title = ytDlpGetTilt(url, isOversea, this.myProxy); - e.reply(`${ this.identifyPrefix }识别:TikTok,视频下载中请耐心等待 \n${ title }`); - await ytDlpHelper(path, cleanedTiktokUrl, isOversea, this.myProxy, this.videoDownloadConcurrency); - await this.sendVideoToUpload(e, `${ path }/temp.mp4`); + const rawTitle = (await ytDlpGetTilt(url, isOversea, this.myProxy)).toString().replace(/\n/g, ''); + // 清理文件名并截断到10个字符 + const safeTitlePrefix = cleanFilename(rawTitle).substring(0, 10); + const videoFilename = `${safeTitlePrefix}.mp4`; + + // 清理可能存在的旧文件或同名文件 + await checkAndRemoveFile(`${path}/${videoFilename}`); + // 清理旧的 temp 文件 + await checkAndRemoveFile(`${path}/temp.mp4`); + + + e.reply(`${ this.identifyPrefix }识别:TikTok,视频下载中请耐心等待 \n${ rawTitle }`); + // 注意:ytDlpHelper 的 outputFilename 参数位置在 maxThreads 之后 + await ytDlpHelper(path, cleanedTiktokUrl, isOversea, this.myProxy, this.videoDownloadConcurrency, safeTitlePrefix); + await this.sendVideoToUpload(e, `${ path }/${ videoFilename }`); return true; } @@ -2146,46 +2156,65 @@ export class tools extends plugin { } const path = this.getCurDownloadPath(e); + const rawTitle = (await ytDlpGetTilt(url, isOversea, this.myProxy, this.youtubeCookiePath)).toString().replace(/\n/g, ''); + // 清理文件名并截断到10个字符 + const safeTitlePrefix = cleanFilename(rawTitle).substring(0, 10); + + // 使用安全标题作为文件名前缀 + const videoFilename = `${safeTitlePrefix}.mp4`; + const audioFilename = `${safeTitlePrefix}.flac`; + const thumbnailFilename = `${safeTitlePrefix}_thumbnail.png`; // 确保缩略图文件名也唯一 + + // 清理可能存在的旧文件或同名文件 + await checkAndRemoveFile(path + "/" + videoFilename); + await checkAndRemoveFile(path + "/" + audioFilename); + await checkAndRemoveFile(path + "/" + thumbnailFilename); + // 清理旧的 temp 文件,以防万一 await checkAndRemoveFile(path + "/temp.mp4"); await checkAndRemoveFile(path + "/temp.flac"); await checkAndRemoveFile(path + "/thumbnail.png"); - await ytDlpGetThumbnail(path, url, isOversea, this.myProxy, this.youtubeCookiePath); - const title = ytDlpGetTilt(url, isOversea, this.myProxy, this.youtubeCookiePath).toString().replace(/\n/g, ''); + + + await ytDlpGetThumbnail(path, url, isOversea, this.myProxy, this.youtubeCookiePath, safeTitlePrefix + "_thumbnail"); // 传递不带扩展名的前缀 + const fullThumbnailPath = `${path}/${thumbnailFilename}`; + // 音频逻辑 if (url.includes("music")) { e.reply([ - segment.image(`${ path }/thumbnail.png`), - `${ this.identifyPrefix }识别:油管音乐\n视频标题:${ title }` + segment.image(fullThumbnailPath), + `${ this.identifyPrefix }识别:油管音乐\n视频标题:${ rawTitle }` ]); - await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, true, graphics, timeRange, this.youtubeCookiePath); + await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, safeTitlePrefix, true, graphics, timeRange, this.youtubeCookiePath); + const fullAudioPath = `${path}/${audioFilename}`; if (this.isSendVocal) { - e.reply(segment.record(`${ path }/temp.flac`)); + await e.reply(segment.record(fullAudioPath)); } - this.uploadGroupFile(e, `${ path }/temp.flac`); + await this.uploadGroupFile(e, fullAudioPath); // uploadGroupFile 内部会删除 fullAudioPath + await checkAndRemoveFile(fullThumbnailPath); // 删除缩略图 // 发送完就截断 return; } // 下面为视频逻辑 - const Duration = convertToSeconds(ytDlpGetDuration(url, isOversea, this.myProxy, this.youtubeCookiePath).toString().replace(/\n/g, '')); + const Duration = convertToSeconds((await ytDlpGetDuration(url, isOversea, this.myProxy, this.youtubeCookiePath)).toString().replace(/\n/g, '')); // logger.info('时长------',Duration) if (Duration > this.youtubeDuration) { e.reply([ - segment.image(`${ path }/thumbnail.png`), - `${ this.identifyPrefix }识别:油管,视频时长超限 \n视频标题:${ title }\n⌚${ DIVIDING_LINE.replace('{}', '限制说明').replace(/\n/g, '') }⌚\n视频时长:${ (Duration / 60).toFixed(2).replace(/\.00$/, '') } 分钟\n大于管理员限定解析时长:${ (this.youtubeDuration / 60).toFixed(2).replace(/\.00$/, '') } 分钟` + segment.image(fullThumbnailPath), + `${ this.identifyPrefix }识别:油管,视频时长超限 \n视频标题:${ rawTitle }\n⌚${ DIVIDING_LINE.replace('{}', '限制说明').replace(/\n/g, '') }⌚\n视频时长:${ (Duration / 60).toFixed(2).replace(/\.00$/, '') } 分钟\n大于管理员限定解析时长:${ (this.youtubeDuration / 60).toFixed(2).replace(/\.00$/, '') } 分钟` ]); } else if (Duration > this.youtubeClipTime && timeRange != '00:00:00-00:00:00') { e.reply([ - segment.image(`${ path }/thumbnail.png`), - `${ this.identifyPrefix }识别:油管,视频截取中请耐心等待 \n视频标题:${ title }\n✂️${ DIVIDING_LINE.replace('{}', '截取说明').replace(/\n/g, '') }✂️\n视频时长:${ (Duration / 60).toFixed(2).replace(/\.00$/, '') } 分钟\n大于管理员限定截取时长:${ (this.youtubeClipTime / 60).toFixed(2).replace(/\.00$/, '') } 分钟\n将截取视频片段` + segment.image(fullThumbnailPath), + `${ this.identifyPrefix }识别:油管,视频截取中请耐心等待 \n视频标题:${ rawTitle }\n✂️${ DIVIDING_LINE.replace('{}', '截取说明').replace(/\n/g, '') }✂️\n视频时长:${ (Duration / 60).toFixed(2).replace(/\.00$/, '') } 分钟\n大于管理员限定截取时长:${ (this.youtubeClipTime / 60).toFixed(2).replace(/\.00$/, '') } 分钟\n将截取视频片段` ]); - await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, true, graphics, timeRange, this.youtubeCookiePath); - this.sendVideoToUpload(e, `${ path }/temp.mp4`); + await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, safeTitlePrefix, true, graphics, timeRange, this.youtubeCookiePath); + this.sendVideoToUpload(e, `${ path }/${ videoFilename }`); } else { - e.reply([segment.image(`${ path }/thumbnail.png`), `${ this.identifyPrefix }识别:油管,视频下载中请耐心等待 \n视频标题:${ title }\n视频时长:${ (Duration / 60).toFixed(2).replace(/\.00$/, '') } 分钟`]); - await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, true, graphics, timeRange, this.youtubeCookiePath); - this.sendVideoToUpload(e, `${ path }/temp.mp4`); + e.reply([segment.image(fullThumbnailPath), `${ this.identifyPrefix }识别:油管,视频下载中请耐心等待 \n视频标题:${ rawTitle }\n视频时长:${ (Duration / 60).toFixed(2).replace(/\.00$/, '') } 分钟`]); + await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, safeTitlePrefix, true, graphics, timeRange, this.youtubeCookiePath); + this.sendVideoToUpload(e, `${ path }/${ videoFilename }`); } } catch (error) { logger.error(error); @@ -3236,14 +3265,15 @@ export class tools extends plugin { // 正常发送视频 if (videoSize > videoSizeLimit) { e.reply(`当前视频大小:${ videoSize }MB,\n大于设置的最大限制:${ videoSizeLimit }MB,\n改为上传群文件`); - await this.uploadGroupFile(e, path); + await this.uploadGroupFile(e, path); // uploadGroupFile 内部会处理删除 } else { - e.reply(segment.video(path)); + await e.reply(segment.video(path)); + await checkAndRemoveFile(path); // 发送成功后删除 } } catch (err) { logger.error(`[R插件][发送视频判断是否需要上传] 发生错误:\n ${ err }`); - // logger.info(logger.yellow(`上传发生错误,R插件正在为你采用备用策略,请稍等,如果发不出来请再次尝试!`)); - // e.reply(segment.video(path)); + // 如果发送失败,也尝试删除,避免残留 + await checkAndRemoveFile(path); } } @@ -3260,5 +3290,6 @@ export class tools extends plugin { } else { await e.group.sendFile(path); } + await checkAndRemoveFile(path); // 上传成功后删除 } } diff --git a/utils/yt-dlp-util.js b/utils/yt-dlp-util.js index 42dffc9..762dddc 100644 --- a/utils/yt-dlp-util.js +++ b/utils/yt-dlp-util.js @@ -1,4 +1,4 @@ -import { exec, execSync } from "child_process"; +import { exec } from "child_process"; /** * 构建梯子参数 @@ -27,7 +27,7 @@ function constructCookiePath(url, cookiePath) { * @returns {string} */ function constructEncodingParam(url) { - return url.includes("youtu") ? "--encoding UTF-8" : ""; + return "--encoding UTF-8"; // 始终为标题获取使用 UTF-8 编码 } @@ -40,9 +40,19 @@ function constructEncodingParam(url) { * @returns string */ export function ytDlpGetDuration(url, isOversea, proxy, cookiePath = "") { - // 构造 cookie 参数 - const cookieParam = constructCookiePath(url, cookiePath); - return execSync(`yt-dlp --get-duration --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url}`); + return new Promise((resolve, reject) => { + // 构造 cookie 参数 + const cookieParam = constructCookiePath(url, cookiePath); + const command = `yt-dlp --get-duration --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url}`; + exec(command, (error, stdout, stderr) => { + if (error) { + logger.error(`[R插件][yt-dlp审计] Error executing ytDlpGetDuration: ${error}. Stderr: ${stderr}`); + reject(error); + } else { + resolve(stdout.trim()); + } + }); + }); } /** @@ -54,11 +64,21 @@ export function ytDlpGetDuration(url, isOversea, proxy, cookiePath = "") { * @returns string */ export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") { - // 构造 cookie 参数 - const cookieParam = constructCookiePath(url, cookiePath); - // 构造 编码 参数 - const encodingParam = constructEncodingParam(url); - return execSync(`yt-dlp --get-title --skip-download ${cookieParam} ${ constructProxyParam(isOversea, proxy) } ${ url } ${encodingParam}`); + return new Promise((resolve, reject) => { + // 构造 cookie 参数 + const cookieParam = constructCookiePath(url, cookiePath); + // 构造 编码 参数 + const encodingParam = constructEncodingParam(url); + const command = `yt-dlp --get-title --skip-download ${cookieParam} ${ constructProxyParam(isOversea, proxy) } ${ url } ${encodingParam}`; + exec(command, (error, stdout, stderr) => { + if (error) { + logger.error(`[R插件][yt-dlp审计] Error executing ytDlpGetTilt: ${error}. Stderr: ${stderr}`); + reject(error); + } else { + resolve(stdout.trim()); + } + }); + }); } /** @@ -68,11 +88,23 @@ export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") { * @param isOversea * @param proxy * @param cookiePath + * @param thumbnailFilenamePrefix 缩略图文件名前缀 (不含扩展名) */ -export function ytDlpGetThumbnail(path, url, isOversea, proxy, cookiePath= "") { - // 构造 cookie 参数 - const cookieParam = constructCookiePath(url, cookiePath); - return execSync(`yt-dlp --write-thumbnail --convert-thumbnails png --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url} -P ${path} -o "thumbnail.%(ext)s"`); +export function ytDlpGetThumbnail(path, url, isOversea, proxy, cookiePath = "", thumbnailFilenamePrefix = "thumbnail") { + return new Promise((resolve, reject) => { + // 构造 cookie 参数 + const cookieParam = constructCookiePath(url, cookiePath); + const finalThumbnailName = thumbnailFilenamePrefix || "thumbnail"; // 确保有默认值 + const command = `yt-dlp --write-thumbnail --convert-thumbnails png --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url} -P "${path}" -o "${finalThumbnailName}.%(ext)s"`; + exec(command, (error, stdout, stderr) => { + if (error) { + logger.error(`[R插件][yt-dlp审计] Error executing ytDlpGetThumbnail: ${error}. Stderr: ${stderr}`); + reject(error); + } else { + resolve(); // The return value is not used + } + }); + }); } /** @@ -86,22 +118,26 @@ export function ytDlpGetThumbnail(path, url, isOversea, proxy, cookiePath= "") { * @param graphics YouTube画质参数 * @param timeRange 截取时间段 * @param maxThreads 最大并发 + * @param outputFilename 输出文件名 (不含扩展名) * @param cookiePath Cookie所在位置 */ -export async function ytDlpHelper(path, url, isOversea, proxy, maxThreads, merge = false, graphics, timeRange, cookiePath = "") { +export async function ytDlpHelper(path, url, isOversea, proxy, maxThreads, outputFilename, merge = false, graphics, timeRange, cookiePath = "") { return new Promise((resolve, reject) => { let command = ""; // 构造 cookie 参数 const cookieParam = constructCookiePath(url, cookiePath); + // 确保 outputFilename 不为空,提供一个默认值以防万一 + const finalOutputFilename = outputFilename || "temp_download"; + if (url.includes("music")) { // 这里是 YouTube Music的处理逻辑 // e.g yt-dlp -x --audio-format mp3 https://youtu.be/5wEtefq9VzM -o test.mp3 - command = `yt-dlp -x --audio-format flac -f ba ${cookieParam} ${constructProxyParam(isOversea, proxy)} -P ${path} -o "temp.flac" ${url}`; + command = `yt-dlp -x --audio-format flac -f ba ${cookieParam} ${constructProxyParam(isOversea, proxy)} -P "${path}" -o "${finalOutputFilename}.flac" ${url}`; } else { // 正常情况下的处理逻辑 const fParam = url.includes("youtu") ? `--download-sections "*${timeRange}" -f "bv${graphics}[ext=mp4]+ba[ext=m4a]" ` : ""; - command = `yt-dlp -N ${maxThreads} ${fParam} --concurrent-fragments ${maxThreads} ${cookieParam} ${constructProxyParam(isOversea, proxy)} -P ${path} -o "temp.%(ext)s" ${url}`; + command = `yt-dlp -N ${maxThreads} ${fParam} --concurrent-fragments ${maxThreads} ${cookieParam} ${constructProxyParam(isOversea, proxy)} -P "${path}" -o "${finalOutputFilename}.%(ext)s" ${url}`; } logger.info(`[R插件][yt-dlp审计] ${command}`);