jshbrntt
jshbrntt

Reputation: 5394

How can I bundle ffmpeg in an Electron application

I'm building an Electron application starting from the electron-webpack boilerplate.

I found this node module @ffmpeg-installer/ffmpeg which installs a compatible precompiled binary into the /node_modules directory then makes the path of that executable accessible through.

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

This works fine during development, but when I build the distributable and run it I get an error when attempting to spawn a child process with that path. Presumably, because the path does not point at the binary.

The path is set to the following when running the distributable.

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

However, when looking in the AppName.app package contents I find the binary in the following path.

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

How should I include binary dependencies in an Electron application using electron-webpack and electron-builder?

Upvotes: 9

Views: 10530

Answers (3)

David
David

Reputation: 682

MacOS:

  1. Find the executable file for ffmpeg
  2. Copy the executable file to root directory
  3. Add ffmpeg to package.json file
  4. Create and set path to ffmpeg in js file

package.json:

{
"build": {
    "asarUnpack": [
      "ffmpeg"
    ]
}

script.js:

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

const ffmpegPath = path.resolve(process.resourcesPath, "app.asar.unpacked/ffmpeg");

ffmpeg.setFfmpegPath(ffmpegPath);

Upvotes: -1

Yigal
Yigal

Reputation: 324

From here:

Install: npm i ffmpeg-static ffprobe-static

Include in your package.json:

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

Set path in your 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);

Upvotes: 6

Linus Thiel
Linus Thiel

Reputation: 39223

It's likely because electron will bundle the app in an asar archive (something like a zip/tar/jar). Hence, the path to the executable can't be resolved. Try passing asar: false to electron-builder (in electron-builder.json).

Upvotes: 2

Related Questions