You are here

FFMPEG to Crop Video "Black Bars"

Error message

Deprecated function: strpos(): Passing null to parameter #1 ($haystack) of type string is deprecated in drupal_strip_dangerous_protocols() (line 1458 of /home2/crephoto/public_html/techblog/includes/common.inc).

After using ffmpeg for a number of years, I've learned that there's virtually nothing it can't do. So when I recently encoded a video with "black bars" by mistake, I thought it might just be quicker to crop it than re-encode it. Naturally, ffmpeg can do that. A quick google revealed the following solution on stackoverflow. Here is a summary of the two-step process:

First, identify the size and position of the black border:

ffmpeg -i  -vf cropdetect,metadata=mode=print -f null -

The previous command generates quite a verbose output, but you'll easily be able to recognize the width & height of the video contained between the black border. In my case, the original video was encoded at 1920x1080 (AKA, 1080p HD), and the actual 4x3 video was 1440x1080, with the black "bars" on the left & right sides. Note that this process should work for you if you have a 16x9 video encoded at 4x3 with "bars" at the top & bottom, or even with black all the way around the video as sometimes happens when encoding 720x480 DVD quality video. In any case, you'll need to adjust the values in the next step.

ffmpeg -i  -vf crop=w=1440:h=1080:x=240:y=0 

Note that the black "bars" were symmetrical in my case and occupied the leftmost 240 pixels (x=0 to x=240) and the rightmost 240 pixels (x=1680 to x=1920). So you can see the crop option above, I've set width to 1440, height to 1080, and the upper left corner of the crop starting at x=240 and y=0.

I'm no expert on ffmpeg nor video manipulation, so I was a bit concerned about the transcoding process. I visually compared several frames of the cropped video to the original, and there were no perceptible differences, so I just went with it. However, In the future, I intend to explore this more in-depth since I've used the "-c copy" option in the past to copy the video stream, but that's for a later time. As always, test before you try this!

Tags: