Cropping and resizing videos are essential tasks in video editing. You might resize or crop a video to fit design guidelines, reduce file size and bandwidth, or match social media specifications. These tasks involve changing the resolution and aspect ratio of the video.
FFmpeg, a powerful multimedia framework, offers extensive functionalities, including video cropping and resizing. It can automate video editing processes effectively.
In this article, we will explore how to use FFmpeg to crop and resize videos.
Before we dive into the process, ensure that you have FFmpeg installed on your computer. FFmpeg is a command-line tool available for Windows, macOS, and Linux operating systems. You can download the newest version from the official FFmpeg website.
To follow along this tutorial, download the video example we're using and save it on your computer. The video is a vertical video with dimensions 1920px height by 1080px width.
Cropping a video lets you cut out unwanted parts. Cropping videos is important when repurposing content for social media. You need to adjust based on the channels' video specs.
To do this, FFmpeg provides a simple way to crop videos:
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
input.mp4
with the filename or path to your input video.output.mp4
with the desired filename or path for the output video.w
, h
, x
, and y
according to your requirements.The important part of this command is the -filter:v
and crop
options. The option -filter:v
means apply the filter to the video stream. The crop filter is then used to crop the video using the crop
parameters. You can write -filter:v
using the shorthand -vf
.
w
: Width of the uncropped region in pixels.h
: Height of the uncropped region in pixels.x
: The horizontal position of the top-left corner of the uncropped region. It's relative to the original video's top-left corner. Use a positive value to move right and a negative value to move left.y
: The vertical position of the top-left corner of the uncropped region. It's relative to the original video's top-left corner. Use a positive value to move down and a negative value to move up.For example, our original video is 1920px tall and 1080px wide. We want to crop it to a 1080px square for Instagram.
We also want to crop it from the centre. We do this by using this formula: original height minus target height divided by 2. In our example, it's 1920 - 1080 / 2 = 420
.
To crop our video example, you would use the following command:
ffmpeg -i car-overead-vertical.mp4 -filter:v "crop=1080:1080:420:0" cropped.mp4
The output video looks like this:
Resizing changes the size of the video without cropping.
FFmpeg simplifies this process by offering different resizing options. As with the crop filter, to resize a video you use the FFmpeg scale
filter, with a combination of options.
To resize a video by specifying the desired width and height, use the following command:
ffmpeg -i input.mp4 -filter:v "scale=w:h" output.mp4
w
and h
with the desired width and height, respectively.output.mp4
with the desired filename or path for the output video.For example, to resize our cropped video to 720x720 pixels, use this command:
ffmpeg -i cropped.mp4 -filter:v "scale=720:720" resized.mp4
And here is the output video:
In some cases, you may want to resize a video while keeping its original aspect ratio. You would adjust only one dimension: either width or height. To achieve this, you can use the following commands:
To resize by width, preserving the aspect ratio:
ffmpeg -i input.mp4 -filter:v "scale=w:-1" output.mp4
To resize by height, preserving the aspect ratio:
ffmpeg -i input.mp4 -filter:v "scale=-1:h" output.mp4
Replace w
or h
with the desired width or height, respectively.
If you prefer to resize a video by a certain percentage, you can use the following command:
ffmpeg -i input.mp4 -filter:v "scale=iw*percentage/100:ih*percentage/100" output.mp4
Replace percentage with the desired percentage value. For example, to resize a video to 50% of its original dimensions, use:
ffmpeg -i input.mp4 -filter:v "scale=iw*0.5:ih*0.5" output.mp4
Note that in this example, we use iw
and ih
in the percentage calculation. These parameters are for input width and height. They use the original video width and height and apply the calculation.
You can do the two commands above one after the other to crop and resize. But, it takes longer. And each time you encode, the video loses quality. A better approach is combining both the crop and scale filters in a single command.
FFmpeg filters combine by separating multiple commands with commas.
To crop and resize at the same time:
ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y,scale=w:h" output.mp4
In our video example, you would use the following command:
ffmpeg -i car-overead-vertical.mp4 -filter:v "crop=1080:1080:420:0,scale=720:720" resized-and-cropped.mp4
Here is the final cropped and resized video:
While FFMpeg is an effective tool, it takes a certain level of command-line knowledge and skills to use. Shotstack is an easier, more intuitive and user-friendly FFmpeg alternative to automate your video editing.
Shotstack's key advantages include:
FFMpeg is effective. But, using it takes some command-line skill and becomes complex to manage at scale. Shotstack provides a set of video editing tools, built to seamlessly integrate into your apps and workflows. It is scalable, powerful, and affordable, and provides a compelling alternative to FFmpeg.
Cropping and resizing videos with Shotstack only requires a simple API call.
Keen to get started? Sign up for Shotstack today!
curl --request POST 'https://api.shotstack.io/v1/render' \
--header 'x-api-key: YOUR_API_KEY' \
--data-raw '{
"timeline": {
"tracks": [
{
"clips": [
{
"asset": {
"type": "video",
"src": "https://shotstack-assets.s3.amazonaws.com/footage/beach-overhead.mp4"
},
"start": 0,
"length": "auto"
}
]
}
]
},
"output": {
"format": "mp4",
"size": {
"width": 1280,
"height": 720
}
}
}'