How to Resize and Crop Videos using FFmpeg

November 11, 2024
11 minutes
Video Engineering
Jump to
Share
This is some text inside of a div block.

Optimizing videos for different platforms and devices is essential for delivering a seamless user experience, improving load times, and reducing bandwidth costs. Whether you’re preparing videos for mobile, desktop, or social media, resizing and cropping are key steps in adapting content efficiently. In this guide, we’ll walk through how to use FFmpeg to resize and crop videos with precision, covering practical techniques, performance insights, and example commands to make video processing straightforward and effective.

Why resizing and cropping matter?

Key metrics:

  • Video file size: Larger videos can exceed several gigabytes. Resizing can reduce video file sizes by up to 70%, making them easier to upload, download, and stream. For example, a 4K video that’s 10GB in size could be resized to 1080p, reducing the file size to around 1.5–3GB, depending on the compression.
  • Performance optimization: Videos that are optimized for bandwidth can load up to 3x faster on mobile networks with lower resolutions (e.g., 720p vs 1080p).
  • Social media standards: Each platform has its ideal video resolution:
    1. Instagram: 1080x1080 for square videos, which are 1:1 aspect ratio.
    2. YouTube: 1920x1080 for HD videos, optimizing the viewing experience across various devices.
    3. TikTok: 1080x1920 for full-screen vertical videos, a 9:16 aspect ratio.

Resizing and cropping are essential for addressing these requirements, ensuring the best performance and compatibility across devices.

Prerequisites

  • FFmpeg installed: Ensure you have FFmpeg installed on your machine. Check ffmpeg version with:
1ffmpeg -version
  • Basic command-line knowledge: Some familiarity with the command line is helpful.
  • To check video resolution before resizing or cropping, use:
1ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4

An example of taking a video of resolution 4K as input in FFmpeg.

Step-by-step guide to scaling and resizing with FFmpeg

  1. Basic scaling to a specific resolution

The simplest way to scale a video is by specifying the desired width and height. FFmpeg provides a scale filter, which lets you define custom dimensions.

Command example:

1ffmpeg -i input.mp4 -vf "scale=1280:720" output_720p.mp4

Executing the code in a bash shell to get an output file of resolution 1280x720.

In this command:

  • -i input.mp4 specifies the input file.
  • -vf "scale=1280:720" applies a video filter (-vf) to resize the video to 1280x720 resolution.
  • output_720p.mp4 is the name of the resized output file.

Data insight: Resizing a 4K video (3840x2160) to 720p (1280x720) reduces the file size by about 80%, from an average of 10GB to 1.5GB, with minimal visual quality loss.

  1. Maintaining aspect ratio

When resizing, preserving the aspect ratio (width-to-height ratio) is crucial to avoid stretching or squishing the video. You can use the -1 parameter in one of the dimensions to maintain the original aspect ratio automatically.

Command example:

1ffmpeg -i input.mp4 -vf "scale=1280:-1" output_aspect.mp4

Here, setting -1 for the height tells FFmpeg to calculate the height based on the original aspect ratio.

  1. Scaling for different platforms

Platforms like YouTube, Instagram, and Facebook have recommended resolutions and aspect ratios. Here’s how to scale to a specific platform:

  • Instagram square video (1080x1080):
ffmpeg -i input.mp4 -vf "scale=1080:1080" output_instagram.mp4

After executing the code snippet, output file generated is a square frame video of the input video.

  • YouTube HD (1280x720):
1ffmpeg -i input.mp4 -vf "scale=1280:720" output_youtube.mp4

After executing the code snippet, the output file is a video of 1280x720 resolution, or HD.

Scaling to these standards ensures that your video appears optimally across platforms.

  1. Using scaling for lower bitrate (bandwidth savings)

When reducing resolution for mobile or web distribution, it’s often essential to lower the bitrate for bandwidth efficiency. FFmpeg allows you to specify the bitrate along with scaling.

Command example:

1ffmpeg -i input.mp4 -vf "scale=640:-1" -b:v 500k output_mobile.mp4

After executing the code snippet, the output file is a video with a resolution of 640x500.

Here, -b:v 500k specifies the video bitrate to 500 kbps.

Use case: This command reduces the resolution to 640x360 and sets the video bitrate to 500 kbps, making it ideal for low-bandwidth environments like mobile 3G/4G networks.

Performance metric: Reducing the bitrate by 50% can reduce file sizes by up to 60% with negligible perceptual quality loss in mobile-optimized videos.

  1. Scaling with advanced filter

If you need more control over scaling quality, FFmpeg’s lanczos scaling algorithm is a great option. It improves the clarity of resized videos but may take longer to process.

Command example:

1ffmpeg -i input.mp4 -vf "scale=1280:720:flags=lanczos" output_highquality.mp4

After executing the code snippet, the output file is a video with resolution of 1280x720, and the lanczos filter.

Lanczos scaling uses a more sophisticated resampling technique, improving image sharpness compared to basic linear or bilinear scaling.

  1. Scaling based on percentage

