Search code examples
videoffmpegoverlayalpha-transparencyffmpeg-php

overlay transparency video on top of other video


I have two videos. I used below command to overlay first video(overlay.mp4) on top of second video(main.mp4) and set transparency for alpha chanel to 0.3

ffmpeg -y \
-i main.mp4 \
-i overlay.mp4 \
-filter_complex \
[1]format=yuva420p,colorchannelmixer=aa=0.3,setpts=PTS+8/TB[1d]; \
[0][1d]overlay=enable='between(t,8, 13)'[v1]; \
-map [v1] -map 0:a -c:a copy -c:v libx264 -preset ultrafast output.mp4

The result looks like this. overlay

The background of first video is still remaining (look darker than the main video background).

I want to overlay only the 'foreground' on top of second video. How to set transparency of overlay video background so only the foreground display?

Edit

Set the colorkey option and it works

ffmpeg -y \
-i main.mp4 \
-i overlay.mp4 \
-filter_complex \
[1]format=rgb24,colorkey=black:0.3:0.2,colorchannelmixer=aa=0.3,setpts=PTS+8/TB[1d]; \
[0][1d]overlay=enable='between(t,8, 13)'[v1]; \
-map [v1] -map 0:a -c:a copy -c:v libx264 -preset ultrafast output.mp4

Solution

  • You'll need to use a keying filter to remove the background color

    ffmpeg -y \
    -i main.mp4 \
    -i overlay.mp4 \
    -filter_complex \
    [1]format=rgb24,colorkey=black,colorchannelmixer=aa=0.3,setpts=PTS+8/TB[1d]; \
    [0][1d]overlay=enable='between(t,8, 13)'[v1]; \
    -map [v1] -map 0:a -c:a copy -c:v libx264 -preset ultrafast output.mp4