Use FFmpeg to crop and resize videos

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.

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 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.

Use FFmpeg to crop videos

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
  • 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. 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.

Understanding the crop parameters

  • 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:

Use FFmpeg to resize videos

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.

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 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:

Resizing by width or height while maintaining aspect ratio

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.

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

Use FFmpeg to crop and resize videos

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:

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

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!

Get started with Shotstack's video editing API in two steps:

  1. Sign up for free to get your API key.
  2. Send an API request to create your video:
    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
    }
    }
    }'
Kathy Calilao

BY KATHY CALILAO
26th July 2023

Studio Real Estate
Experience Shotstack for yourself.
SIGN UP FOR FREE

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