Video Editing

7 min read

How to crop video using FFmpeg?

Learn how to easily crop videos using FFmpeg, a powerful command-line tool. This guide provides step-by-step instructions and examples to help you customize your videos for various purposes.

How to crop a video with ffmpeg?
How to crop video using FFmpeg?

Cropping videos is a valuable feature for various reasons—from removing unwanted/black spaces, focusing on a specific area of the clip, or scaling/resizing/altering the aspect ratio for different platforms. This step-by-step guide will show you how to easily crop videos using the popular FFmpeg tool. FFmpeg is a powerful framework with commands like cropdetect, ffmpeg crop video, ffmpeg vf scale, etc.

How to find a video’s width & height before cropping?

To ascertain the width and height of a video file before cropping, you can use the FFprobe command. Here's how:

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

This command employs the FFprobe tool (which belongs to the FFmpeg suite) to extract the width and height of the specified video file. Here's what each part of the command does:

  • ffprobe: This initiates the FFprobe tool to get information about the video.
  • -v error: Configures the verbosity level to only show errors so you don't see unnecessary details.
  • -select_streams v:0: This picks the video stream for analysis — specifically the video stream with index 0 (v:0)
  • -show_entries stream=width,height: This tells FFprobe to show only the width and height of the video.
  • -of compact: This sets the output format to be compact and easy to read.
  • input_video.mp4: This is the name of the video file you're checking.

How to crop video using FFmpeg?

Using the crop filter

To crop a video using FFmpeg, you can simply use the ffmpeg crop filter. Here’s a straightforward way to do it:

ffmpeg -i example_input.mp4 -vf "crop=width:height:x_position:y_position" example_output.mp4

Here, enter in place of example_input.mp4 your input video file name, and in place of example_output.mp4 your desired output video file name. The parameters width, height, x_position, and y_position indicate the dimensions and location of the cropped area:

  • width: The width of the cropped area in pixels.
  • height: The height of the cropped area in pixels.
  • x_position: The horizontal offset from the top-left corner of the original video to the top-left corner of the cropped area. Positive values move the crop area to the right; negative values move it to the left.
  • y_position: The vertical offset from the top-left corner of the original video to the top-left corner of the cropped area. Positive values move the crop area down; negative values move it up.

The -vf option applies the crop filter to the video stream.

Example:

Let's say your original video is 1280 pixels wide and 720 pixels tall, and you want to crop it to a 720x720 square centered horizontally. To find the horizontal position (x_position), use the formula (original width - target width) / 2. In this case:

(1280−720)/2=280

To crop this video, the command would be:

ffmpeg -i nature_scene.mp4 -vf "crop=720:720:280:0" cropped_scene.mp4

This will crop the video to a 720x720 square; it will start 280 pixels from the left edge and have no vertical offset.

How to crop to a specific aspect ratio with FFmpeg?

To crop video to a specific aspect ratio in FFmpeg, you can use the crop filter. This allows you to define the width and height of the cropped area and specify where the crop should start within the video.

When you specify the position (x), FFmpeg will start cropping from that point towards the right and bottom of the video.

For example, to crop from the top-left corner (0,0):

ffmpeg -i example_video.mp4 -vf "crop=800:600:0:0" cropped_top_left.mp4

Here's a breakdown of the command:

  • -i example_video.mp4: Specifies the input video file.
  • -vf "crop=800:600:0:0": Applies the crop filter:
  • 800:600: Crops a section of the video with a width of 800 pixels and a height of 600 pixels.
  • 0:0: Starts cropping from the top-left corner of the video.
  • cropped_top_left.mp4: Specifies the output video file.

If you need to crop from the bottom-left corner, you should set the vertical position (y) to the difference between the video height and the crop height. For a video that is 1080 pixels tall and a crop height of 600 pixels, the y position would be 480 (1080 - 600):

ffmpeg -i example_video.mp4 -vf "crop=800:600:0:480" cropped_bottom_left.mp4

Also, use the in_h variable to automatically adjust the vertical position:

ffmpeg -i example_video.mp4 -vf "crop=800:600:0:in_h-600" cropped_bottom_left.mp4

To change the aspect ratio by cropping from the right, set the horizontal position (x) to the difference between the video width and the crop width. For a video 1920 pixels wide and a crop width of 800 pixels, the x position would be 1120:

ffmpeg -i example_video.mp4 -vf "crop=800:600:1120:0"cropped_right.mp4

Or, use the in_w variable for a more dynamic adjustment:

ffmpeg -i example_video.mp4 -vf "crop=800:600:in_w-800:0" cropped_right.mp4

How to crop a center/specific area of the video?

It is possible to crop the central part of a video using FFmpeg without specifying the x and y coordinates directly. Here's the command to do so:

ffmpeg -i sample_video.mp4 -vf "crop=500:500" center_crop.mp4

This command will extract a central square of 500x500 pixels from sample_video.mp4 and save it as center_crop.mp4.

  • -i sample_video.mp4: Specifies the input video file.
  • -vf "crop=500:500": Applies the crop filter:
  • 500:500: Specifies the width and height of the cropped area as 500x500 pixels.
  • center_crop.mp4: Specifies the output video file.

