How to remove black bands from a video with FFmpeg ‘cropdetect’ and ‘crop’ filters.

How to remove black bands from a video with FFmpeg ‘cropdetect’ and ‘crop’ filters

Miguel Menéndez

Easily and automatically remove with FFmpeg’s cropdetect and crop filters the black bands typical of widescreen videos converted to 4:3.

First we are going to find out which areas of the image are occupied by those black bands that usually occupy the upper and lower edges but which, as we will see in the following example, can also appear on the left and right.

Find the area to crop with the cropdetect filter

$ ffmpeg -ss 90 -i input.mpg -vframes 10 -vf cropdetect -f null -

Where -vframes is the number of frames to parse (10 in this example).

The above command will return, among other things:

[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:7200 t:0.080000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:10800 t:0.120000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:14400 t:0.160000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:18000 t:0.200000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:21600 t:0.240000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:25200 t:0.280000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:28800 t:0.320000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:32400 t:0.360000 crop=688:400:14:88
[Parsed_cropdetect_0 @ 0x55f569ad44c0] x1:7 x2:707 y1:83 y2:492 w:688 h:400 x:14 y:88 pts:36000 t:0.400000 crop=688:400:14:88

The only thing we care about here are the crop values, in this example crop=688:400:14:88.

Preview

Before actually cropping the video, we play it by applying the crop proposed by the cropdetect filter:

$ ffplay -vf crop=688:400:14:88 input.mpg

We finally crop with the filter crop

If we are satisfied with the result of the preview, we apply the crop to the video:

$ ffmpeg -i input.mpg -vf crop=688:400:14:88 -c:a copy output.mpg

Before

After

Images from Big Buck Bunny .

Comments

Found a bug? Do you think something could be improved? Feel free to let me know and I will be happy to take a look.