如何在 Electron 应用程序中捆绑 ffmpeg

How can I bundle ffmpeg in an Electron application

我正在从 electron-webpack 样板开始构建一个 Electron 应用程序。

我找到了这个节点模块 @ffmpeg-installer/ffmpeg,它将兼容的预编译二进制文件安装到 /node_modules 目录中,然后使可执行文件的路径可以通过。

const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path

这在开发期间工作正常,但是当我构建可分发文件并 运行 它时,我在尝试使用该路径生成子进程时遇到错误。估计是因为路径没有指向binary.

运行安装可分发文件时,路径设置如下。

/Users/me/project/dist/mac/AppName.app/Contents/Resources/app.asar/node_modules/@ffmpeg-installer/darwin-x64/ffmpeg

但是,在查看 AppName.app 包内容时,我在以下路径中找到了二进制文件。

/Users/me/project/dist/mac/AppName.app/Contents/Resources/app.asar.unpacked/node_modules/@ffmpeg-installer/darwin-x64/ffmpeg

我应该如何使用 electron-webpackelectron-builder 在 Electron 应用程序中包含二进制依赖项?

这可能是因为 electron 会将应用程序捆绑在 asar 存档中(类似于 zip/tar/jar)。因此,无法解析可执行文件的路径。尝试将 asar: false 传递给 electron-builder(在 electron-builder.json 中)。

From here:

安装:npm i ffmpeg-static ffprobe-static

包含在您的 package.json 中:

build{
...
    "asarUnpack":[
        "node_modules/ffmpeg-static/bin/${os}/${arch}/ffmpeg",
        "node_modules/ffmpeg-static/index.js",
        "node_modules/ffmpeg-static/package.json"
        ]
    }

在你的 JS 中设置路径:

const ffmpeg = require('fluent-ffmpeg');

//Get the paths to the packaged versions of the binaries we want to use
const ffmpegPath = require('ffmpeg-static').replace(
    'app.asar',
    'app.asar.unpacked'
);
const ffprobePath = require('ffprobe-static').path.replace(
    'app.asar',
    'app.asar.unpacked'
);

//tell the ffmpeg package where it can find the needed binaries.
ffmpeg.setFfmpegPath(ffmpegPath);
ffmpeg.setFfprobePath(ffprobePath);