diff --git a/src/common/download-ffmpeg.ts b/src/common/download-ffmpeg.ts index cba7e8be..1eb6dd27 100644 --- a/src/common/download-ffmpeg.ts +++ b/src/common/download-ffmpeg.ts @@ -281,28 +281,72 @@ export async function downloadFFmpeg( return null; } } + +/** + * 检查系统PATH环境变量中是否存在指定可执行文件 + * @param executable 可执行文件名 + * @returns 如果找到返回完整路径,否则返回null + */ +function findExecutableInPath(executable: string): string | null { + // 仅适用于Windows系统 + if (os.platform() !== 'win32') return null; + + // 获取PATH环境变量 + const pathEnv = process.env['PATH'] || ''; + const pathDirs = pathEnv.split(';'); + + // 检查每个目录 + for (const dir of pathDirs) { + if (!dir) continue; + try { + const filePath = path.join(dir, executable); + if (fs.existsSync(filePath)) { + return filePath; + } + } catch (error) { + continue; + } + } + + return null; +} + export async function downloadFFmpegIfNotExists(log: LogWrapper) { // 仅限Windows if (os.platform() !== 'win32') { return { path: null, - isExist: false + reset: false }; } + const ffmpegInPath = findExecutableInPath('ffmpeg.exe'); + const ffprobeInPath = findExecutableInPath('ffprobe.exe'); + + if (ffmpegInPath && ffprobeInPath) { + const ffmpegDir = path.dirname(ffmpegInPath); + return { + path: ffmpegDir, + reset: true + }; + } + + // 如果环境变量中没有,检查项目目录中是否存在 const currentPath = path.dirname(fileURLToPath(import.meta.url)); const ffmpeg_exist = fs.existsSync(path.join(currentPath, 'ffmpeg', 'ffmpeg.exe')); const ffprobe_exist = fs.existsSync(path.join(currentPath, 'ffmpeg', 'ffprobe.exe')); + if (!ffmpeg_exist || !ffprobe_exist) { await downloadFFmpeg(path.join(currentPath, 'ffmpeg'), path.join(currentPath, 'cache'), (percentage: number, message: string) => { - log.log(`[Ffmpeg] [Download] ${percentage}% - ${message}`); + log.log(`[FFmpeg] [Download] ${percentage}% - ${message}`); }); return { path: path.join(currentPath, 'ffmpeg'), - isExist: false + reset: true } } + return { path: path.join(currentPath, 'ffmpeg'), - isExist: true + reset: true } } \ No newline at end of file diff --git a/src/common/ffmpeg.ts b/src/common/ffmpeg.ts index ffb285d5..40385d23 100644 --- a/src/common/ffmpeg.ts +++ b/src/common/ffmpeg.ts @@ -7,6 +7,7 @@ import { fileTypeFromFile } from 'file-type'; import imageSize from 'image-size'; import { fileURLToPath } from 'node:url'; import { platform } from 'node:os'; +import { LogWrapper } from './log'; const currentPath = dirname(fileURLToPath(import.meta.url)); const execFileAsync = promisify(execFile); const getFFmpegPath = (tool: string): string => { @@ -19,16 +20,14 @@ const getFFmpegPath = (tool: string): string => { }; export let FFMPEG_CMD = getFFmpegPath('ffmpeg'); export let FFPROBE_CMD = getFFmpegPath('ffprobe'); -console.log('[Info] ffmpeg:', FFMPEG_CMD); -console.log('[Info] ffprobe:', FFPROBE_CMD); export class FFmpegService { // 确保目标目录存在 - public static setFfmpegPath(ffmpegPath: string): void { + public static setFfmpegPath(ffmpegPath: string,logger:LogWrapper): void { if (platform() === 'win32') { FFMPEG_CMD = path.join(ffmpegPath, 'ffmpeg.exe'); FFPROBE_CMD = path.join(ffmpegPath, 'ffprobe.exe'); - console.log('[Info] ffmpeg:', FFMPEG_CMD); - console.log('[Info] ffprobe:', FFPROBE_CMD); + logger.log('[Check] ffmpeg:', FFMPEG_CMD); + logger.log('[Check] ffprobe:', FFPROBE_CMD); } } private static ensureDirExists(filePath: string): void { diff --git a/src/framework/napcat.ts b/src/framework/napcat.ts index 6b571925..9f19cecf 100644 --- a/src/framework/napcat.ts +++ b/src/framework/napcat.ts @@ -38,9 +38,9 @@ export async function NCoreInitFramework( const logger = new LogWrapper(pathWrapper.logsPath); const basicInfoWrapper = new QQBasicInfoWrapper({ logger }); const wrapper = loadQQWrapper(basicInfoWrapper.getFullQQVesion()); - downloadFFmpegIfNotExists(logger).then(({ path, isExist }) => { - if (!isExist && path) { - FFmpegService.setFfmpegPath(path); + downloadFFmpegIfNotExists(logger).then(({ path, reset }) => { + if (reset && path) { + FFmpegService.setFfmpegPath(path,logger); } }).catch(e => { logger.logError('[Ffmpeg] Error:', e); diff --git a/src/shell/base.ts b/src/shell/base.ts index 98d3077a..9dbd3c74 100644 --- a/src/shell/base.ts +++ b/src/shell/base.ts @@ -313,9 +313,9 @@ export async function NCoreInitShell() { const pathWrapper = new NapCatPathWrapper(); const logger = new LogWrapper(pathWrapper.logsPath); handleUncaughtExceptions(logger); - downloadFFmpegIfNotExists(logger).then(({ path, isExist }) => { - if (!isExist && path) { - FFmpegService.setFfmpegPath(path); + downloadFFmpegIfNotExists(logger).then(({ path, reset }) => { + if (reset && path) { + FFmpegService.setFfmpegPath(path,logger); } }).catch(e => { logger.logError('[Ffmpeg] Error:', e);