FFmpeg & Black and White Conversion - ffmpeg

How to convert a video to black and white using ffmpeg?

Desaturate
Use the hue filter if you want to desaturate:
ffmpeg -i input -vf hue=s=0 output
This is like using Colors → Saturation in the GIMP.
Grayscale
Use the format filter if you want to convert to grayscale format:
ffmpeg -i input -vf format=gray output
This is like using Image → Mode → Grayscale in the GIMP.
Threshold
See FFmpeg convert video to Black & White with threshold?
This is like using Colors → Threshold in the GIMP.

The monochrome filter can do the trick:
ffmpeg -i input.mp4 -vf monochrome output.mp4

Related

ffmpeg: pad filter with transparent background for GIF input/output?

I'm trying to pad the GIF source to a square thumbnail and the extended area should be transparent.
(image source: https://store.steampowered.com/app/2113680/The_Dot/)
To achieve the goal, I used the following command:
ffmpeg -i ./test/source.gif -filter_complex "[0]scale=255:144[s1];[s1]pad=256:256:0:-56:blue[s2];[s2]split[s3][s4];[s3]palettegen=reserve_transparent=on:transparency_color=blue[p];[s4][p]paletteuse[s5]" -map [s5] -loop 0 ./test/output.gif -y
[0]scale=255:144[s1] First I scale down the image.
[s1]pad=256:256:0:-56:blue[s2] Then pad the image with an uncommon background color: blue
[s2]split[s3][s4] And I split to two streams. One for palettegen and another for paletteuse.
[s3]palettegen=reserve_transparent=on:transparency_color=blue[p] Generate the color palette that indicates blue as the transparency_color
[s4][p]paletteuse[s5] Use the new color palette for output. The padded image with blue background color should be treated as a transparent background.
But the output animated GIF still has a visible blue background color no matter if I put the paletteuse at first or in the end.
Related posts:
Making GIFs with ffmpeg: what does "transparency_color" do?
How do I resize an animated GIF and keep transparency?
Transparency is gone while resizing (scaling) gif using ffmpeg
overlay on transparent background:
ffmpeg -i 74bRi.gif -filter_complex "
color=s=256x256:d=100,format=argb,colorchannelmixer=aa=0.0[b];
[0]scale=255:144[f];
[b][f]overlay=(W-w)/2:(H-h)/2:shortest=1,split[s1][s2];
[s1]palettegen[p];
[s2][p]paletteuse
" -loop 0 output.gif
pad using transparent color ffffff00
ffmpeg -i 74bRi.gif -filter_complex "
[0]scale=255:144,pad=256:256:0:-1:ffffff00,split[s1][s2];
[s1]palettegen[p];
[s2][p]paletteuse
" -loop 0 out.gif

ffmpeg hstack png and replace transparent color with white color

Below command works fine to stack two png file with alpha channel left and right, The output png file will keep the alpha channel as well.
ffmpeg -i a.png -i b.png -filter_complex hstack=inputs=2 output.png
I wish replace alpha channel with white color (no need alpha channel), which parameter should I use?
You can try this filter:
color=c=white[b];\
hstack=inputs=2[f];\
[b][f]scale2ref[b1][f1];\
[b1][f1]overlay
also set -pix_fmt rgb24 output option

FFmpeg - Crop image white space

I'm looking for a way, with FFmpeg, to crop an image removing all white pixels; something like the Photoshop's "crop white spaces".
I tried looking around but I didn't find a solution, here is an example of what I'm looking for:
Given this:
I would be able to obtain this:
How should I get this edit?
Use negate to invert the image colors and cropdetect to find crop parameters:
ffmpeg -loop 1 -i input.png -frames:v 3 -vf "negate,cropdetect=limit=0:round=0" -f null -
...
[Parsed_cropdetect_1 # 0x5581f7287580] x1:198 x2:1255 y1:472 y2:968 w:1058 h:496 x:198 y:472 pts:3 t:0.120000 crop=1056:496:200:472
Test with ffplay if desired:
ffplay -vf crop=1056:496:200:472 input.png
Then use crop:
ffmpeg -i input.png -vf crop=1056:496:200:472 output.png
See ffmpeg get value from cropdetect for a Bash shell example to extract the cropdetect value for scripted usage.

ffmpeg gif with color instead transparency [duplicate]

I'm converting a PNG to JPG. The transparent background turns black by default. I need it to be white.
What is the FFmpeg command to set the alpha channel to a color?
I think it has something to do with the alphamerge and alphaextract Filters.
ffmpeg -i image.png -qscale:v 2 image.jpg
This replaces white with transparency when converting to png:
-vf chromakey=white
You can use the geq filter.
ffmpeg -i in.png -vf format=yuva444p,geq='if(lte(alpha(X,Y),16),255,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))' out.jpg
If your alpha is a pure black and white image, change 16 to 1.

How to Convert PNG Transparent Background to JPG White Background?

I'm converting a PNG to JPG. The transparent background turns black by default. I need it to be white.
What is the FFmpeg command to set the alpha channel to a color?
I think it has something to do with the alphamerge and alphaextract Filters.
ffmpeg -i image.png -qscale:v 2 image.jpg
This replaces white with transparency when converting to png:
-vf chromakey=white
You can use the geq filter.
ffmpeg -i in.png -vf format=yuva444p,geq='if(lte(alpha(X,Y),16),255,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))':'if(lte(alpha(X,Y),16),128,p(X,Y))' out.jpg
If your alpha is a pure black and white image, change 16 to 1.

Resources