To crop a section of the video relative to its dimensions, you can use expressions. For example, to crop a central portion that covers half the width and height of the input video, you can run:

ffmpeg -i sample_video.mp4 -vf "crop=iw/2:ih/2" dynamic_crop.mp4

Here, iw and ih stand for input width and height, respectively. This crops out a central area that is half the size of the original video and saves it as dynamic_crop.mp4.

How to crop the video without re-encoding?

While true lossless cropping without re-encoding isn’t usually feasible with standard formats, you can minimize re-encoding when possible. Typically, FFmpeg does not support cropping without re-encoding directly.

To crop video without re-encoding in FFmpeg, we have the -c copy option. This way, you can modify the video container without changing the video stream itself. Here's how:

ffmpeg -i input.mp4 -vf "crop=width:height:x:y" -c:a copy output.mp4

Here's a breakdown of the command:

  •  input.mp4: Specifies the input video file.
  • -vf "crop=width:height:x:y": Applies the crop filter to the video:
  • width: The width of the cropped area.
  • height: The height of the cropped area.
  • x: The x-coordinate (horizontal position) of the top-left corner of the cropped area.
  • y: The y-coordinate (vertical position) of the top-left corner of the cropped area.
  • -c:a copy: Copies the audio stream from the input file without re-encoding. This keeps the original audio quality.
  • output.mp4: Specifies the output video file.

Replace width, height, x, and y with your desired crop dimensions and starting position. This command will re-encode the video but will copy the audio stream without re-encoding.

For specific formats like MPEG-TS, where direct stream copying is more applicable, you might use:

ffmpeg -i input.ts -vf "crop=width:height:x:y" -c copy output.ts

However, this may not always be applicable, and in most cases, FFmpeg will need to re-encode the video stream when cropping.

How to crop the black borders in the video using FFmpeg?

When a video has black borders to fit a particular aspect ratio, you can use FFmpeg’s cropdetect filter to determine the best crop area automatically. This filter analyzes the video, detects the non-black areas, and recommends the appropriate crop parameters. Here's how:

Step 1: First, run the following command to analyze the video and find the crop parameters:

ffmpeg -i sample_video.mp4 -vf cropdetect -f null - 2>&1 | grep crop | tail -1

This command processes sample_video.mp4 and outputs the recommended crop settings.

Step 2: After obtaining the crop parameters from the previous command, apply them to remove the black borders. For example, if the detected parameters are crop=1024:768:64:48, you can use:

ffmpeg -i sample_video.mp4 -vf "crop=1024:768:64:48" cropped_video.mp4

Step 3: To preview the cropped video without saving it, use:

ffplay -i sample_video.mp4 -vf "crop=1024:768:64:48"

This way, you can see the effect of the crop in real time before finalizing the output.

With that, we've reached the end of this article. As we've shown, cropping and resizing your videos with FFmpeg is quick and simple. Follow these simple commands, and you should be able to customize your videos precisely to your needs.

FAQs

1. How to crop different values of top/bottom in video through FFmpeg?

To crop different values from the top/bottom in a video using FFmpeg, you can use the crop filter. You need to specify the width and height of the cropped area, along with the offset from the top-left corner of the original video. Suppose you have a video file named input.mp4, and you want to crop 20 pixels from the top and 10 pixels from the bottom. Here's the command to do so:

ffmpeg -i input.mp4 -vf crop=iw:ih-30:0:10 -c:v av1_nvenc -c:a copy output.mp4

2. How to crop and scale video at the same time in FFmpeg?

You can crop and resize your video in one command using FFmpeg. This is more efficient as it discards the need for multiple encodings, which can decrease video quality. FFmpeg lets you combine filters by separating multiple commands with commas:

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

Here, the crop filter halves the width and height of the original video. Beginning from the center horizontally and from the top, this effectively crops the upper right part of the original video. The scale filter is used to resize the cropped video to a resolution of 1280x720 pixels.

3. How to get the aspect ratio of video using FFmpeg?

You can use FFmpeg with the following command:

ffmpeg -i <yourfilename>

The command will print detailed information about the video file to the screen. Look for the parameters related to the video stream, specifically the Pixel Aspect Ratio (PAR) and the Display Aspect Ratio (DAR). You’ll see output similar to this:

Stream #0:0[0x258]: Video: mpeg2video, yuv420p, 720x576 [PAR 64:45 DAR 16:9], 4350 kb/s, 27.97 fps, 25 tbr, 90k tbn, 50 tbc

4. Can I use FFmpeg to crop a video without losing quality?

Yes, cropping a video with FFmpeg itself doesn’t cause quality loss. However, encoding it after cropping typically leads to quality degradation. To minimize this, you can use FFmpeg’s crop and scale filters in a single command to avoid multiple encodings. Here's the command:

ffmpeg -i input.mp4 -vf "crop=width:height:x:y,scale=new_width:new_height" -c:a copy output.mp4

5. Can I crop multiple sections from a video using FFmpeg?

No, FFmpeg’s crop filter only lets you crop a single region at a time. To crop multiple sections, you would need to apply the crop filter separately for each section and then use other tools or FFmpeg commands to combine those sections into a single video.

Images or Videos Loading Slow?

We compress them so your users don't need to wait.