You can also scale videos by a percentage instead of an absolute resolution. This can be useful for quickly creating smaller versions without calculating exact pixel dimensions.

Command example:

1ffmpeg -i input.mp4 -vf "scale=iw*0.5:ih*0.5" output_halfsize.mp4

After executing the code snippet, the output is a video file scaled down to 50% of its original dimensions.

In this example:

  • iw and ih stand for "input width" and "input height," respectively.
  • iw*0.5 and ih*0.5 scale the video to 50% of its original dimensions.

Use Case: Ideal for creating video thumbnails or smaller versions of the original content.

  1. Automating multiple resolutions for adaptive bitrate streaming

For Adaptive Bitrate Streaming (ABR), you may want to generate multiple versions of the same video in different resolutions. Here’s how to create three standard ABR versions (1080p, 720p, and 480p):

1ffmpeg -i input.mp4 -vf "scale=1920:1080" -b:v 5000k output_1080p.mp4
2ffmpeg -i input.mp4 -vf "scale=1280:720" -b:v 3000k output_720p.mp4
3ffmpeg -i input.mp4 -vf "scale=854:480" -b:v 1000k output_480p.mp4

By preparing these versions, you can offer users different resolutions based on their internet speed and device capabilities.

Data point: A video with resolutions at 1080p, 720p, and 480p will dynamically adjust depending on the viewer’s network speed, reducing buffering and improving user experience. Statistics show that videos with multiple bitrate versions have up to 30% fewer buffering events during playback.

Troubleshooting common scaling issues

  1. Aspect ratio distortion: If your video looks stretched, double-check that you’re using -1 for one of the dimensions to maintain the aspect ratio.
  2. Output file size too large: Try lowering the bitrate (-b:v), which can help reduce file size while retaining decent quality.
  3. Video quality drop: If you notice significant quality loss, experiment with flags=lanczos for a better resampling algorithm.

Cropping a video with FFmpeg

FFmpeg offers the crop filter, which allows you to specify the exact dimensions and position of the crop area within a video.

  1. Basic cropping

To crop a video, you need to specify:

  • width and height of the cropped area.
  • x and y coordinates to set where the crop should start.

Command example:

1ffmpeg -i input.mp4 -vf "crop=640:360:100:50" output_cropped.mp4

After executing the code snippet, the output is a video file with a cropped width and height of 640 by 360.

In this command:

  • 640:360 sets the width and height of the cropped area.
  • 100:50 positions the top-left corner of the crop area 100 pixels from the left and 50 pixels from the top of the original video.

This command will create a 640x360 video cropped from the specified (100, 50) position of the original video.

Use case: Removing black bars or centering the action in a video, for example, when preparing content for mobile or social media.

  1. Center cropping

If you want to crop from the center of the video, you can use FFmpeg’s iw (input width) and ih (input height) variables to calculate the center coordinates.

Command example:

1ffmpeg -i input.mp4 -vf "crop=640:360:(iw-640)/2:(ih-360)/2" output_center_cropped.mp4

After executing the code snippet, the output is a video file of cropped resolution 640 by 260 from the centre.

This command crops a 640x360 region fromthe center of the video by calculating (iw-640)/2 and (ih-360)/2 as thestarting coordinates.

Use case: This is especially useful for videos that were filmed in wide aspect ratios but need to be adapted for platforms that favor square or vertical formats.

  1. Command example:

If you need to convert a video’s aspect ratio (e.g., from 16:9 to 4:3), you can use the crop filter to remove excess width or height.

For example, to crop a 16:9 video to 4:3 without distorting the image:

1ffmpeg -i input.mp4 -vf "crop=ih*4/3:ih" output_4by3.mp4

After executing the command, the output is a video file with an aspect ratio cropped from 16:9 to 4:3.

Here:

  • ih*4/3 calculates the width that would achieve a 4:3 aspect ratio based on the original height.

Data insight: By cropping the width and adjusting the aspect ratio, videos will fit the display requirements of platforms or devices that prefer 4:3 over 16:9.

  1. Automating cropping with FFmpeg's in_w and in_h

If you are unsure of the exact dimensions or want to automate cropping based on video resolution, use in_w and in_h (input width and height) variables.

Command example:

1ffmpeg -i input.mp4 -vf "crop=in_w/2:in_h/2" output_half_crop.mp4

After executing the command, the output is a video file with resolution cropped to half.

This crops the video to half of its original width and height, starting from the top-left corner.

  1. Removing black borders

If your video has black borders, you can use ffmpeg’s cropdetect filter to detect and remove them automatically.

Command example:

1ffmpeg -i input.mp4 -vf "cropdetect=24:16:0" -f null -

This will output the suggested cropping values in the console. Once you have these values, you can apply them manually with the crop filter:

1ffmpeg -i input.mp4 -vf "crop=WIDTH:HEIGHT:X:Y" output_no_borders.mp4

Replace WIDTH, HEIGHT, X, and Y with the values returned from cropdetect to achieve an automatic, precise crop.

