How to crop and resize videos using FFmpeg

Cropping and resizing are two common video editing tasks used to transform and optimise videos. Typically, you would resize or crop a video to meet design guidelines, reduce file size and internet bandwidth or to match social media specifications. Altering the size of a video is also referred to as changing the resolution, and altering the size also determines the aspect ratio.

FFmpeg, a powerful and versatile multimedia framework, provides a wide range of functionalities, including video cropping and resizing. The great thing about FFmpeg is that it can be used to automate video editing.

In this article, we will explore how to use FFmpeg to crop and resize videos and compare it with Shotstack, a cloud-based video editing API, to achieve your desired output.

Installing FFmpeg

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 latest version from the official FFmpeg website and follow the installation instructions provided.

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 x 1080px width:

Crop videos using FFmpeg

Cropping a video allows you to remove unwanted portions from the frame, drawing viewers' eyes to a specific subject or adjusting the aspect ratio. A good example of when cropping videos is important is when you're repurposing and adjusting content based on social media channels' video specs.

To do this, FFmpeg provides a straightforward method to crop videos using the following syntax:

ffmpeg -i input.mp4 -filter:v "crop=w:h:x:y" output.mp4
  • Replace input.mp4 with the filename or path to your input video.
  • Replace output.mp4 with the desired filename or path for the output video.
  • Specify the cropping parameters w, h, x, and y according to your requirements.

The important part of this command is the -filter:v and crop options. FFmpeg has hundreds of build in video filters that let you manipulate and transform video (and audio). 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. Note that -filter:v can be written using the shorthand -vf

Understanding the crop parameters

  • w: Width of the uncropped region in pixels.
  • h: Height of the uncropped region in pixels.
  • x: Horizontal position of the top-left corner of the uncropped region relative to the original video's top-left corner. Use a positive value to move right and a negative value to move left.
  • y: Vertical position of the top-left corner of the uncropped region 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 example has a height of 1920px and a width of 1080px and we want to crop it to a 1080px x 1080px video for Instagram.

We also want to crop it from the centre and we do this by calculating the x axis with this formula: original height - target height / 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:

Resize videos using FFmpeg

Resizing videos allows you to adjust their dimensions, whether to fit within specific display resolutions, reduce file size, or change aspect ratios. Unlike cropping, which focuses on removing portions of the frame, resizing adjusts the overall 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.

Resizing by width and height

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
  • Replace w and h with the desired width and height, respectively.
  • Modify output.mp4 with the desired filename or path for the output video.

For instance, to resize our cropped video example to 720x720 pixels, you would use the following command:

ffmpeg -i cropped.mp4 -filter:v "scale=720:720" resized.mp4

And here is the output video:

Resizing by width or height while maintaining aspect ratio

In some cases, you may want to resize a video while maintaining its original aspect ratio, adjusting 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.

Resizing by percentage

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 stand for input width and input height and use the original video width and height and apply the calculation.

Crop and resize videos using FFmpeg

There are cases when you would need to crop a video and select an area the shows your main subject and reduce the video's width and length. This way, you can transform the video to fit a social media channel's aspect ratio while keeping the most important parts and quality of your video.

Sure, you can do the two commands above but it takes longer and each time you encode, the video loses quality. A better approach is combining both the crop and scale filers in a single command.

FFmpeg filters can be combined by simply separating multiple commands with a comma.

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:

FFmpeg vs. Shotstack

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:

  • Shotstack does not require installation or setup, while FFmpeg must be compiled and configured on your own server or machine.
  • Shotstack uses a JSON-based editing format that is easy to learn and allows you to create complex video workflows and edits using familiar video editing concepts, while FFmpeg uses various flags and parameters that can be hard to understand and remember.
  • Shotstack provides a fully managed, scalable and reliable service that handles video rendering and hosting, while FFmpeg requires you to manage your own infrastructure and resources for video processing and storage.

Editing videos using Shotstack

There are two ways to crop and resize videos on Shotstack. We recommend using the Video Editing API if you want to perform other tasks, such as adding texts or transitions, stitching multiple videos and more. But if you simply want to resize or crop your video, we suggest using the Ingest API.

Follow our comprehensive guide to resizing and cropping videos using both APIs to find out how easy it is to use Shotstack. You can also check our easy-to-understand Edit API and Ingest API documentation to discover other flexible and robust functionalities that help you create stunning and unique videos at scale.

Keen to get started? Sign up for your free account with Shotstack today – no credit card required!

Kathy Calilao

BY KATHY CALILAO
26th July, 2023

Become an Automated Video Editing Pro

Every month we share articles like this one to keep you up to speed with automated video editing.


You might also like

Merge videos using the Edit API

Merge videos using the Edit API

Jeff Shillitto
Trim a video using the Edit API

Trim a video using the Edit API

Jeff Shillitto