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
:
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:
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:
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:
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