Put Video on top of another video using fluent-ffmpeg

142 views Asked by At

I have two videos.

  1. Tutorial
  2. Person in the circle moving hands

i want to put second video on top of the first one, ideally in the corner.

enter image description here

1

There are 1 answers

0
Iman Mohammadi On

To overlay a video on top of another video using fluent-ffmpeg, you need to use ffmpeg's overlay filter.

  1. First, install fluent-ffmpeg:

    npm install fluent-ffmpeg
    
  2. If you don't have ffmpeg and ffprobe installed on your machine, make sure you do, or you can set their paths using fluent-ffmpeg.

  3. Use the following code to overlay the second video (person_in_circle.mp4) on top of the first video (tutorial.mp4):

const ffmpeg = require('fluent-ffmpeg');

const inputMainVideo = 'tutorial.mp4';
const inputOverlayVideo = 'person_in_circle.mp4';

const outputVideo = 'output.mp4';

const overlayXPosition = "main_w-overlay_w";
const overlayYPosition = "0";

ffmpeg()
  .input(inputMainVideo)
  .input(inputOverlayVideo)
  .complexFilter([
    {
      filter: 'overlay',
      options: {
        x: overlayXPosition,
        y: overlayYPosition
      },
      inputs: ['0:v', '1:v'],
      outputs: 'result'
    }
  ], 'result')
  .on('end', function() {
    console.log('Finished processing');
  })
  .on('error', function(err) {
    console.error('Error:', err);
  })
  .save(outputVideo);

Ensure the overlay video is smaller in dimension than the main video if you want it to be fully visible. If it's larger, you might need to resize it first using ffmpeg.