From d2ecdd2542f1c251acbbe015ce5c4feae08738ed Mon Sep 17 00:00:00 2001 From: CSSZYF <63802741+CSSZYF@users.noreply.github.com> Date: Tue, 3 Jun 2025 20:33:06 +0800 Subject: [PATCH 1/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8DWindows=E5=AF=B9TiK=20ToK?= =?UTF-8?q?=E8=A7=A3=E6=9E=90=E4=B8=AD=E6=96=87=E4=B9=B1=E7=A0=81=E7=9A=84?= =?UTF-8?q?=E9=97=AE=E9=A2=98=20=E5=BC=BA=E5=88=B6=E8=BF=9B=E8=A1=8CUTF-8?= =?UTF-8?q?=20=E7=BC=96=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/yt-dlp-util.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/utils/yt-dlp-util.js b/utils/yt-dlp-util.js index 42dffc9..8b977d8 100644 --- a/utils/yt-dlp-util.js +++ b/utils/yt-dlp-util.js @@ -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 编码 } From 1523287649b745dfe2d2d04ee61b29f664fce4a2 Mon Sep 17 00:00:00 2001 From: CSSZYF <63802741+CSSZYF@users.noreply.github.com> Date: Tue, 3 Jun 2025 20:40:25 +0800 Subject: [PATCH 2/5] =?UTF-8?q?=E5=B0=86=20yt-dlp=20=E7=9B=B8=E5=85=B3?= =?UTF-8?q?=E5=87=BD=E6=95=B0=E6=94=B9=E4=B8=BA=E5=BC=82=E6=AD=A5=E6=89=A7?= =?UTF-8?q?=E8=A1=8C=E4=BB=A5=E8=A7=A3=E5=86=B3=E8=BF=9B=E7=A8=8B=E9=98=BB?= =?UTF-8?q?=E5=A1=9E?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/yt-dlp-util.js | 56 ++++++++++++++++++++++++++++++++++---------- 1 file changed, 43 insertions(+), 13 deletions(-) diff --git a/utils/yt-dlp-util.js b/utils/yt-dlp-util.js index 8b977d8..06cf9ca 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"; /** * 构建梯子参数 @@ -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()); + } + }); + }); } /** @@ -69,10 +89,20 @@ export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") { * @param proxy * @param cookiePath */ -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 = "") { + return new Promise((resolve, reject) => { + // 构造 cookie 参数 + const cookieParam = constructCookiePath(url, cookiePath); + const command = `yt-dlp --write-thumbnail --convert-thumbnails png --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url} -P "${path}" -o "thumbnail.%(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 + } + }); + }); } /** From 0d7fbe63be625d69dba7872387fd2b843caa15d7 Mon Sep 17 00:00:00 2001 From: CSSZYF <63802741+CSSZYF@users.noreply.github.com> Date: Tue, 3 Jun 2025 20:41:13 +0800 Subject: [PATCH 3/5] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E8=A7=A3=E6=9E=90=20YouT?= =?UTF-8?q?ube=20=E8=A7=86=E9=A2=91=E4=BF=A1=E6=81=AF=E6=97=B6=E9=98=BB?= =?UTF-8?q?=E5=A1=9E=E4=B8=BB=E8=BF=9B=E7=A8=8B=EF=BC=8C=E6=8F=90=E9=AB=98?= =?UTF-8?q?=E6=9C=BA=E5=99=A8=E4=BA=BA=E5=93=8D=E5=BA=94=E6=96=B0=E6=B6=88?= =?UTF-8?q?=E6=81=AF=E7=9A=84=E5=8F=8A=E6=97=B6=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tools.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/apps/tools.js b/apps/tools.js index 8614b73..6e40d8e 100644 --- a/apps/tools.js +++ b/apps/tools.js @@ -641,7 +641,7 @@ export class tools extends plugin { // 下载逻辑 const path = this.getCurDownloadPath(e); await checkAndRemoveFile(path + "/temp.mp4"); - const title = ytDlpGetTilt(url, isOversea, this.myProxy); + const title = await 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`); @@ -2150,7 +2150,7 @@ export class tools extends plugin { 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, ''); + const title = (await ytDlpGetTilt(url, isOversea, this.myProxy, this.youtubeCookiePath)).toString().replace(/\n/g, ''); // 音频逻辑 if (url.includes("music")) { @@ -2168,7 +2168,7 @@ export class tools extends plugin { } // 下面为视频逻辑 - 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([ From 3abb3b1fa638ce7e601ea1a4c6462546d4392360 Mon Sep 17 00:00:00 2001 From: CSSZYF <63802741+CSSZYF@users.noreply.github.com> Date: Tue, 3 Jun 2025 21:27:43 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E4=B8=B4=E6=97=B6=E4=BF=AE=E5=A4=8Dtiktok?= =?UTF-8?q?=E5=92=8Cyoutube=20=E9=98=B2=E6=AD=A2=E8=A7=A3=E6=9E=90?= =?UTF-8?q?=E8=BF=87=E6=85=A2=E8=A2=AB=E6=96=87=E4=BB=B6=E8=A6=86=E7=9B=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/tools.js | 81 +++++++++++++++++++++++++++++++++++---------------- 1 file changed, 56 insertions(+), 25 deletions(-) diff --git a/apps/tools.js b/apps/tools.js index 6e40d8e..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 = await 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,23 +2156,42 @@ 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 = (await 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; } @@ -2172,20 +2201,20 @@ export class tools extends plugin { // 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); // 上传成功后删除 } } From 63d24efd6bc52d3606a1a18e9b2c94752125906b Mon Sep 17 00:00:00 2001 From: CSSZYF <63802741+CSSZYF@users.noreply.github.com> Date: Tue, 3 Jun 2025 21:29:05 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E7=A1=AE=E4=BF=9Dtiktok=E5=92=8Cyoutube?= =?UTF-8?q?=E8=A7=86=E9=A2=91=E6=96=87=E4=BB=B6=E8=A2=AB=E5=88=A0=E9=99=A4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- utils/yt-dlp-util.js | 16 +++++++++++----- 1 file changed, 11 insertions(+), 5 deletions(-) diff --git a/utils/yt-dlp-util.js b/utils/yt-dlp-util.js index 06cf9ca..762dddc 100644 --- a/utils/yt-dlp-util.js +++ b/utils/yt-dlp-util.js @@ -88,12 +88,14 @@ export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") { * @param isOversea * @param proxy * @param cookiePath + * @param thumbnailFilenamePrefix 缩略图文件名前缀 (不含扩展名) */ -export function ytDlpGetThumbnail(path, 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 command = `yt-dlp --write-thumbnail --convert-thumbnails png --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url} -P "${path}" -o "thumbnail.%(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}`); @@ -116,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}`);