fix: 自动化验证环境变量的ffmpeg

This commit is contained in:
手瓜一十雪
2025-04-17 17:59:06 +08:00
parent eca73eae18
commit d49e69735a
4 changed files with 58 additions and 15 deletions

View File

@@ -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
}
}

View File

@@ -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 {

View File

@@ -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);

View File

@@ -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);