1

I wanna merge two videos into one showing one on the left side and another on the right, not concatenation. Below code works almost fine, but both two audio lost. how can i do this without losing sound?

ffmpeg()
  .input('video1.mp4')
  .input('video2.mp4')
  .videoCodec('libx264')
  .complexFilter([
    '[0:v]scale=400:300[0scaled]',
    '[1:v]scale=400:300[1scaled]',
    '[0scaled]pad=800:300[0padded]',
    '[0padded][1scaled]overlay=shortest=1:x=400[output]',
  ])
  .outputOptions(['-map [output]'])
  .output('output.mp4')
  .on('error', function (er) {
    console.log('error occured: ' + er.message);
  })
  .on('end', function () {
    console.log('success');
  })
  .run();
yabbee
  • 11
  • 1
  • 3
  • Look up how to do this with just regular ffmpeg (dozens if not hundreds of ffmpeg tutorials/superuser posts/etc out there, plenty about how to combine two video files), and then turn that command line instruction into the appropriate function calls? – Mike 'Pomax' Kamermans Mar 12 '22 at 18:42
  • I don't know node.js, but it looks like you have to map the audio `.outputOptions(['-map [output] -map 0'])` for mapping the audio from `video1.mp4` or `.outputOptions(['-map [output] -map 1'])` for the audio from `video2.mp4` – Rotem Mar 12 '22 at 21:22
  • Add to @Rotem's answer, if you wish to keep both audio, add `amix` filter to your filtergraph (as a new independent single-filter chain) and map its output. – kesh Mar 13 '22 at 00:11
  • @kesh Thanks bro! Solved it. Adding amix filter works fine. It keeps both sound without losing. – yabbee Mar 14 '22 at 02:17

1 Answers1

0

Based on @kesh's comment, adding amix filter solved it.Here is my code.

ffmpeg()
  .input('video1.mp4')
  .input('video2.mp4')
  .videoCodec('libx264')
  .complexFilter([
    '[0:v]scale=400:300[0scaled]',
    '[1:v]scale=400:300[1scaled]',
    '[0scaled]pad=800:300[0padded]',
    '[0padded][1scaled]overlay=shortest=1:x=400[output]',
    'amix=inputs=2:duration=shortest',  ///////// here
  ]).outputOptions(['-map [output]'])
  .output('output.mp4')
  .on('error', function (er) {
    console.log('error occured: ' + er.message);
  }).on('end', function () {
    console.log('success');
  }).run();

You don't have to write :duration=shortest if you don't need it. This command is used for determining mixed video duration.

Aqib Javed
  • 935
  • 1
  • 5
  • 15
yabbee
  • 11
  • 1
  • 3