How to use node-ffmpeg library

Dilshod shoolimkhon
3 min readJun 27, 2023

--

To use the “node-ffmpeg” library, you need to install it using npm:

npm install ffmpeg

After installing the library, you can include it in your Node.js project using the require statement:

var ffmpeg = require('ffmpeg');

The library provides two ways to interact with FFmpeg: using the callback function or using promises.

Using the Callback Function

Here’s an example of using the library with the callback function:

try {
new ffmpeg('/path/to/your_movie.avi', function (err, video) {
if (!err) {
console.log('The video is ready to be processed');
} else {
console.log('Error: ' + err);
}
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}

Using Promises

Alternatively, you can use promises to work with the library:

try {
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
console.log('The video is ready to be processed');
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}

Once you have the video object, you can perform various operations on it. The video object provides access to video metadata, FFmpeg configuration, and various methods for conversions.

Example: Extracting Audio to MP3

Here’s an example of extracting the audio stream from a video file and saving it as an MP3 file:

try {
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
video.fnExtractSoundToMP3('/path/to/your_audio_file.mp3', function (error, file) {
if (!error) {
console.log('Audio file: ' + file);
}
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}

Example: Extracting Frames to JPG

Here’s an example of extracting frames from a video file and saving them as JPG images:

try {
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
video.fnExtractFrameToJPG('/path/to/save_your_frames', {
frame_rate: 1,
number: 5,
file_name: 'my_frame_%t_%s'
}, function (error, files) {
if (!error) {
console.log('Frames: ' + files);
}
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}

Example: Adding Watermark

Here’s an example of adding a watermark to a video file:

try {
var process = new ffmpeg('/path/to/your_movie.avi');
process.then(function (video) {
video.fnAddWatermark('/path/to/retrieve/watermark_file.png', '/path/to/save/your_file_video.mp4', {
position: 'SE'
}, function (error, file) {
if (!error) {
console.log('New video file: ' + file);
}
});
}, function (err) {
console.log('Error: ' + err);
});
} catch (e) {
console.log(e.code);
console.log(e.msg);
}

Custom Settings

The video object in the library provides various methods to set custom settings for video and audio encoding. Here are some examples of these methods:

  • video.setDisableAudio(): Disables audio encoding.
  • video.setDisableVideo(): Disables video encoding.
  • video.setVideoFormat(format): Sets the new video format.
  • video.setVideoCodec(codec): Sets the new video codec.
  • video.setVideoBitRate(bitrate): Sets the video bitrate.
  • video.setVideoFrameRate(framerate): Sets the framerate of the video.
  • video.setVideoStartTime(time): Sets the start time of the video.
  • video.setVideoDuration(duration): Sets the duration of the video.
  • video.setVideoAspectRatio(aspect): Sets the aspect ratio of the video.
  • video.setVideoSize(size, keepPixelAspectRatio, keepAspectRatio, paddingColor): Sets the size of the video.
  • video.setAudioCodec(codec): Sets the new audio codec.
  • video.setAudioFrequency(frequency): Sets the audio sample frequency.
  • video.setAudioChannels(channel): Sets the number of audio channels.
  • video.setAudioBitRate(bitrate): Sets the audio bitrate.
  • video.setAudioQuality(quality): Sets the audio quality.
  • video.setWatermark(watermarkPath, settings): Sets the watermark for the video.

These methods allow you to fine-tune the encoding settings according to your requirements.

Adding Custom Options

If the available functions do not cover your specific requirements, you can manually add custom options to the FFmpeg command using the addCommand function. Here's an example:

video.addCommand('-f', 'avi');

In this example, the output format of the video is set to AVI by adding the -f option with the value 'avi'.

Saving the Modified File

Once you have set the desired parameters and made the necessary modifications, you can save the modified file using the save function. This function takes the destination file path as input and optionally a callback function. If the callback function is not provided, you can use the promise object returned by save for handling the result. Here's an example:

video.save(destinationFileName, function (error, file) {
if (!error) {
console.log('Video file: ' + file);
}
});

In this example, the modified video file will be saved at the specified destinationFileName, and the callback function will be called with the resulting file path.

--

--

Dilshod shoolimkhon

I am a highly skilled Full Stack Developer with 3 years of experience.