1

I have 3 inputs:

  1. Video file: https://file.io/6jtUlnZ7TGmT
  2. Animated GIF with transparent background https://i.stack.imgur.com/nmJpw.gif
  3. Mask file (BW) the same resolution as the video https://i.stack.imgur.com/E1MCY.png

I want to put the image on the video, but throw out all GIF pixels that are transparent in the mask (replace them with transparent ones).

Video
Mask
Image
Result

My current command, without masking (it works fine):

ffmpeg -i input.mp4 -ignore_loop 0 -i animation.gif \
-filter_complex "[1]scale=700x700[scaled_gif];\
[0][scaled_gif]overlay=50:30:shortest=1" \
-codec:a copy output.mp4

UPDATE #1

I managed to get closer to my goal with this command:

ffmpeg -y -i input.mp4 -loop 1 -i mask.png \ 
-filter_complex "[1:v]alphaextract[mask]; \
movie=animation.gif,scale=1920x1080[scaled_gif]; \
[scaled_gif][mask]alphamerge[masked]; \
[0:v][masked]overlay=0:0" \
-c:a copy output_masked.mp4

but it has 2 problems:

  1. GIF loses it's transparency. Transparent pixels become white.
  2. GIF plays only once (i.e. no loop)

Output: https://file.re/2021/03/11/outputmasked/

zeromodule
  • 43
  • 6
  • If you make your mask black and white instead of black and transparent it will result in a simpler ffmpeg command. – llogan Mar 11 '21 at 17:24
  • @llogan thank you, you're right! BW mask is better. I've done with masking, but now GIF loses it's transparency (see UPDATE #1 above) – zeromodule Mar 11 '21 at 18:20
  • Can you provide a link to your GIF? – llogan Mar 11 '21 at 18:22
  • @llogan sure, here two examples: https://i.imgur.com/Vo3KHZm.gif and https://i.imgur.com/btgHQPm.gif – zeromodule Mar 11 '21 at 18:26
  • @llogan this is an output I'm getting now with the second GIF file https://file.re/2021/03/11/outputmasked/, the alpha channel of the GIF turns white color. – zeromodule Mar 11 '21 at 19:03

0 Answers0