Merge pull request #68 from CSSZYF/master

改善并修复一些问题
This commit is contained in:
RrOrange 2025-06-04 15:47:27 +08:00 committed by GitHub
commit f4e671aed6
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 110 additions and 43 deletions

View File

@ -640,11 +640,21 @@ export class tools extends plugin {
const cleanedTiktokUrl = parsedUrl.toString(); const cleanedTiktokUrl = parsedUrl.toString();
// 下载逻辑 // 下载逻辑
const path = this.getCurDownloadPath(e); const path = this.getCurDownloadPath(e);
await checkAndRemoveFile(path + "/temp.mp4"); const rawTitle = (await ytDlpGetTilt(url, isOversea, this.myProxy)).toString().replace(/\n/g, '');
const title = ytDlpGetTilt(url, isOversea, this.myProxy); // 清理文件名并截断到10个字符
e.reply(`${ this.identifyPrefix }识别TikTok视频下载中请耐心等待 \n${ title }`); const safeTitlePrefix = cleanFilename(rawTitle).substring(0, 10);
await ytDlpHelper(path, cleanedTiktokUrl, isOversea, this.myProxy, this.videoDownloadConcurrency); const videoFilename = `${safeTitlePrefix}.mp4`;
await this.sendVideoToUpload(e, `${ path }/temp.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; return true;
} }
@ -2146,46 +2156,65 @@ export class tools extends plugin {
} }
const path = this.getCurDownloadPath(e); 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.mp4");
await checkAndRemoveFile(path + "/temp.flac"); await checkAndRemoveFile(path + "/temp.flac");
await checkAndRemoveFile(path + "/thumbnail.png"); 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")) { if (url.includes("music")) {
e.reply([ e.reply([
segment.image(`${ path }/thumbnail.png`), segment.image(fullThumbnailPath),
`${ this.identifyPrefix }识别:油管音乐\n视频标题:${ title }` `${ 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) { 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; 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) // logger.info('时长------',Duration)
if (Duration > this.youtubeDuration) { if (Duration > this.youtubeDuration) {
e.reply([ e.reply([
segment.image(`${ path }/thumbnail.png`), segment.image(fullThumbnailPath),
`${ 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$/, '') } 分钟` `${ 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') { } else if (Duration > this.youtubeClipTime && timeRange != '00:00:00-00:00:00') {
e.reply([ e.reply([
segment.image(`${ path }/thumbnail.png`), segment.image(fullThumbnailPath),
`${ 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将截取视频片段` `${ 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); await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, safeTitlePrefix, true, graphics, timeRange, this.youtubeCookiePath);
this.sendVideoToUpload(e, `${ path }/temp.mp4`); this.sendVideoToUpload(e, `${ path }/${ videoFilename }`);
} else { } else {
e.reply([segment.image(`${ path }/thumbnail.png`), `${ this.identifyPrefix }识别:油管,视频下载中请耐心等待 \n视频标题:${ title }\n视频时长:${ (Duration / 60).toFixed(2).replace(/\.00$/, '') } 分钟`]); 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, true, graphics, timeRange, this.youtubeCookiePath); await ytDlpHelper(path, url, isOversea, this.myProxy, this.videoDownloadConcurrency, safeTitlePrefix, true, graphics, timeRange, this.youtubeCookiePath);
this.sendVideoToUpload(e, `${ path }/temp.mp4`); this.sendVideoToUpload(e, `${ path }/${ videoFilename }`);
} }
} catch (error) { } catch (error) {
logger.error(error); logger.error(error);
@ -3236,14 +3265,15 @@ export class tools extends plugin {
// 正常发送视频 // 正常发送视频
if (videoSize > videoSizeLimit) { if (videoSize > videoSizeLimit) {
e.reply(`当前视频大小:${ videoSize }MB\n大于设置的最大限制:${ videoSizeLimit }MB\n改为上传群文件`); e.reply(`当前视频大小:${ videoSize }MB\n大于设置的最大限制:${ videoSizeLimit }MB\n改为上传群文件`);
await this.uploadGroupFile(e, path); await this.uploadGroupFile(e, path); // uploadGroupFile 内部会处理删除
} else { } else {
e.reply(segment.video(path)); await e.reply(segment.video(path));
await checkAndRemoveFile(path); // 发送成功后删除
} }
} catch (err) { } catch (err) {
logger.error(`[R插件][发送视频判断是否需要上传] 发生错误:\n ${ 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 { } else {
await e.group.sendFile(path); await e.group.sendFile(path);
} }
await checkAndRemoveFile(path); // 上传成功后删除
} }
} }

View File

@ -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} * @returns {string}
*/ */
function constructEncodingParam(url) { 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 * @returns string
*/ */
export function ytDlpGetDuration(url, isOversea, proxy, cookiePath = "") { export function ytDlpGetDuration(url, isOversea, proxy, cookiePath = "") {
// 构造 cookie 参数 return new Promise((resolve, reject) => {
const cookieParam = constructCookiePath(url, cookiePath); // 构造 cookie 参数
return execSync(`yt-dlp --get-duration --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url}`); 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 * @returns string
*/ */
export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") { export function ytDlpGetTilt(url, isOversea, proxy, cookiePath = "") {
// 构造 cookie 参数 return new Promise((resolve, reject) => {
const cookieParam = constructCookiePath(url, 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}`); 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 isOversea
* @param proxy * @param proxy
* @param cookiePath * @param cookiePath
* @param thumbnailFilenamePrefix 缩略图文件名前缀 (不含扩展名)
*/ */
export function ytDlpGetThumbnail(path, url, isOversea, proxy, cookiePath= "") { export function ytDlpGetThumbnail(path, url, isOversea, proxy, cookiePath = "", thumbnailFilenamePrefix = "thumbnail") {
// 构造 cookie 参数 return new Promise((resolve, reject) => {
const cookieParam = constructCookiePath(url, cookiePath); // 构造 cookie 参数
return execSync(`yt-dlp --write-thumbnail --convert-thumbnails png --skip-download ${cookieParam} ${constructProxyParam(isOversea, proxy)} ${url} -P ${path} -o "thumbnail.%(ext)s"`); 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 graphics YouTube画质参数
* @param timeRange 截取时间段 * @param timeRange 截取时间段
* @param maxThreads 最大并发 * @param maxThreads 最大并发
* @param outputFilename 输出文件名 (不含扩展名)
* @param cookiePath Cookie所在位置 * @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) => { return new Promise((resolve, reject) => {
let command = ""; let command = "";
// 构造 cookie 参数 // 构造 cookie 参数
const cookieParam = constructCookiePath(url, cookiePath); const cookieParam = constructCookiePath(url, cookiePath);
// 确保 outputFilename 不为空,提供一个默认值以防万一
const finalOutputFilename = outputFilename || "temp_download";
if (url.includes("music")) { if (url.includes("music")) {
// 这里是 YouTube Music的处理逻辑 // 这里是 YouTube Music的处理逻辑
// e.g yt-dlp -x --audio-format mp3 https://youtu.be/5wEtefq9VzM -o test.mp3 // 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 { } else {
// 正常情况下的处理逻辑 // 正常情况下的处理逻辑
const fParam = url.includes("youtu") ? `--download-sections "*${timeRange}" -f "bv${graphics}[ext=mp4]+ba[ext=m4a]" ` : ""; 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}`); logger.info(`[R插件][yt-dlp审计] ${command}`);