Welcome folks today in this blog post we will be making the video
from series of images with audio using the videoshow
library in node.js using the ffmpeg library. All the full source code of the application is shown below.
Get Started
In order to get started you need to initialize a new node.js project using the below command as shown below
npm init -y
npm i videoshow
After that you need to make an index.js
file and copy paste the following code
index.js
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
var videoshow = require('videoshow') var images = [ 'step1.jpg', 'step2.jpg', 'step3.jpg', 'step4.jpg' ] var videoOptions = { fps: 25, loop: 5, // seconds transition: true, transitionDuration: 1, // seconds videoBitrate: 1024, videoCodec: 'libx264', size: '640x?', audioBitrate: '128k', audioChannels: 2, format: 'mp4', pixelFormat: 'yuv420p' } videoshow(images, videoOptions) .audio('song.mp3') .save('video.mp4') .on('start', function (command) { console.log('ffmpeg process started:', command) }) .on('error', function (err, stdout, stderr) { console.error('Error:', err) console.error('ffmpeg stderr:', stderr) }) .on('end', function (output) { console.error('Video created in:', output) }) |
As you can see we are importing the videoshow
library and then we are calling the videoshow()
method passing the series of images that we have declared inside the array. These images paths you need to provide in the array and then these images will be converted into slideshow video. And also we are providing the videooptions
as the second argument which contain the different config options such as the format,pixeltype
, fps and bitrate of the video. And also we are attaching the audio
to the video using the audio()
method. Inside this method you need to provide the path of the audio file
which is song.mp3
. And then we are exporting the video using the save()
method and inside it we are providing the output file name which is output.mp4
. And lastly we have the different events we can listen on when the process starts we have the start event and also we have the error event if any error takes place. And when the process finishes we have the end
event also.
You can even create a config.json
file and inside it you can paste the below json code to export the images with audio to video as shown below
config.json
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
{ "output": "video.mp4", "options": { "fps": 25, "loop": 5, "transition": true, "transitionDuration": 1, "videoBitrate": 1024, "videoCodec": "libx264", "size": "640x?", "audioBitrate": "128k", "audioChannels": 2, "format": "mp4", "subtitleStyles": { "Fontname": "Verdana", "Fontsize": "26", "PrimaryColour": "11861244", "SecondaryColour": "11861244", "TertiaryColour": "11861244", "BackColour": "-2147483640", "Bold": "2", "Italic": "0", "BorderStyle": "2", "Outline": "2", "Shadow": "3", "Alignment": "1", "MarginL": "40", "MarginR": "60", "MarginV": "40" } }, "images": ["./images/1.png", "./images/2.png", "./images/3.png"] } |
videoshow -c config.json --audio song.mp3 --logo logo.png video.mp4
As you can see in the above command we are adding the audio using the --audio
flag and also we are adding the logo to the video using the --logo
flag. And lastly we get the output.mp4 video file