| 1 | const fs = require('fs/promises') |
| 2 | const path = require('path') |
| 3 | const { Arch } = require('builder-util') |
| 4 | |
| 5 | const sourceFileNameFor = (platformName, archName) => { |
| 6 | if (platformName === 'darwin' && archName === 'arm64') return 'ffmpeg-arm' |
| 7 | if (platformName === 'darwin' && archName === 'x64') return 'ffmpeg-intel' |
| 8 | if (platformName === 'win32' && archName === 'x64') return 'ffmpeg.exe' |
| 9 | if (platformName === 'linux' && archName === 'x64') return 'ffmpeg-linux-x64' |
| 10 | return null |
| 11 | } |
| 12 | |
| 13 | const targetFileNameFor = (platformName) => (platformName === 'win32' ? 'ffmpeg.exe' : 'ffmpeg') |
| 14 | |
| 15 | const isRequiredFfmpegFor = (platformName) => platformName !== 'linux' |
| 16 | |
| 17 | exports.default = async function afterPack(context) { |
| 18 | const platformName = context.electronPlatformName |
| 19 | const archName = Arch[context.arch] |
| 20 | const sourceFileName = sourceFileNameFor(platformName, archName) |
| 21 | |
| 22 | if (!sourceFileName) return |
| 23 | |
| 24 | const sourcePath = path.join(context.packager.projectDir, 'resources', 'ffmpeg', sourceFileName) |
| 25 | const resourcesDir = context.packager.getResourcesDir(context.appOutDir) |
| 26 | const targetDir = path.join(resourcesDir, 'app.asar.unpacked', 'resources', 'ffmpeg') |
| 27 | const targetPath = path.join(targetDir, targetFileNameFor(platformName)) |
| 28 | |
| 29 | try { |
| 30 | await fs.access(sourcePath) |
| 31 | } catch { |
| 32 | if (!isRequiredFfmpegFor(platformName)) { |
| 33 | console.warn( |
| 34 | `[afterPack] optional bundled ffmpeg missing for ${platformName}-${archName}: ${sourcePath}. ` + |
| 35 | 'The package will be created without built-in MP4 export support.' |
| 36 | ) |
| 37 | return |
| 38 | } |
| 39 | |
| 40 | throw new Error(`Missing bundled ffmpeg for ${platformName}-${archName}: ${sourcePath}`) |
| 41 | } |
| 42 | |
| 43 | await fs.rm(targetDir, { recursive: true, force: true }) |
| 44 | await fs.mkdir(targetDir, { recursive: true }) |
| 45 | await fs.copyFile(sourcePath, targetPath) |
| 46 | |
| 47 | if (platformName !== 'win32') { |
| 48 | await fs.chmod(targetPath, 0o755) |
| 49 | } |
| 50 | |
| 51 | console.log(`[afterPack] bundled ffmpeg ${sourceFileName} -> ${targetPath}`) |
| 52 | } |
| 53 |