alphamerge, alphaextract

Watch on youtube.com
doc

https://ffmpeg.org/ffmpeg-filters.html#alphamerge, https://ffmpeg.org/ffmpeg-filters.html#alphaextract

see also

maskedmerge

When two input streams do not have alpha, “overlay” literally makes only the top layer visible for overlapping regions:

00:00:00
#! /bin/sh
f1="Pexels_2877_2880.mp4"
f2="Pexels_2880_2877.mp4"

ffmpeg -y -i "${f1}" -i "${f2}" -filter_complex "
[0:v][1:v]overlay[v]
" -map '[v]' -an out.mp4

If your intention is “overlaying the transparent image”, you can use “blend” or “mix” (in recent ffmpeg), but you can also use “alphamerge” instead.

00:00:24
#! /bin/sh
f1="Pexels_2877_2880.mp4"
f2="Pexels_2880_2877.mp4"

ffmpeg -y -i "${f1}" -i "${f2}" -filter_complex "
color=0xAAAAAA:s=1920x1080:d=24[alpha];
[1:v][alpha]alphamerge[1v];
[0:v][1v]overlay[v]
" -map '[v]' -an out.mp4

“alphamerge” regards input stream 2 as a grayscale image and uses it as an alpha plane. This behavior may be difficult to handle in some cases. That is, it’s the case which you wish to use it as it is when the input stream originally has an alpha plane. In this case, it is good to use in combination with “alphaextract”. However, pay attention to “format”. Some do not support alpha plane.

00:00:48
#! /bin/sh
f1="Pexels_2877_2880.mp4"
f2="Pexels_2880_2877.mp4"

ffmpeg -y -i "${f1}" -i "${f2}" -filter_complex "
color=black@0.5:s=1920x1080:d=24
,format=rgba
,alphaextract[alpha];

[1:v][alpha]alphamerge[1v];
[0:v][1v]overlay[v]
" -map '[v]' -an out.mp4

Of course, since the examples up to the previous are examples which you can obediently use “blend” or “mix”, so it is not worth. It would be something like this that “alphamerge” demonstrates its true value. In this case, “alpha” is gradated according to the distance from the image center using “geq”.

00:01:12
#! /bin/sh
f1="Pexels_2877_2880.mp4"
f2="Pexels_2880_2877.mp4"

ffmpeg -y -i "${f1}" -i "${f2}" -filter_complex "
color=black:s=1920x1080:d=24,
geq='lum=mod(sqrt((W/2-X)^2+(H/2-Y)^2)*3,256)'
[alpha];

[1:v][alpha]alphamerge[1v];
[0:v][1v]overlay[v]
" -map '[v]' -an out.mp4

Naturally, it will be like this if you use moving images as alpha. In this example I used “testsrc2”.

00:01:36
#! /bin/sh
f1="Pexels_2877_2880.mp4"
f2="Pexels_2880_2877.mp4"

ffmpeg -y -i "${f1}" -i "${f2}" -filter_complex "
testsrc2=s=1920x1080:d=24[alpha];

[1:v][alpha]alphamerge[1v];
[0:v][1v]overlay[v]
" -map '[v]' -an out.mp4