🎬 火宝短剧 - 基于AI的一站式短剧生成平台 《一句话生成完整短剧,从剧本到成片全自动化》 Huobao Drama - An AI-Powered End-to-End Short Drama Generator "One Sentence to Complete Drama: Fully Automated from Script to Final Video"
TypeScript
fix(ffmpeg): 二进制损坏时探测降级,不再同步 EFTYPE 崩掉进程
Connorf04d705603bd
diff --git a/backend/src/services/ffmpeg-merge.ts b/backend/src/services/ffmpeg-merge.tsindex ff63cac..31df912 100644--- a/backend/src/services/ffmpeg-merge.ts+++ b/backend/src/services/ffmpeg-merge.ts@@ -1,9 +1,6 @@/*** FFmpeg 多镜头拼接 — 将所有生成后的镜头视频拼接为一集*/-import ffmpeg from 'fluent-ffmpeg'-import ffmpegPath from 'ffmpeg-static'-import { createRequire } from 'module'import fs from 'fs'import path from 'path'import { fileURLToPath } from 'url'@@ -13,13 +10,7 @@ import { eq } from 'drizzle-orm'import { now } from '../utils/response.js'import { logTaskError, logTaskStart, logTaskSuccess } from '../utils/task-logger.js'import { extractVideoPoster } from '../utils/video-poster.js'--// ffprobe-static 无类型声明,用 createRequire 引入(仓库 .gitignore 忽略 *.d.ts)-const ffprobeStatic = createRequire(import.meta.url)('ffprobe-static') as { path: string }--// 系统未安装 ffmpeg 时使用项目内置二进制-if (ffmpegPath) ffmpeg.setFfmpegPath(ffmpegPath)-if (ffprobeStatic?.path) ffmpeg.setFfprobePath(ffprobeStatic.path)+import { ffmpeg, checkFfmpegSuite } from '../utils/ffmpeg.js'const __dirname = path.dirname(fileURLToPath(import.meta.url))const STORAGE_ROOT = process.env.STORAGE_PATH || path.resolve(__dirname, '../../../data/static')@@ -53,6 +44,13 @@ export async function mergeEpisodeVideos(episodeId: number, dramaId: number, stoif (clips.length === 0) throw new Error('所选镜头还没有可拼接的视频')+ // 拼接前探测 ffmpeg:二进制损坏时 fluent-ffmpeg 的同步 EFTYPE 会崩掉整个进程,+ // 这里提前拦截并给出可操作的修复指引(路由层会作为 400 返回前端)+ const suite = await checkFfmpegSuite()+ if (!suite.ffmpeg || !suite.ffprobe) {+ throw new Error('本机 ffmpeg 不可用,无法拼接视频(常见于 node_modules 跨平台拷贝或 ffmpeg-static 下载损坏)。请删除 node_modules 后在本机重新 npm install,或设置 FFMPEG_BIN 指向有效的 ffmpeg 可执行文件后重启服务')+ }+// 校验视频文件真实存在:DB 里的 video_url 可能指向已被清理的文件,// 直接拼会得到 ffmpeg 的 "No such file or directory" 晦涩报错const missing = clips.filter(c => !fs.existsSync(toAbsPath(c.url)))diff --git a/backend/src/utils/ffmpeg.ts b/backend/src/utils/ffmpeg.tsnew file mode 100644index 0000000..2545027--- /dev/null+++ b/backend/src/utils/ffmpeg.ts@@ -0,0 +1,79 @@+/**+ * FFmpeg 统一配置与可用性探测+ *+ * 背景:Windows 上若 node_modules 跨平台拷贝、或 ffmpeg-static postinstall+ * 下载损坏(HTML 错误页被存成 ffmpeg.exe),spawn 非 PE 文件会同步抛+ * EFTYPE;该异常发生在 fluent-ffmpeg 的延迟回调里,外层 try/catch 接不住,+ * 直接崩掉整个 Node 进程。这里在业务调用前主动 spawn -version 探测,+ * 不可用时由业务侧优雅降级(海报帧跳过 / 拼接返回明确错误)。+ */+import { spawn } from 'child_process'+import fs from 'fs'+import ffmpeg from 'fluent-ffmpeg'+import ffmpegPathImport from 'ffmpeg-static'+import { createRequire } from 'module'++// ffprobe-static 无类型声明,用 createRequire 引入(仓库 .gitignore 忽略 *.d.ts)+const ffprobeStatic = createRequire(import.meta.url)('ffprobe-static') as { path: string }++// ffmpeg-static 类型声明为 string,实际平台不支持时为 null+const ffmpegPath = ffmpegPathImport as string | null+const ffprobePath = (ffprobeStatic?.path as string | null) || null++// 系统未安装 ffmpeg 时使用项目内置二进制+if (ffmpegPath) ffmpeg.setFfmpegPath(ffmpegPath)+if (ffprobePath) ffmpeg.setFfprobePath(ffprobePath)++export { ffmpeg }++/** spawn 一次 -version 验证二进制真实可执行(含同步 EFTYPE 兜底) */+function probeBinary(binPath: string | null): Promise<boolean> {+ if (!binPath || !fs.existsSync(binPath)) return Promise.resolve(false)+ return new Promise((resolve) => {+ let settled = false+ const finish = (ok: boolean) => {+ if (settled) return+ settled = true+ clearTimeout(timer)+ resolve(ok)+ }+ const timer = setTimeout(() => finish(false), 10_000)+ let child+ try {+ child = spawn(binPath, ['-version'], { stdio: 'ignore', windowsHide: true })+ } catch {+ // Windows 上 spawn 非 PE 文件(如下载损坏的 ffmpeg.exe)会同步抛 EFTYPE+ finish(false)+ return+ }+ child.on('error', () => finish(false))+ child.on('close', (code) => finish(code === 0))+ })+}++export interface FfmpegSuite {+ ffmpeg: boolean+ ffprobe: boolean+}++// 成功的探测结果缓存(进程内只探测一次);失败不缓存,修复二进制后无需重启即可恢复+let okCache: FfmpegSuite | null = null+let warned = false++/** 探测内置 ffmpeg/ffprobe 是否可用 */+export async function checkFfmpegSuite(): Promise<FfmpegSuite> {+ if (okCache) return okCache+ const [ffmpegOk, ffprobeOk] = await Promise.all([probeBinary(ffmpegPath), probeBinary(ffprobePath)])+ const suite = { ffmpeg: ffmpegOk, ffprobe: ffprobeOk }+ if (ffmpegOk && ffprobeOk) {+ okCache = suite+ } else if (!warned) {+ warned = true+ console.warn(+ `[ffmpeg] 内置二进制不可用 (ffmpeg=${ffmpegOk ? 'ok' : 'FAIL'}, ffprobe=${ffprobeOk ? 'ok' : 'FAIL'})。` ++ `海报帧提取与视频拼接将跳过/失败。修复:删除 node_modules 后在本机重新 npm install,` ++ `或设置 FFMPEG_BIN 指向有效的 ffmpeg 可执行文件`+ )+ }+ return suite+}diff --git a/backend/src/utils/video-poster.ts b/backend/src/utils/video-poster.tsindex 4317a21..981fc17 100644--- a/backend/src/utils/video-poster.ts+++ b/backend/src/utils/video-poster.ts@@ -1,13 +1,9 @@/*** 视频海报帧提取 — 供 <video poster> 使用,避免列表页为显示首帧而缓冲整个视频*/-import ffmpeg from 'fluent-ffmpeg'-import ffmpegPath from 'ffmpeg-static'import path from 'path'import { getAbsolutePath } from './storage.js'--// 系统未安装 ffmpeg 时使用项目内置二进制(与 ffmpeg-merge.ts 同一策略)-if (ffmpegPath) ffmpeg.setFfmpegPath(ffmpegPath)+import { ffmpeg, checkFfmpegSuite } from './ffmpeg.js'/** 由视频相对路径推导海报帧路径:static/videos/x.mp4 → static/videos/x_poster.jpg */export function posterPathFor(relativePath: string): string {@@ -20,6 +16,11 @@ export function posterPathFor(relativePath: string): string {*/export async function extractVideoPoster(relativePath: string): Promise<string | null> {try {+ // ffmpeg 二进制损坏(跨平台拷贝 node_modules / 下载不完整)时静默跳过,+ // 避免 fluent-ffmpeg 延迟回调里的同步 EFTYPE 崩掉整个进程+ const { ffmpeg: ffmpegOk } = await checkFfmpegSuite()+ if (!ffmpegOk) return null+const posterRel = posterPathFor(relativePath)const posterAbs = getAbsolutePath(posterRel)await new Promise<void>((resolve, reject) => {