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.
Resizing and cropping are essential for addressing these requirements, ensuring the best performance and compatibility across devices.
1ffmpeg -version
1ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4
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
In this command:
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.
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.
Platforms like YouTube, Instagram, and Facebook have recommended resolutions and aspect ratios. Here’s how to scale to a specific platform:
ffmpeg -i input.mp4 -vf "scale=1080:1080" output_instagram.mp4
1ffmpeg -i input.mp4 -vf "scale=1280:720" output_youtube.mp4
Scaling to these standards ensures that your video appears optimally across platforms.
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
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.
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
Lanczos scaling uses a more sophisticated resampling technique, improving image sharpness compared to basic linear or bilinear scaling.
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
In this example:
Use Case: Ideal for creating video thumbnails or smaller versions of the original content.
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.
FFmpeg offers the crop filter, which allows you to specify the exact dimensions and position of the crop area within a video.
To crop a video, you need to specify:
Command example:
1ffmpeg -i input.mp4 -vf "crop=640:360:100:50" output_cropped.mp4
In this command:
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.
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
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.
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
Here:
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.
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
This crops the video to half of its original width and height, starting from the top-left corner.
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.
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.
By resizing and cropping videos strategically, you can achieve:
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!
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.
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.
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.
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.
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.
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.
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.
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.
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.
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.