5

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?

2 Answers 2

3

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);
2

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).

3
  • This works but I would like to know why the ASAR did not work. Is it because of the default asarUnpack option?
    – jshbrntt
    Dec 16, 2017 at 20:21
  • ⚠️ Packaging using asar archive is disabled — it is strongly not recommended. Please enable asar and use asarUnpack to unpack files that must be externally available.
    – jshbrntt
    Dec 16, 2017 at 20:31
  • @JoshuaBarnett it's been a while since I encountered this - it seems like the asarUnpack/smartUnpack options are new to me. If you can get it to work, this seems like the better option - I was reluctant to use asar: false myself. Dec 17, 2017 at 16:04

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.