How to crop and resize videos using FFmpeg

It’s no secret that video content has become increasingly popular and pervasive and an essential tool for organisations. Naturally, cropping and resizing are two important editing skills needed to transform and optimise videos to fit aspect ratios required by different social media channels.

FFmpeg, a powerful and versatile multimedia framework, provides a wide range of functionalities, including video cropping and resizing. In this article, we will explore how to utilise 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. A YouTube video of a car showcase is too wide to share on Instagram Reels or TikTok so you would want to crop to focus on certain parts of the car instead.

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.

Understanding the cropping parameters

  • w: Width of the cropped region in pixels.
  • h: Height of the cropped region in pixels.
  • x: Horizontal position of the top-left corner of the cropped 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 cropped 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 while preserving the entirety of the content within the frame.

FFmpeg simplifies this process by offering different resizing 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 -vf "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 -vf "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 -vf "scale=w:-1" output.mp4

To resize by height, preserving the aspect ratio:

ffmpeg -i input.mp4 -vf "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 -vf "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 -vf "scale=iw*0.5:ih*0.5" output.mp4

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.

To crop and resize at the same time:

ffmpeg -i input.mp4 -vf “crop=""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 -vf "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 tool to edit your videos.

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.


Related articles

Resize and crop videos using the Edit and Ingest API

Resize and crop videos using the Edit and Ingest API

Learn to crop or resize videos using the Shotstack's Edit and Ingest API's

Emad Bin Abid
Merge videos using the editing API

Merge videos using the editing API

How to use the Shotstack API to merge video clips together to edit a simple video.

Jeff Shillitto
Trim a video using the editing API

Trim a video using the editing API

Learn how to trim the start, end or middle of a video using the Shotstack API.

Jeff Shillitto