In this article we will show you how to crop a video file to given dimensions using ffmpeg.
We can use this solution in every platform supported by ffmpeg. So it is a good solution to crop a video file for Windows, Max or Linux systems (ffmpeg is available for all of them)
We can achieve this operation using the ffmpeg crop filter. It uses the syntax:
ffmpeg -i input_video.mp4 -filter:v "crop=w:h:x:y" output_video.mp4
We are using the parameters:
-i input_video.avi to indicate the input video file. It can be a video file (or also an image file if you want to use ffmpeg to crop an image, but they are other programs which can perform this task)
-filter:v “crop=w:h:x:y” we are using -filter:v which indicates that we are using a video filter. The video filter is specified after that, and we are using the crop filter. After that we are indicating 4 important variables:
w which indicates the width of the rectangle that we want to crop from the original video.
h which indicates the height of the rectangle that we want to crop from the original video.
x which indicates the x coordinate of the rectangle that we want to crop from the original video. The left of the video would be zero. You can omit this variable if you want and ffmpeg will take x as zero so it will crop from the left part of the video.
y which indicates the y coordinate of the rectangle that we want to crop from the original video. The top of the video would be zero.
output_video.avi is the name of the output file that we want to produce. You should have in mind that after cropping the video it will be encoded, so it can be important to choose the right video file extension, or specify the right video/audio codecs.
Here we have the original video.
It has an original resolution of 320x180 pixels.
If you want to crop a 100x60 section, from position (100,100) you can use:
ffmpeg -i input_video.mp4 -filter:v "crop=100:60:100:100" output_video.mp4
Here is the result.
You can crop a rectangle of whatever aspect ratio you want. For example, crop the same video from the position (100,50) with a width of 100 pixels and a height of 120 pixels.
ffmpeg -i input_video.mp4 -filter:v "crop=100:120:100:50" output_video.mp4
Here it is the result.
Some final notes
As you can see in the last example you can crop a video to whatever aspect ratio you want. There are no limitations on it.
You can use various operations and filters on the same command. It can be useful to crop only a part of a video file. For example you can use the following command to crop a 100x60 rectangle from the position (120,80) but only from the second 5 of the video to the second 10 (using a 5 seconds duration)
ffmpeg -i input_video.mp4 -ss 00:00:05.000 -t 5 -filter:v “crop=100:60:120:80” output_video.mp4
And the result:
Also remember that the output video will be encoded so if the result is big it can take some time to create it. This also allows you to change the video format or the codecs used in it.
There are some variables that you can use as width, height, x and y. And you can use expressions with them. It can be helpful to use some of them like:
- in_w indicates the input video width. For example you can use it to indicate half of the video width with the expresion 'in_w/2'
- in_h indicates the input video height. You can use it to indicate half of the video height with the expresion 'in_h/2'
Did you find this article helpful?