8

I saw that FFmpeg can merge/blend two videos with alpha channel and can encode video with alpha channel using the qtrle codec, but, It is possible to apply a PNG file mask, either a black and white or a PNG with an alpha channel to a video in order to have a video with the alpha channel of the PNG applied?

Here is the concept in images

Original video: Original video

PNG file with alpha channel, the "mask": PNG file with alpha channel

And the result: Result

Or even a black and white image as a mask for alpha channel: bnw image

Any help would be appreciated, thank you!

Rodrigo Polo
  • 4,314
  • 2
  • 26
  • 32

1 Answers1

7

Use the alphaextract and alphamerge filters:

ffmpeg -i video -vf "movie='image',alphaextract[a];[in][a]alphamerge" -c:v qtrle output.mov
Gyan
  • 85,394
  • 9
  • 169
  • 201
  • 1
    Thank you, it worked for one frame, but your settings give me the understanding, so here is the command that worked: `ffmpeg -y -i input.mp4 -loop 1 -i mask_with_alpha.png -filter_complex "[1:v]alphaextract[alf];[0:v][alf]alphamerge" -c:v qtrle -an output.mov` – Rodrigo Polo Apr 07 '16 at 07:12
  • Hi. I asked a similar question here: http://stackoverflow.com/questions/38951088/writing-alpha-channel-into-decoded-ffmpeg-frame What is the programmatic way to do this ? – Mic Aug 15 '16 at 11:55
  • 1
    Did you use the mask file or the black and white file , I couldnt quite get the output you have got – Jeffin Jul 08 '17 at 12:54
  • 1
    I was attempting to mask off an already-transparant video, and it required blending together alpha channels. Adding another `alphaextract` filter and setting a `blend` filter to `darken` did the trick: `-filter_complex "[1:v]alphaextract[alf];[0:v]alphaextract[oalf];[alf][oalf]blend=all_mode=darken[res];[0:v][res]alphamerge"` Doing this removes the need for `-loop 1` on the input. – Tyzoid Jun 19 '20 at 22:42
  • How to do the same just for single image? – pete Jun 04 '23 at 04:31