diff --git a/apps/tools.js b/apps/tools.js index 58f58e7..bbea87a 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -2163,20 +2163,16 @@ export class tools extends plugin { // 使用安全标题作为文件名前缀 const videoFilename = `${safeTitlePrefix}.mp4`; const audioFilename = `${safeTitlePrefix}.flac`; - const thumbnailFilename = `${safeTitlePrefix}_thumbnail.png`; // 确保缩略图文件名也唯一 + const thumbnailFilenamePrefix = `${safeTitlePrefix}_thumbnail`; // 清理可能存在的旧文件或同名文件 - 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 checkAndRemoveFile(`${path}/${videoFilename}`); + await checkAndRemoveFile(`${path}/${audioFilename}`); + await checkAndRemoveFile(`${path}/${thumbnailFilenamePrefix}.png`); - - await ytDlpGetThumbnail(path, url, isOversea, this.myProxy, this.youtubeCookiePath, safeTitlePrefix + "_thumbnail"); // 传递不带扩展名的前缀 - const fullThumbnailPath = `${path}/${thumbnailFilename}`; + // 下载缩略图并获取实际文件名 + const actualThumbnailFilename = await ytDlpGetThumbnail(path, url, isOversea, this.myProxy, this.youtubeCookiePath, thumbnailFilenamePrefix); + const fullThumbnailPath = `${path}/${actualThumbnailFilename}`; // 音频逻辑 @@ -2204,17 +2200,21 @@ export class tools extends plugin { 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$/, '') } 分钟` ]); + await checkAndRemoveFile(fullThumbnailPath); // 删除缩略图 } else if (Duration > this.youtubeClipTime && timeRange != '00:00:00-00:00:00') { e.reply([ 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将截取视频片段` ]); + // 注意:sendVideoToUpload 内部会删除视频文件,但不会删除缩略图 await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, safeTitlePrefix, true, graphics, timeRange, this.youtubeCookiePath); - this.sendVideoToUpload(e, `${ path }/${ videoFilename }`); + await this.sendVideoToUpload(e, `${ path }/${ videoFilename }`); + await checkAndRemoveFile(fullThumbnailPath); // 删除缩略图 } else { 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 }`); + await this.sendVideoToUpload(e, `${ path }/${ videoFilename }`); + await checkAndRemoveFile(fullThumbnailPath); // 删除缩略图 } } catch (error) { logger.error(error); diff --git a/utils/yt-dlp-util.js b/utils/yt-dlp-util.js index 762dddc..686af48 100644 --- a/utils/yt-dlp-util.js +++ b/utils/yt-dlp-util.js @@ -43,7 +43,7 @@ export function ytDlpGetDuration(url, isOversea, proxy, cookiePath = "") { return new Promise((resolve, reject) => { // 构造 cookie 参数 const cookieParam = constructCookiePath(url, cookiePath); - const command = `yt-dlp --get-duration --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url}`; + 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}`); @@ -69,7 +69,7 @@ export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") { const cookieParam = constructCookiePath(url, cookiePath); // 构造 编码 参数 const encodingParam = constructEncodingParam(url); - const command = `yt-dlp --get-title --skip-download ${cookieParam} ${ constructProxyParam(isOversea, proxy) } ${ url } ${encodingParam}`; + 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}`); @@ -92,16 +92,29 @@ export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") { */ 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"`; + 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); + return reject(error); + } + // 从yt-dlp的输出中提取文件名 + const match = stdout.match(/Writing thumbnail to: (.*)/); + if (match && match[1]) { + const thumbnailPath = match[1].trim(); + // 只返回文件名部分 + const thumbnailFilename = thumbnailPath.split(/[\\/]/).pop(); + logger.info(`[R插件][yt-dlp审计] Thumbnail downloaded: ${thumbnailFilename}`); + resolve(thumbnailFilename); } else { - resolve(); // The return value is not used + // 兜底方案:如果无法从输出中解析,则按原逻辑拼接 + logger.warn("[R插件][yt-dlp审计] Could not parse thumbnail filename from stdout. Falling back to default."); + // 尝试查找文件,因为yt-dlp可能没有输出我们期望的格式 + const expectedPngPath = `${finalThumbnailName}.png`; + resolve(expectedPngPath); } }); }); @@ -132,12 +145,12 @@ export async function ytDlpHelper(path, url, isOversea, proxy, maxThreads, outpu 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 "${finalOutputFilename}.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 "${finalOutputFilename}.%(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}`);