1. Introduction

Video cropping and resizing are important techniques to control the visual aspects of a video. Moreover, they serve several purposes like optimizing for different screens, focusing on a particular object, or enhancing aesthetics.

FFmpeg is probably the most popular and powerful multimedia framework. FFmpeg is widely used in various multimedia applications:

It’s available on a wide range of operating systems like Linux, Mac OS, and Windows.

In this tutorial, we’ll learn to crop and resize a video using FFmpeg.

2. Environment Setup

First, let’s make sure that we have all the prerequisites ready, including FFmpeg and a sample video.

2.1. FFmpeg Installation

Before we dive into the details, let’s verify that we have FFmpeg installed on our system:

$ ffmpeg -version
ffmpeg version 4.4.2-0ubuntu0.22.04.1 Copyright (c) 2000-2021 the FFmpeg developers
built with gcc 11 (Ubuntu 11.2.0-19ubuntu1)

In case it’s not already available, we can install FFmpeg from the local package manager:

$ sudo apt install ffmpeg

In addition to FFmpeg, we’ll also need a sample video file for learning to crop and resize a video.

2.2. Sample Video

In addition to FFmpeg, we’ll also need a sample video file.

Let’s get one using the wget command:

$ wget https://sample-videos.com/video123/mp4/720/big_buck_bunny_720p_5mb.mp4

We’ll use the downloaded video file to learn how to crop and resize a video using FFmpeg throughout this tutorial.

2.3. Extracting Video Width and Height

First, we’ll find the width and height of our sample video using ffprobe:

$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of compact big_buck_bunny_720p_5mb.mp4

This command uses the FFprobe tool, part of the FFmpeg suite, to extract the width and height of the specified video file. Let’s understand each option used in this command:

  • ffprobe calls the FFprobe tool to extract information from selected media streams
  • -v error sets the verbosity level to error only
  • -select_streams is used to select the specific streams for the analysis, in this case it’s video stream with index 0 (v:0)
  • -show_entries specifies what information to display, stream=width,height tells the FFprobe to display the width and height of the selected video stream, i.e., resolution of the video
  • -of compact sets the output printing format to compact
  • big_buck_bunny_720p_5mb.mp4 is the name of the input video file

Now that we know the resolution, i.e., the width and height of the video, we can proceed toward cropping.

3. Cropping a Video

We crop a video to remove unwanted objects from the frame and focus on a particular object.

3.1. Basic Cropping

To begin with, we’ll use a template command to learn about the different options used for cropping a video:

$ ffmpeg -i input.mp4 -filter:v "crop=out_w:out_h:x:y" output.mp4

In this command, ffmpeg invokes the FFmpeg tool with the -i option, used to specify the input video file. The output file is output.mp4 as appended at the end of the command.

The part of the command where actual video processing happens is -filter:v “crop=out_w:out_h:x:y”. The -filter:v is used to specify a video filter, in this case, the crop filter with four parameters:

  1. out_w is the desired width of the output video
  2. out_h is the desired height of the output video
  3. x is the horizontal position in the input video
  4. y is the vertical position in the input video

So, x and y are the starting coordinates (top left corner) of the cropped area in the output video. In essence, we draw a rectangle over the input video and extract that as the output video viewport.

3.2. Cropping the Sample Video

For our particular crop, we’ll keep the central portion and remove some parts from each side of the video:

$ ffmpeg -i big_buck_bunny_720p_5mb.mp4 -filter:v "crop=640:360:320:180" cropped_video.mp4

This command creates a video file named cropped_video.mp4 by cropping the original video, starting from coordinates 320 (x), and 180 (y) resulting in a cropped video with a resolution of 640x360 pixels.

Further, we can use named options in the crop filter:

$ ffmpeg -i big_buck_bunny_720p_5mb.mp4 -filter:v "crop=w=640:h=360:x=320:y=180" cropped_video.mp4

Both of the above commands are the same and accomplish identical goals. We can also use the shortened -vf instead of -filter:v to set the video filters.

