Converting videos directly with NodeJS is now possible!
NicolasBrondinBernard
Do you know our lord FFMPEG?

Article published on 02/12/2020, last updated on 10/08/2026
I can already hear some of you saying that anything is possible as soon as you create a new system process from our NodeJS environment thanks to a command like spawn() for example.
If you don't know the spawn() and exec() methods of the NodeJS child_process module, I invite you to read my article explaining how they work!
Except that's not what this is about. I'm not talking about calling the executable of the FFMPEG library installed on the system, no, I really am talking about running FFMPEG within NodeJS!
But how is that possible? Simply thanks to a technology I've already presented on the blog: WebAssembly.
To put it briefly, WebAssembly allows you to compile low-level code into executable bytecode (WASM) run by the Javascript execution engine.
Converting a video with NodeJS and FFMPEG
I want to point out that the following example also works on some browsers that support "SharedArrayBuffer", which currently means Google Chrome.
The first step is to import the FFMPEG module into your node project by running the following command:
# Use npm
$ npm install @ffmpeg/ffmpeg @ffmpeg/core
From that point on, you'll be able to easily convert your video files by harnessing FFMPEG's incredible power, simply from your NodeJS script:
const fs = require('fs');
const { createFFmpeg, fetchFile } = require('@ffmpeg/ffmpeg');
const ffmpeg = createFFmpeg({ log: true });
(async () => {
await ffmpeg.load();
ffmpeg.FS('writeFile', 'example.avi', await fetchFile('./example.avi'));
await ffmpeg.run('-i', 'example.avi', 'example.mp4');
await fs.promises.writeFile('./example.mp4', ffmpeg.FS('readFile', 'example.mp4'));
process.exit(0);
})();
Personally, I use the latest version of NodeJS (15.3.0) and for the script to work I have to add the following flags: "--experimental-wasm-threads --experimental-wasm-bulk-memory"
Be careful, the flags must be added before the name of the script to be run, like this:
$ node --experimental-wasm-threads --experimental-wasm-bulk-memory index.js
And there you have it, in a few seconds (depending on the length of your video), you'll be able to get your brand new example.mp4 file! If you want to use a test video file in .avi format, you can find one right here: https://www.engr.colostate.edu/me/facil/dynamics/avis.htm
To learn more and discover everything you can do with the WASM version of FFMPEG, I invite you to check out the documentation on Github:
_
No spam. Only free content, news, and ever more resources to level up your skills!
Join +1500 developers
No comments yet