In the world of web development, sometimes we have to deal with other issues that are not directly related to programming or web design itself. In this case we are going to talk about cropping in FFmpeg and how to use it to remove black bars from videos. Thanks to this we will be able to generate thumbnails of our videos for use in our web projects (such as a Youtube clone).
As an example, let’s see the following capture made from a video:
Let’s imagine that this capture has been done automatically by a server-side application (FFmpeg can do this too). Next we are going to use FFmpeg to auto-detect and remove the black bars.
The first step is to use the cropdetect video filter to get the values automatically. The FFmpeg parameter is:
The cropdetect parameters follow the format
This parameter can contain up to three extra options. For our example we are not going to use any of them, but still, a brief explanation:
- limit: Sets the black color intensity threshold, which can be specified from nothing (0) to all (255). By default it’s 24. This will help when removing dark parts that are not black bars. Or vice versa, when it is not detecting the bars because they are not so black.
- round: Ensures that the output resolution is divisible by the specified value. Default is 16, the best value for most video codecs.
- reset: Determines after how many frames cropdetect will reset the previously detected largest video area and start counting again to detect the optimal crop area. This is useful when there are logos or animations at the beginning of a video. Default is 0, no reset.
We run the following command and since we are using
2>&1 the result will be printed on the screen.
As we can see at the end of the result, we already have our crop values.
The next step is to use those values in the following way:
The crop parameters follow the format
crop=w:h:x:y. Where W and H refer to the size (width and height), and X and Y refer to the coordinates where the result will start. An X and Y of 20 indicates that 20px to the left and 20px to the top will be ignored.
In this example, horizontally nothing is removed since there are no horizontal black bars. Vertically, the result will remove 80px at the top using the Y parameter. The bottom part is calculated in a different way. Our video is 576px high so after skipping 80px at the top by Y, FFmpeg will give us a H of 416 (576 - 80 - 80). Or in other words, 80px will be removed from both top and bottom.
This result can be used in our server application. For example, if our application is written in PHP we can use something like this:
In order not to receive an immense amount of cropping results, we have used the
-vframes 1 parameter, which limits our command to a single frame. Also, since the first frame is very likely to be all black, we will skip the first 150 seconds using the
-ss 150 parameter. This values will depend on the type of videos we are going to crop, a 40-second ad is not the same as a 120-minute movie.
The result is as follows:
If we want consistency in our thumbnails, rather than each having a different aspect, this is the step to take.
Remember that this generates images at the same size as the original video. If we want smaller thumbnails we have to add the resize process to make the images smaller.