Remove .mp4 video top and bottom black bars using ffmpeg

23

11

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

user360123

Posted 2014-09-11T18:23:08.413

Reputation:

Windows? Install WinFF. It will allow you to type in the values you want to crop through a GUI... – Kinnectus – 2014-09-11T19:11:04.257

Answers

41

FFmpeg cropdetect and crop filters

1. Get crop parameters

cropdetect can be used to provide the parameters for the crop filter. In this example the first 90 seconds is skipped and 10 frames are processed:

$ 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

So according to cropdetect we can use crop=1280:720:0:0.

2. Preview with ffplay

$ 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.

Also see


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

llogan

Posted 2014-09-11T18:23:08.413

Reputation: 31 929

Sorry but I'm new to ffmpeg and I do not understand the part to remove black bars on top and bottom and resize video to the remaining size without black bars. – None – 2014-09-11T20:23:57.287

1Is there an automating way to remove black bars or I have to use manually crop values always? – None – 2014-09-11T20:24:46.260

1@rexhin When you run cropdetect it will dump a bunch of info in the console output as shown above. It literally gives you the actual crop parameters that you need to use. You can automate it via scripting. – llogan – 2014-09-11T20:58:34.820

Wow awesome it worked. But where did we put the filter for black? Also what are the other two params in crop=1280:720:0:24 – None – 2014-09-11T21:11:55.567

It has removed the top black bar but has left a small black bar at bottom. Why did this happen? – None – 2014-09-11T21:31:29.340

@rexhin Sorry, but I don't understand what you mean by, "where did we put the filter for black?". See the crop documentation to see what each parameter means. If cropdetect did not supply the correct values, then you will have to adjust it (see docs), or just use crop "manually".

– llogan – 2014-09-11T23:28:00.813

Thank you very much :) I am more clear now aurodetect works just fine. – None – 2014-09-12T23:00:52.233

cropdetect command does not work, returns "At least one output file must be specified" – Ben Sinclair – 2015-02-25T10:48:11.990

@Andy Without your command and console output I can't come up with any suggestions. – llogan – 2015-02-25T16:48:50.250

@LordNeckbeard I guess the problem is in ffmpeg for mac. All tools for mac have some quirks. – Ben Sinclair – 2015-02-25T20:31:38.937

1

@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.

Note: -c:a can be replaced with: -acodec

The easiest alternative free solution I found was to use handbrake.

It's autocrop removed the black bars without much trouble.

Hope that helps.

phyatt

Posted 2014-09-11T18:23:08.413

Reputation: 231