How To Merge Two Videos Using Node.Js

In this article, we will learn how to Merge two videos using Node.js.

Step 1: Create your project using the following command.

npm init -y

Now, we have to create an index.js file for our merging code.

Step 2:  First, we need to install a fluent-ffmpeg, @ffmpeg-installer/ffmpeg, fs, and path in our application to create a merged video, You can install it using the below command

npm install fluent-ffmpeg
npm install @ffmpeg-installer/ffmpeg
npm install fs
npm install path

Step 3: First of all, we have created one function for converting a .mp4 file to a .ts file.

Why convert to .ts file and not directly .mp4 files to merge?

If you use fluent-ffmpeg node package to merge video then if you directly .mp4 file use then merged video is not a proper way to merge in the video some part blank or video length issue occurs or sometimes if you use the fluent-ffmpeg package to direct .mergeToFile() function code use than you can only two same length video merge, so first of all convert to .ts file to .mp4 file.

const mp4ToTs = () => {

  var cmd = new Promise((resolve, reject) => {
    ffmpeg('./one.mp4') //add your first video path
      .setFfmpegPath(ffmpegInstaller.path)
      .format('mp4')
      .on('end', () => {
        resolve()
        console.log('ts file created')
      })
      .on('error', err => {
        console.error(err);
      })
      .save('./file-one.ts');
  });
  var cmd2 = new Promise((resolve, reject) => {
    ffmpeg('./two.mp4') //add your secound video path
      .setFfmpegPath(ffmpegInstaller.path)
      .format('mp4')
      .on('end', () => {
        resolve()
        console.log('ts2 file created')
      })
      .on('error', err => {
        console.error(err);
      })
      .save('./file-two.ts');
  })

  return Promise.all([cmd, cmd2]);
}

Step 4: Now, we have merged the above created .ts files to a single .ts file.

This mergeSingleTsFile()  function required a ffmpeg file so first of all, download ffmpeg in your windows link is here

go to the link and find the release builds section, and click to ffmpeg-release-essentials.zip download zip file and compress, then change to folder name as ffmpeg, and put this ffmpeg folder in your project.

const mergeSingleTsFile = () => {
  return new Promise((resolve, reject) => {
    var one = "file-one.ts" //.ts file path
    var two = "file-two.ts" //.ts file path
    ffmpeg.setFfmpegPath(path.join(__dirname, './ffmpeg/bin/ffmpeg.exe')); 
    ffmpeg.setFfprobePath(path.join(__dirname, './ffmpeg/bin/ffprobe.exe'));
    ffmpeg({ source: one })
      .input(two)
      .on('end', () => {
        resolve()
        console.log("merge is done")
      })
      .on('error', (err) => console.log('Error', err))
      .mergeToFile("final.ts")

  })
}

Step 4: Now, we have to convert the above created .ts file to a .mp4 file.

If you try to above function to convert directly two .ts to .mp4 then video is not the proper way to merge so I am using this finalVideo() function to properly merge video occurs.

const finalVideo = () => {
  return new Promise((resolve, reject) => {

    function startDecode() {
      var infs = fs.createReadStream('final.ts');
      ffmpeg(infs)
        .save('output.mp4'); //add a path to save video
      console.log('Decoding....');
    }
    startDecode();
    resolve()
  })
}

Step 5: Now, we have to call the final function for the final merge video.

function startDecoding() {
  mp4ToTs().then(res => {
    console.log('mp4ToTs function run')
    mergeSingleTsFile().then(vid => {
      console.log('mergeSingleTsFile function run')
      finalVideo().then(com => {
        console.log('Final Video Processing...')
      })
    })
  })
}
startDecoding()

Now, run the application we are able to merge video.

Output:

I hope this article helps you and you will like it.

Please give your valuable feedback and if you have any questions or issues about this article, please let me know.

Submit a Comment

Your email address will not be published.

Subscribe

Select Categories