3.3. Use of Built-in Variables

We can use built-in predefined variables to set cropping parameters in FFmpeg:

  • in_w for input width
  • in_h for input height
  • x for the horizontal position in the input video
  • y for the vertical position in the input video

Let’s use some of these built-in variables to crop the video:

$ ffmpeg -i big_buck_bunny_720p_5mb.mp4 -filter:v "crop=in_w/2:in_h/2:320:180" cropped_video1.mp4

The crop filter from the above command is configured to halve the width (in_w/2) and height (in_h/2) of the original video, starting from coordinates 320 (x) and 180 (y). We can also use iw and ih, equivalents of in_w and in_h, respectively.

3.4. Cropping Central Input Area

We can crop the central part from a video using FFmpeg without specifying the x and y coordinates explicitly:

$ ffmpeg -i big_buck_bunny_720p_5mb.mp4 -vf "crop=600:600" cropped_video2.mp4

The above command crops the central square from the input video with a size of 600 pixels.

Additionally, we can use expressions to specify the output width and height:

$ ffmpeg -i big_buck_bunny_720p_5mb.mp4 -vf "crop=2/3*in_w:2/3*in_h" cropped_video3.mp4

This command crops the central area with a size of 2/3 of the input video and saves the cropped area to cropped_video3.mp4.

4. Resizing a Video

We can resize a video using the scale video filter from FFmpeg.

Let’s check the width and height of one of the cropped videos, cropped_video.mp4 with the ffprobe command:

$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of compact cropped_video.mp4 

Thus, the cropped_video.mp4 has a size of 640 pixels in width and 360 in height. Let’s resize this cropped video to its original size, i.e., 1280x720.

The scale filter in FFmpeg enables us to adjust the dimensions of a video. Let’s apply the scale filter to restore the cropped video to its initial resolution:

$ ffmpeg -i cropped_video.mp4 -vf "scale=1280:720" resized_video.mp4

This command creates a video file named resized_video.mp4 by resizing the input video, i.e., cropped_video.mp4, to a resolution of 1280 pixels in width and 720 pixels in height.

We can also check the size of the resized video with ffprobe:

$ ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of compact resized_video.mp4 

Therefore, we successfully resized cropped_video.mp4 to a size of 1280x720. Of course, we can expect a lower quality due to the stretching.

If any value for either width or height is less than 0, the scale filter from FFmpeg uses a value that maintains the original aspect ratio based on the other parameter:

$ ffmpeg -i cropped_video.mp4 -vf "scale=1280:-1" resized_video1.mp4

In this command, -1 indicates the automatic calculation of height to maintain the original aspect ratio. Moreover, if the provided value for width or height is 0, then the FFmpeg uses the input values for the respective output.

5. Cropping and Resizing Together

With FFmpeg, we can use multiple audio or video filters within a single command, separated by a comma:

$ ffmpeg -i big_buck_bunny_720p_5mb.mp4 -vf "crop=in_w/2:in_h/2:in_w/2:0,scale=1280:720" crop_resized_video.mp4

This command combines two video filters, crop, and scale, to manipulate the video file named big_buck_bunny_720p_5mb.mp4.

The first filter, crop, is configured to halve the width (in_w/2) and height (in_h/2) of the original video, starting from the center horizontally (in_w/2:0). As a result, this effectively crops the upper right part of the original video.

Subsequently, the second filter, scale, is applied to resize the cropped area to a resolution of 1280 pixels in width and 720 pixels in height. Finally, this command saves the resulting video as crop_resized_video.mp4.

6. Conclusion

In this article, we learned to crop and resize a video using FFmpeg.

Firstly, we learned how to install the FFmpeg tool and check the resolution, i.e., the height and width of the video. Secondly, we practiced cropping and resizing a video separately and simultaneously.

Moreover, we used the built-in variables in_w and in_h to define the cropping parameters. In addition, we saw how to let FFmpeg determine the optimal resolution based on the specified width for the video.

Comments are closed on this article!