Insight: Black bars typically account for 10–20% of video content on social media platforms. Removing them can save bandwidth and improve content visibility.

Integrating with FastPix API

Fastpix provides an easy-to-integrate API to stream both live and on-demand content seamlessly using adaptive bitrate streaming. Using FastPix you can automatically scale video and get multiple renditions for ABR support.

Here is an example of how you can scale videos using create media by URL:

1{
2  "inputs": [
3    {
4      "type": "video",
5      "url": "https://static.fastpix.io/sample.mp4"
6    }
7  ],
8  "metadata": {
9    "key1": "value1"
10  },
11  "accessPolicy": "public",
12  "maxResolution": "1080p"
13}
14

Here, you can specify the max resolution of your video files to cap the max resolution of your ABR ladder up to 4k resolution.

Use case: Ideal for streaming services that require multiple resolutions to cater to different devices and network speeds.

Conclusion: Why resize and crop for better performance?

By resizing and cropping videos strategically, you can achieve:

  • Faster loading times: Smaller file sizes for quicker streaming and downloading.
  • Improved video quality: Resizing with the right scaling algorithms like Lanczosresults in minimal quality degradation.
  • Enhanced user experience: Tailored resolutions for each platform, device, and network condition ensure videos look great everywhere.

Through a combination of resizing techniques and cropping strategies, FFmpeg empowers video content creators to optimize their work for both storage and delivery efficiency.

Create your free account and sign up today!

Frequently asked questions (FAQs)

What are 'iw' and 'ih' in FFmpeg?

In FFmpeg, 'iw' and 'ih' stand for "input width" and "input height," respectively. These variables represent the original dimensions of the input video, which you can use in filters to make dynamic calculations, like maintaining aspect ratio or centering a crop.

How do I keep the aspect ratio when resizing in FFmpeg?

To keep the aspect ratio, set one dimension (width or height) to -1. For example, scale=1280:-1 resizes the width to 1280 pixels and automatically adjusts the height to maintain the original aspect ratio.

How can I crop a video from the center in FFmpeg?

Use the formula (iw - WIDTH)/2:(ih - HEIGHT)/2 in the crop filter to center the crop. For example, crop=640:360:(iw-640)/2:(ih-360)/2 crops a 640x360 section from the center of the video.

What does the 'cropdetect' filter do in FFmpeg?

The 'cropdetect' filter automatically detects and outputs the dimensions needed to remove black borders from a video. It scans the video and suggests crop parameters, which you can apply to the video for precise cropping.

What is the difference between resizing and cropping a video?

Resizing refers to changing the resolution of a video, either by scaling it down (e.g., 4K to 1080p) or up (e.g., 720p to 1080p), to fit the desired display or file size.

Cropping, on the other hand, involves removing unwanted portions from the video, typically around the edges, to focus on the most important content or change the aspect ratio.

  • Example Use Case: You may want to crop a wide-angle shot (16:9) to a squarer aspect ratio (1:1) for Instagram, and then resize it to fit the 1080x1080 resolution.

How do I change a video’s aspect ratio without stretching in FFmpeg?

Use the crop filter to adjust the video’s aspect ratio by trimming excess width or height. For example, to change a 16:9 video to 4:3, use crop=ih*4/3:ih to keep the height and adjust the width.

How does resizing affect video quality and file size?

Resizing a video reduces its resolution, which typically results in a decrease in file size. However, the extent of quality loss depends on the scaling method and the resolution you are resizing to.

  • Metric: Reducing the resolution of a 4K video (3840x2160) to 1080p (1920x1080) can reduce the file size by 70%–80%, with minimal quality loss when using high-quality resampling methods like Lanczos scaling.
  • Tip: For mobile networks or low-bandwidth environments, reducing resolution to 720p or 480p helps videos load faster and minimizes buffering, improving the user experience.

How do I resize a video by percentage instead of fixed dimensions?

You can resize by a percentage by multiplying iw and ih by the desired factor. For example, scale=iw*0.5:ih*0.5 resizes the video to 50% of its original width and height.

What’s the impact of resizing on SEO and user engagement?

Optimizing video file sizes through resizing directly impacts SEO and user engagement. Faster loading videos improve user retention, which is a key metric for platforms like YouTube, Vimeo, and social media platforms.

  • Example: Videos that load in under 3 seconds are 50% more likely to be watched to completion, and reducing video size by 50% can lead to 25% faster load times.
  • Actionable Tip: For YouTube, videos with smaller file sizes (whilemaintaining quality) rank better because they load faster, improving both userengagement and SEO performance.

Why is my output file size so large after resizing in FFmpeg?

File size depends on the bitrate, not just the resolution. To control file size, specify a lower bitrate with the -b:v flag. For example, -b:v 1000k reduces the video bitrate, which can help decrease file size.

Start Live Streaming for free

Enjoyed reading? You might also like

Try FastPix today!

FastPix grows with you – from startups to growth stage and beyond.