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.
Related
I've got an MKV that I would like to replace the first 5 seconds with a static png image that fades in/out from black. How can I accomplish this with just ffmpeg?
Easy method is to overlay the image:
ffmpeg -i input.mkv -loop 1 -t 5 -i image.png -filter_complex "[1]fade=type=in:duration=1,fade=type=out:duration=1:start_time=4[fg];[0]drawbox=t=fill:enable='lte(t,5)'[bg];[bg][fg]overlay=eof_action=pass:x=(W-w)/2:y=(H-h)/2" -c:a copy output.mkv
I added the drawbox filter to make a black background because I didn't know the size of your image.
See FFmpeg Filter Documentation.
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.
Im new in FFMPEG and I try to make just simple green background chroma keying of png images.
In FFMPEG documentation in 38.16 Chromakey - there is an example script:
ffmpeg -i input.png -vf chromakey=green out.png
for which is said that "Make every green pixel in the input image transparent"
That exactly what i wanted.
I tried with many different png images but the result is always a copy of input image.
Also i replaced the word "green" to 0x00FF00 or 0x008000 and still no successes. Only when replacing green to black and then the result is as expected - transparent.
Im working on Windows with pre-build binaries of FFMpeg.
What am I missing?
Thanks.
When working with RGB pixel formats (like in PNG), use the colorkey filter. Also, specify colour as hex codes, since ffmpeg's colour labels may not match what you expect e.g. Green is 0x008000, and not 0x00FF00.
Use
ffmpeg -i input.png -vf colorkey=0x00FF00 out.png
Yes
ffmpeg -i input.png -vf colorkey=0x008000 out.png
works.
I also found / after so much reading other related information/ that "chromakey" CAN work as well. Here is how
ffmpeg -i input.png -vf "chromakey=0x008000:0.1:0.1" -c copy -c:v png out.png
this work fine as well - the reason is codec -c:v of png.
A little bit more info- when i compared the results from #Gyan method and the second method - the second method produce much more anti aliased result.
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
What would be the best way to take a screenshot with ffmeg ( say the first frame of movie ) and then
make a 200x200 image from it?
Im not sure what method to look for in docs or api to accomplish these tasks.
EDIT #1
Seems that:
ffmpeg -i 001.mp4 -ss 3 -f image2 -vframes 1 -s 600x600 output.png
Does convert to an image, but... this is not scaled (squased together)
what would be the correct way to have the image proportional?
Copied and modified from elsewhere on Stackoverflow...
You want something like this: ffmpeg -i video filename -vcodec video codec -vframes 1 -an -s 200x200 -f rawvideo output.jpg