1

so what the title says. I've never used ffmpeg before but I really need to extract an alpha mask for a project. The problem is a grey color which should be white so when I merge it with the main video there is transparency where there shouldn't be.

I used the command

ffmpeg -i angry.mov -filter:v alphaextract mask.mov

and

ffmpeg -i angry.mov -vf alphaextract,format=yuv420p 123.mov 

is there any other way to do it? I did the original mov file from png images.

Thanks

1 Answer 1

1

We may use lutyuv filter, but it may be too complicated if your never used FFmpeg before.

Example for converting all gray values in the alpha channel to white:

ffmpeg -i angry.mov -vf alphaextract,lutyuv="y=(val-(maxval-1))*maxval" -pix_fmt gray 123.mov


Example:
Input image: peppers_alpha.png:
enter image description here
As you can see there are semi-transparent pixels.
(You may download the above image for executing the code samples below).


Extracting alpha channel using alphaextract filter:

ffmpeg -y -i peppers_alpha.png -vf alphaextract -pix_fmt gray alpha.png

Result:
enter image description here
As you can see the semi-transparent pixels turned to gray levels (and the opaque pixels are zeros).


Turning the semi-transparent pixels to black pixels using alphaextract and lutyuv filters:

ffmpeg -y -i peppers_alpha.png -vf alphaextract,lutyuv="y=(val-(maxval-1))*maxval" -pix_fmt gray new_alpha.png

Result:
enter image description here
As you can see, the semi-transparent pixels turned to black.

Note: maxval = 255 for 8 bits images.


We may adjust adjust the threshold by subtracting 50 instead of 1 (for example):

ffmpeg -y -i peppers_alpha.png -vf alphaextract,lutyuv="y=(val-(255-50))*255" -pix_fmt gray new_alpha.png

Result:
enter image description here


Why y=(val-254)*255 sets pixels in range [0, 254] to 0?

Explanation:
All the mathematical operations are applied with clipping to [0, 255].
Values below 0 are clipped to 0 and values above 255 are clipped to 255.

val-254 is 0 (after clipping) for all values except 255 (for values in range [0, 254]).

So for val in [0, 254] the result is:

0*255 = 0

And for val = 255 the result is:

(255-254)*255 = 255

1
  • Just created an account to thank you, I did use (255-254)*255 and now it looks as it should. Thanks again Commented Jul 6, 2022 at 0:22

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .