From 973ccaebed4d96534ccbd7386f51330c949883bd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=89=8B=E7=93=9C=E4=B8=80=E5=8D=81=E9=9B=AA?= Date: Fri, 13 Mar 2026 19:48:14 +0800 Subject: [PATCH] Throw on invalid ffprobe duration, remove prefetch MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Remove prefetching of video duration from the initial Promise.all and change getDuration to stop swallowing errors: it now throws when ffprobe returns a non-numeric duration (including the raw stdout in the error) instead of returning a default 60s. This prevents silently masking ffprobe failures — callers should handle or catch errors from getDuration. --- .../helper/ffmpeg/ffmpeg-exec-adapter.ts | 31 ++++++++++--------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/packages/napcat-core/helper/ffmpeg/ffmpeg-exec-adapter.ts b/packages/napcat-core/helper/ffmpeg/ffmpeg-exec-adapter.ts index da2a8b222..ff04b2730 100644 --- a/packages/napcat-core/helper/ffmpeg/ffmpeg-exec-adapter.ts +++ b/packages/napcat-core/helper/ffmpeg/ffmpeg-exec-adapter.ts @@ -101,7 +101,6 @@ export class FFmpegExecAdapter implements IFFmpegAdapter { // 获取文件大小和类型 const [fileType, duration] = await Promise.all([ fileTypeFromFile(videoPath).catch(() => null), - this.getDuration(videoPath), ]); // 创建临时缩略图路径 @@ -136,22 +135,24 @@ export class FFmpegExecAdapter implements IFFmpegAdapter { } /** - * 获取时长 - */ - async getDuration (filePath: string): Promise { - try { - const { stdout } = await execFileAsync(this.ffprobePath, [ - '-v', 'error', - '-show_entries', 'format=duration', - '-of', 'default=noprint_wrappers=1:nokey=1', - filePath, - ]); + * 获取时长 + */ + async getDuration(filePath: string): Promise { + const { stdout } = await execFileAsync(this.ffprobePath, [ + "-v", + "error", + "-show_entries", + "format=duration", + "-of", + "default=noprint_wrappers=1:nokey=1", + filePath, + ]); - const duration = parseFloat(stdout.trim()); - return isNaN(duration) ? 60 : duration; - } catch { - return 60; // 默认时长 + const duration = parseFloat(stdout.trim()); + if (isNaN(duration)) { + throw new Error(`ffprobe 返回了无效的时长值: "${stdout.trim()}"`); } + return duration; } /**