I'm new to ffmpeg and I have heard that it has a filter for black. I want to remove black bars on top and bottom of the video so the video will be cropped to the remaining video without black bars. Thanks:)
FFmpeg cropdetect and crop filters
1. Get crop parameters
$ ffmpeg -ss 90 -i input.mp4 -vframes 10 -vf cropdetect -f null -
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:215 t:0.215000 crop=1280:720:0:0
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:257 t:0.257000 crop=1280:720:0:0
[Parsed_cropdetect_0 @ 0x220cdc0] x1:0 x2:1279 y1:0 y2:719 w:1280 h:720 x:0 y:0 pts:299 t:0.299000 crop=1280:720:0:0
At the end of each line, you can see it says
crop=1280:720:0:0. So according to cropdetect we can use
2. Preview with
$ ffplay -vf crop=1280:720:0:0 input.mp4
3. Re-encode using the crop filter
$ ffmpeg -i input.mp4 -vf crop=1280:720:0:0 -c:a copy output.mp4
In this example the audio is just stream copied (re-muxed) since you probably don't need to re-encode it.
Crop during playback
As you've seen above with the
ffplay example some players allow you to crop upon playback. This has the advantage of:
- Instant gratification; no need to re-encode
- The quality is preserved
@LordNeckbeard 's answer is great. I would recommend it in most cases.
ffplay worked great and previewed well, but the version of ffmpeg I was using struggled with the audio from this video I was using.
st:1 error, non monotone timestamps
I ended up having trouble with the proposed answer both with
-c:a not being supported with the version I was running and with a problem with bitrate conversion with the video I was using.
-c:a can be replaced with:
The easiest alternative free solution I found was to use handbrake.
It's autocrop removed the black bars without much trouble.
Hope that helps.