6

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?

jshbrntt
  • 5,134
  • 6
  • 31
  • 60

2 Answers2

6

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);
Yigal
  • 185
  • 3
  • 7
  • I have never been able to get this to work. For one thing, the directory structure he has listed is wrong. ffmpeg-static only downloads the one needed for the specific build so there aren't separate folders. But even then it doesn't work and nothing ends up in the app.asar.unpacked location. – deusprogrammer Jul 27 '23 at 20:31
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).

Linus Thiel
  • 38,647
  • 9
  • 109
  • 104
  • 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 '17 at 20:21
  • 2
    `⚠️ 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 '17 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. – Linus Thiel Dec 17 '17 at 16:04