Convert MP4 Video to GIF Animation in Node.js
This tutorial will show you how to convert an MP4 video into a GIF animation in Node.js. We will use the fluent-ffmpeg library for this purpose. In this library, the complex command line usage of ffmpeg is abstracted into an easy-to-use Node.js module.
The fluent-ffmpeg is a wrapper library to ffmpeg library which is a video and audio processing library. This library uses ffmpeg library in the background. To use this library you should have ffmpeg installed on your device or system.
To install the library open the terminal and type there like below and execute your command.
npm i fluent-ffmpeg
First of all, we require some modules in our program.
var ffmpeg = require('ffmpeg'); var ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; var ffprobePath = require('@ffprobe-installer/ffprobe').path; var ffmpeg = require('fluent-ffmpeg');
In the above program, ffmpeg
provides us tools for processing audio and video.
Then the ffmpegPath
will get the path for us. The path will be either available locally or we need to require @ffmpeg-installer/ffmpeg
library which will automatically install a binary of ffmpeg and will get the path for us.
Similarly, the ffprobePath
will provide us the path. This is also a ffmpeg
required package.
After that, the fluent-ffmpeg
to generate and execute the ffmpeg
commands for us.
ffmpeg.setFfmpegPath(ffmpegPath); ffmpeg.setFfprobePath(ffprobePath);
In the above program, I set the FfmpegPath
and passed the ffmpegPath
which I declared as a variable in the earlier step.
Similarly, I set the FfprobePath
and passed the ffprobePath
which I declared as a variable in the earlier step.
ffmpeg("video.mp4") .setStartTime('00:00:04') .setDuration('5') .size("1280x720") .fps(40) .output( "output.gif")
First of all, ffmpeg("video.mp4")
to get the video that we want to convert into GIF. The video should be present in the root directory.
.setStartTime('00:00:04')
to set the starting time from which time we want to convert the video into GIF. Here I have started 4th second of the video.
.setDuration('5')
to set the duration of the GIF. Here I have passed 5 inside in sec. So the duration of our GIF will be 5 seconds.
.size("1280x720")
to set the quality of the GIF. Here the quality of the GIf will be HD (720p).
.fps(40)
to set the frame rate per second or to control the speed of the GIF animation.
Then, .output( "output.gif")
is the path where the output file will be created.
.on('end', function(err) { if(!err) { console.log('CONVERTED') } })
The .on("end")
event will execute when the process is over. If there is no error it will print CONVERTED.
.on('error', function(err){ console.log(err) }) .run()
If there is any sort of error takes place then .on("error")
event will execute and display the error which is taken place.
.run()
to start the process. The process won’t start without it.
Here’s the complete code below.
var ffmpeg = require('ffmpeg'); var ffmpegPath = require('@ffmpeg-installer/ffmpeg').path; var ffprobePath = require('@ffprobe-installer/ffprobe').path; var ffmpeg = require('fluent-ffmpeg'); ffmpeg.setFfmpegPath(ffmpegPath); ffmpeg.setFfprobePath(ffprobePath); ffmpeg("video.mp4") .setStartTime('00:00:01') .setDuration('5') .size("1280x720") .fps(40) .output( "output.gif") .on('end', function(err) { if(!err) { console.log('CONVERTED') } }) .on('error', function(err){ console.log(err) }) .run()
To run the above program simply open the terminal and type node then your file name and hit enter like below.
PS G:\saM> node index.js
If you run the above program, it will create an output.gif in your root directory which is a GIF animation.
Leave a Reply