1

I am making an app where I need to overlay two video files one above the other using ffmpeg. I tried various commands but all it does is merging a video.

llogan
  • 121,796
  • 28
  • 232
  • 243
Ria Mehta
  • 23
  • 1
  • 7

1 Answers1

7

Basic method is

ffmpeg -i in1 -i in2 -filter_complex
       "[1]format=yuva444p,colorchannelmixer=aa=0.5[in2];[0][in2]overlay"
       out

where 0.5 sets 50% transparency for the 2nd input. The format filter is needed to make sure that the 2nd video has an alpha channel.

To resize in2 to match in1, use

ffmpeg -i in1 -i in2 -filter_complex
       "[1]format=yuva444p,colorchannelmixer=aa=0.5[in2];
        [in2][0]scale2ref[in2][in1];[in1][in2]overlay"
       out
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • thanks @Gyan that's working! The video is overlay-ed, but the overlay-ed video width and height is too small. So how can i set width and height of both videos equal ? – Ria Mehta Oct 02 '18 at 08:11
  • have can you help me? how can it work fast? @Gyan – Ria Mehta Oct 02 '18 at 10:54
  • You can speed it up slightly by doing a manual scale. You may be able to speed up encoding by adding `-preset veryfast` but that depends on which encoder you're using. – Gyan Oct 02 '18 at 11:39
  • Instead of `colorchannelmixer` you could use a lighter filter like [lut](https://ffmpeg.org/ffmpeg-all.html#lut_002c-lutrgb_002c-lutyuv) and set the alpha channel to 255*{alpha percentage} like so: `lut=a=255*0.5` – Ștefan-Gabriel Muscalu Sep 09 '20 at 17:12
  • 1
    Changing the format to YUV all the time can be very taxing, especially if the input format is RGB, so you could let ffmpeg choose the best format with alpha to use out of a provided list, like so `format=yuva420p|yuva444p|yuva422p|rgba|abgr|bgra|gbrap|ya8`; here we ask ffmpeg to choose best format between all variants of yuv, rgb and/or grayscale. To see all supported pixel formats, use `ffmpeg -pix_fmts` – Ștefan-Gabriel Muscalu Sep 09 '20 at 17:15