Skip to content

🎬 火宝短剧 - 基于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(desktop): win 交叉打包修复 + ffmpeg 按平台分包瘦身

Connor37c448e3a0e6
diff --git a/desktop/electron-builder.yml b/desktop/electron-builder.yml
index aedc914..5cce0e4 100644
--- a/desktop/electron-builder.yml
+++ b/desktop/electron-builder.yml
@@ -18,8 +18,12 @@ asarUnpack:
extraResources:
- { from: resources/frontend, to: frontend }
- { from: resources/workspace-template, to: workspace-template }
- - { from: resources/bin, to: bin }
+ # ${os} 宏:darwin → bin-darwin/(无扩展名二进制),win32 → bin-win32/(.exe),
+ # 各自只带本平台的 ffmpeg/ffprobe
+ - { from: "resources/bin-${os}", to: bin }
npmRebuild: true
+# win 交叉打包修复:宿主机(mac)下载的原生预编译是 Mach-O,afterPack 换入官方 win32 .node
+afterPack: scripts/after-pack.mjs
mac:
target:
- target: dmg
diff --git a/desktop/scripts/after-pack.mjs b/desktop/scripts/after-pack.mjs
new file mode 100644
index 0000000..ba068f1
--- /dev/null
+++ b/desktop/scripts/after-pack.mjs
@@ -0,0 +1,48 @@
+/**
+ * electron-builder afterPack 钩子 — 修 Windows 包的 better-sqlite3 原生模块。
+ *
+ * 背景:dist:win 在 mac 上交叉打包,@electron/rebuild/prebuild-install 按宿主机
+ * 平台下载预编译产物,导致 win 包里的 build/Release/better_sqlite3.node 实为
+ * Mach-O(mac 二进制),Windows 上 dlopen 失败 → 后端 exit code 1。
+ *
+ * 修复:打包后(win-unpacked 生成、NSIS 封装前)用官方 win32-x64 预编译 .node
+ * 覆盖。预编译文件缓存于 build/win-bin/better_sqlite3.node(同 ffmpeg.exe 的
+ * 缓存方式;better-sqlite3 或 Electron 主版本升级时需重新下载对应 ABI:
+ * https://github.com/WiseLibs/better-sqlite3/releases)
+ */
+import fs from 'node:fs'
+import path from 'node:path'
+import { fileURLToPath } from 'node:url'
+
+const WIN_BIN = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..', 'build', 'win-bin', 'better_sqlite3.node')
+
+export default async function afterPack(context) {
+ // electron-builder 对缺失的 extraResources 只警告不报错(曾导致整包没打进 ffmpeg),
+ // 这里硬性校验:任何平台都必须有本平台的 ffmpeg/ffprobe
+ const resourcesDir = context.electronPlatformName === 'darwin'
+ ? path.join(context.appOutDir, `${context.packager.appInfo.productFilename}.app`, 'Contents', 'Resources')
+ : path.join(context.appOutDir, 'resources')
+ const exe = context.electronPlatformName === 'win32' ? '.exe' : ''
+ for (const name of [`ffmpeg${exe}`, `ffprobe${exe}`]) {
+ const p = path.join(resourcesDir, 'bin', name)
+ if (!fs.existsSync(p)) throw new Error(`打包产物缺少 bin/${name}(检查 prepare-resources 与 \${os} 目录命名)`)
+ }
+
+ if (context.electronPlatformName !== 'win32') return
+ if (!fs.existsSync(WIN_BIN)) {
+ throw new Error(
+ `缺少 win32 better-sqlite3 预编译: ${WIN_BIN}\n` +
+ '下载 https://github.com/WiseLibs/better-sqlite3/releases/download/v12.11.1/' +
+ 'better-sqlite3-v12.11.1-electron-v136-win32-x64.tar.gz 解出 build/Release/better_sqlite3.node 放入该路径',
+ )
+ }
+ const target = path.join(
+ context.appOutDir, 'resources', 'app.asar.unpacked', 'node_modules',
+ 'better-sqlite3', 'build', 'Release', 'better_sqlite3.node',
+ )
+ if (!fs.existsSync(target)) throw new Error(`未找到待替换的 better_sqlite3.node: ${target}`)
+ fs.copyFileSync(WIN_BIN, target)
+ // 顺手清掉宿主机平台预编译残留(bindings 不会命中,但白占体积)
+ fs.rmSync(path.join(path.dirname(path.dirname(path.dirname(target))), 'bin'), { recursive: true, force: true })
+ console.log(`[after-pack] win32 better_sqlite3.node 已替换为 PE 二进制 (${(fs.statSync(target).size / 1024 / 1024).toFixed(1)}MB)`)
+}
diff --git a/desktop/scripts/prepare-resources.mjs b/desktop/scripts/prepare-resources.mjs
index df1426f..bbf3bc2 100644
--- a/desktop/scripts/prepare-resources.mjs
+++ b/desktop/scripts/prepare-resources.mjs
@@ -3,7 +3,8 @@
*
* 1. frontend/ ← nuxt generate 产物(.output/public,含 index.html)
* 2. workspace-template/ ← backend/workspace(skills + prompts,首启动拷入 userData)
- * 3. bin/ ← ffmpeg/ffprobe 静态二进制(保留执行位)
+ * 3. bin-<os>/ ← ffmpeg/ffprobe 按平台分目录(electron-builder ${os} 宏各取所需,
+ * 避免 mac 包带 exe、win 包带 mac 二进制白白 +144MB)
*/
import fs from 'fs'
import path from 'path'
@@ -36,19 +37,21 @@ console.log('resources/workspace-template ✓')
// Windows 交叉打包:ffmpeg.exe 从 ffmpeg-static GitHub release 获取(本地缓存),
// ffprobe.exe 直接用 ffprobe-static 自带的 win32/x64 产物)
const req = createRequire(import.meta.url)
-const binDir = path.join(RES, 'bin')
-fs.mkdirSync(binDir, { recursive: true })
+const binMac = path.join(RES, 'bin-mac')
+const binWin = path.join(RES, 'bin-win')
+fs.mkdirSync(binMac, { recursive: true })
+fs.mkdirSync(binWin, { recursive: true })
const ffmpegPath = req('ffmpeg-static')
const ffprobePath = req('ffprobe-static')?.path
if (!ffmpegPath || !ffprobePath || !fs.existsSync(ffmpegPath) || !fs.existsSync(ffprobePath)) {
console.error('ffmpeg-static/ffprobe-static 二进制缺失,请重新 npm install(或配置 FFMPEG_BINARIES_URL 镜像)')
process.exit(1)
}
-fs.copyFileSync(ffmpegPath, path.join(binDir, 'ffmpeg'))
-fs.copyFileSync(ffprobePath, path.join(binDir, 'ffprobe'))
-fs.chmodSync(path.join(binDir, 'ffmpeg'), 0o755)
-fs.chmodSync(path.join(binDir, 'ffprobe'), 0o755)
-console.log('resources/bin ✓')
+fs.copyFileSync(ffmpegPath, path.join(binMac, 'ffmpeg'))
+fs.copyFileSync(ffprobePath, path.join(binMac, 'ffprobe'))
+fs.chmodSync(path.join(binMac, 'ffmpeg'), 0o755)
+fs.chmodSync(path.join(binMac, 'ffprobe'), 0o755)
+console.log('resources/bin-mac ✓')
// 3b. Windows 二进制(打 win 包用;不打 win 包时缺失不报错,仅提示)
const winBinDir = path.join(DESKTOP, 'build', 'win-bin')
@@ -59,7 +62,7 @@ if (!fs.existsSync(ffmpegWin)) {
'获取: https://github.com/eugeneware/ffmpeg-static/releases/download/b6.0/ffmpeg-win32-x64')
}
if (fs.existsSync(ffmpegWin) && fs.existsSync(ffprobeWinSrc)) {
- fs.copyFileSync(ffmpegWin, path.join(binDir, 'ffmpeg.exe'))
- fs.copyFileSync(ffprobeWinSrc, path.join(binDir, 'ffprobe.exe'))
- console.log('resources/bin (win64) ✓')
+ fs.copyFileSync(ffmpegWin, path.join(binWin, 'ffmpeg.exe'))
+ fs.copyFileSync(ffprobeWinSrc, path.join(binWin, 'ffprobe.exe'))
+ console.log('resources/bin-win ✓')
}