We have built our video transcoding pipeline in NodeJS using the fluent-ffmpeg node module, what kind of parameters do I have to pass for merging 2 or more videos into a single mp4 file with fluent-ffmpeg?
Using Node.js’ fluent-ffmpeg
package, combining several videos into a single video file is a multi-step procedure. Here is an in-depth explanation of how to complete this task:
1. Set up and bring in fluent-ffmpeg
Verify that your Node.js project has the fluent-ffmpeg module installed if not then use the command given below:
npm install fluent-ffmpeg
Now, import it into your Node.js script, use:
const ffmpeg = require('fluent-ffmpeg');
2. Specify the File Output
Choose the output file to be used as the destination for the combined video. The file path and output format (such as MP4) are configurable. For instance:
const outputFilePath = 'merged_video.mp4';
3. Establish a command for FFmpeg
Using the ffmpeg() function, create an FFmpeg command, set the output file, and provide encoding options. Here’s one instance:
ffmpeg()
.output(outputFilePath)
.videoCodec('libx264') // Video codec (e.g., H.264)
.audioCodec('aac') // Audio codec (e.g., AAC)
.on('end', () => {
console.log('Merging complete!');
})
.on('error', (err) => {
console.error('Error:', err);
});
We’ve selected the AAC audio codec and H.264 video codec for this example. These choices can be altered to suit your needs.
4. Include Videos
To upload the video files you wish to combine, use the input method. To add more than one input file, you can chain multiple input calls together. For example:
ffmpeg()
.input('video1.mp4')
.input('video2.mp4')
// Add more input videos as needed
.output(outputFilePath)
.videoCodec('libx264')
.audioCodec('aac')
.on('end', () => {
console.log('Merging complete!');
})
.on('error', (err) => {
console.error('Error:', err);
});
5. Configure Each Input’s Options
You can adjust choices for each input separately if your input videos have varied qualities or if you wish to apply different settings to each. To set different input options, use the inputOptions
method. For instance:
ffmpeg()
.input('video1.mp4')
.inputOptions('-vf scale=1280:720') // Resize the first input to 1280x720
.input('video2.mp4')
.inputOptions('-ss 00:00:10') // Set the start time for the second input
// Add more input videos and options as needed
.output(outputFilePath)
.videoCodec('libx264')
.audioCodec('aac')
.on('end', () => {
console.log('Merging complete!');
})
.on('error', (err) => {
console.error('Error:', err);
});
6. Configure Detailed Filters and Extra Settings
You can alter the videos before combining them by using complex filters or other options if you require more sophisticated editing. For this, make use of the addOption
and complexFilter functions.
ffmpeg()
.input('video1.mp4')
.complexFilter(['[0:v]scale=1280:720[v0]'])
.input('video2.mp4')
.inputOptions('-ss 00:00:10')
.complexFilter(['[1:v]scale=1280:720[v1]'])
.output(outputFilePath)
.videoCodec('libx264')
.audioCodec('aac')
.on('end', () => {
console.log('Merging complete!');
})
.on('error', (err) => {
console.error('Error:', err);
});
7. Combining the Videos
To start the merging operation, call the mergeToFile function
. This will initiate the merge and launch the FFmpeg command.
ffmpeg()
.input('video1.mp4')
.input('video2.mp4')
.output(outputFilePath)
.videoCodec('libx264')
.audioCodec('aac')
.on('end', () => {
console.log('Merging complete!');
})
.on('error', (err) => {
console.error('Error:', err);
})
.mergeToFile(outputFilePath);
8. Taking Care of Events
When the merging procedure is finished, keep an ear out for the ‘end’ event. To address any errors that might arise during the merging process, you can additionally listen for the ‘error’ event.
9. Carrying out the Script
Run your Node.js script, that’s all. When the operation is finished, the combined video will be in the output file (outputFilePath) that was supplied.
Using the fluent-ffmpeg
package in Node.js, you may combine several videos into a single video file by following these instructions. Adapt the input videos and settings to your unique requirements for video editing and use cases.
You can also follow this small tutorial:
Step 1: To combine movies using the concat demuxer, first compile a list of the videos you wish to combine into a text file. Here is a video.txt example file. You can add comments to the file, but the demuxer will not use them.
Eg. # videos.txt
file 'input1.mp4'
file 'input2.mp4'
file 'input3.mp4
Step 2: Once that is finished, you can execute the following command to merge the movies mentioned in that file in the order they are listed.
$ ffmpeg -f concat -i videos.txt -c copy output8.mp4