FFmpeg + Node.js

2 min read

You need to get these packages to use FFmpeg.

yarn add fluent-ffmpeg @ffmpeg-installer/ffmpeg @ffprobe-installer/ffprobe

Example ffmpeg.js file. No more settings need. Require it and use.

const ffmpeg = require('fluent-ffmpeg') const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; const ffprobePath = require('@ffprobe-installer/ffprobe').path; ffmpeg.setFfmpegPath(ffmpegPath); ffmpeg.setFfprobePath(ffprobePath); module.exports = ffmpeg;

Usage

Now you can use FFmpeg API.

const ffmpeg = require('./ffmpeg'); ffmpeg() .input('voice-message.ogg') .save('voice-message.wav');

Electron app

If you want to package binaries to Electron app compressed with asar, update path strings.

const ffmpeg = require('fluent-ffmpeg') const ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; const ffprobePath = require('@ffprobe-installer/ffprobe').path; ffmpeg.setFfmpegPath(ffmpegPath.replace('app.asar', 'app.asar.unpacked')); ffmpeg.setFfprobePath(ffprobePath.replace('app.asar', 'app.asar.unpacked')); module.exports = ffmpeg;

For bundled app paths to binaries will be updated. While app is in development mode (not bundled) FFmpeg will be requested from node_modules directory directly so replace function will do nothing.