FFmpeg: How to Crop Videos/Images Using the Crop Filter

FFmpeg How to Crop Videos/Images Using the Crop Filter

FFmpeg is a powerful CLI tool that can do almost anything you can imagine with multimedia files.

It is a time-efficient and low-resource-consuming tool that can be used to crop both videos and images.

In this post, we will show you how to crop videos/images using the crop filter using FFmpeg.

Install FFmpeg (on Linux)

First, we will take a look at the command to install the FFmpeg tool.

Note: You can install FFmpeg on Linux, Windows or Mac OSX. In this tutorial we’ll quickly mention how to install it on Linux.

Debian-based Linux Distributions

For Debian-based Linux distributions run the below script to install FFmpeg:

sudo apt install ffmpeg

Redhat/ Centos based Linux

To install FFmpeg tool on Redhat/ Centos Linux systems run the following command:

yum localinstall
yum install ffmpeg ffmpeg-devel

Now let’s take a brief look into how to crop images using the FFmpeg tool and then we will dive right into the video. Bear in mind that the command for cropping images and videos is almost the same. So if you pay attention to how to crop photos using FFmpeg, you will not have a hard time understanding videos.

Cropping Images using FFmpeg

FFmpeg tool can be used to crop images. Below mentioned is the command that is used to crop images. The same command can be used to crop videos after minor tweaks so pay attention to what each parameter in it represents.

ffmpeg -i input.png -vf "crop=w:h:x:y" input_crop.png

First, we will take a look at what each of these parameters means to help us better understand how to use this tool. Here,

  • -i is the input parameter and input.png is the input image that we want to crop.
  • -vf specifies we are using a video filter.
  • "crop=w:h:x:y" specifies that we are using the crop video filter where w is the width you want after cropping the image, h is the height of the cropped image, x is the horizontal position from where the cropping starts and y is the horizontal position from where the image cropping starts. If x and y are not specified, the command will default to start cropping from the center.
  • input_crop.png is the new cropped image.

As an example, we have the following image and we want to crop a square of 10 pixels from the top left corner of the image.

word image 1

The above-mentioned command will look like this after putting in our desired dimensions.

ffmpeg -i input.png -vf "crop=10:10:0:0" input_crop.png

After execution this command will generate the following picture by cropping a square box of 10×10 dimensions from the top left corners of the picture.

word image 2

Now let’s see take a look at how to crop videos using the FFmpeg tool.

Crop a video using Pixel Coordinates

Let’s first look at the command that is used to crop videos using pixel coordinates and then discuss that in-depth. Below mentioned is the general structure of the command.

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4

This command is the same as the one used for cropping images we have just changed the input and output to mp4.

  • -i is specifying the input video that will be cropped. (input.mp4 being the input video)
  • -filter:v specifies that we are using a video filter. It can also be written as -vf.
  • "crop=w:h:x:y" specifies that we are using the crop filter which takes in 4 values to determine the dimensions and length of the output video.
    • w is the width of the output video. It can also be represented by out_w. If w is not specified the command will default to the input width or in_w.
    • h is the height of the output video. It is also represented by out_h. If output height is not specified it will default to input height or in_h.
    • x specifies the horizontal position starting from the left, from where the cropping will begin. 0 is the absolute margin for x.
    • y specifies the vertical position starting from the top, from where the cropping will start. 0 is the absolute margin for y. If x and y are not specified, the default crop will start from the center.
    • output.mp4 is the cropped output video.

Few Notes:

There is an additional option available represented by keep_aspect that you define to 1 to keep the output video aspect ratio the same as the input video. To use this with the whole command just add the parameter after specifying crop dimensions like "crop=100:100:0:0:keep_aspect=1". This parameter does not work with images which is why it was not mentioned in the section earlier.

Let’s assume we want to crop a 200×200 portion of a video from the center. We want the input video cropped 100 pixels from the left and 200 pixels from the top. The command with filled-in parameters to do this will look like this:

ffmpeg -i input.mp4 -filter:v "crop=100:100:100:200" output.mp4

In case we do not specify the values of x and y, the cropping will start from the center of the video.

Crop a video using Variables

Instead of specifying the literal pixel coordinates to crop a video, we can also use variables to denote the video parameters. Let’s say we want the cropped video to be of half the width and height as compared to the input video. The command to perform this task will look like this:

ffmpeg -i input.mp4 -filter:v "crop=in_w/2:in_h/2" output.mp4

In the above command we have used in_w instead of w and in_h instead of h. And divided both by 2 to specify that we want the cropped video to be of half-width and height as compared to the input video. As we have not specified the values of x and y, the cropping will start from the center of the video.

If you know the input video dimensions i.e, the video width is 300 and height is 200. Then the above-mentioned command can also be written as follows.

ffmpeg -i input.mp4 -filter:v "crop=300/2:200/2" output.mp4

Which also translates into:

ffmpeg -i input.mp4 -filter:v "crop=150:100" output.mp4

We can tweak the above command to our desire. For example, instead of in_h/2, we can use in_h-200 to subtract 200 pixels from the input video height. Keep these little tricks in mind as they open limitless cropping options.

As we have not specified the values of x and y, the cropping will start from the center of the video.

Cropping black borders using the FFmpeg tool

Now let’s take a look at a more real-life example and use a video to clearly see what is happening.

That is a Linux foundation commercial video and if you look at it you will know that there are black borders on the top and bottom side of the video and we want to remove them.

word image 220

You can run a command to know what the correct dimension should be for the video. The command is as follows:

ffmpeg -i linux-foundation.mp4 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1

If you execute the above-mentioned command it will return the correct dimensions for w,h,x, and y that ensure that black borders have been removed.

ffmpeg -i linux-foundation.mp4 -vf cropdetect -f null - 2>&1 | awk '/crop/ { print $NF }' | tail -1
Output
crop=320:208:0:16

The command returns that the proper width is 320, height is 208, x is 0 and y is 16 for the video to have no black borders.

Now we can put these values in our command for cropping videos and remove the black borders. The command will look as follows after filling in these values.

ffmpeg -i linuxfoundation.mp4 -filter:v "crop=320:208:0:16" output.mp4

When you execute the above command, black borders will be removed and the video will look like this.

word image 221

Conclusion

There are limitless ways of using the FFmpeg tool to crop videos and images. Try and think out of the box and you will achieve your goal.

In this tutorial, we have looked at how to crop videos and images using the crop filter in FFmpeg, using various methods and examples.

0 Shares:
Subscribe
Notify of
guest
Receive notifications when your comment receives a reply. (Optional)
Your username will link to your website. (Optional)

0 Comments
Inline Feedbacks
View all comments
You May Also Like
Bash Wait Command
Read More

Bash Wait Command

The wait command in bash is a process management command that waits for the specified process running in…
Bash While Loop
Read More

Bash While Loop

In almost all programming languages, a loop is an easy and basic method used to repeat single or…