FFmpeg: Video conversion and resizing for achieving target file sizes

FFmpeg is a powerful and versatile open-source software that allows users to manipulate multimedia files with ease. One of its remarkable features is the ability to convert videos to a specific file size, ensuring a balance between quality and storage efficiency.

In our example, we’ll cover the process of converting a 6-minute 4K MOV video into an MP4 file with a target size of 60 MB.

Calculating Bitrates

Before diving into the FFmpeg commands, it’s crucial to understand how to calculate the necessary bitrates for video and audio. The formula for calculating video bitrate is:

Total Bitrate = Target File Size (in KB) × 8 / Video Duration (in seconds)

60 × 1024 × 8 / 360 = 1365 kbps

For audio bitrate, a common choice is 128 kbps for good audio quality. In the end we subtract the audio bitrate from our total bitrate to get our target video bitrate: 1365 – 128 = 1237

First Command

This command converts the input MOV video to an MP4 format using the x264 video codec and AAC audio codec. The specified bitrates are 1205 kbps for video and 128 kbps for audio.

ffmpeg -i input.mov -c:v libx264 -b:v 1237k -c:a aac -b:a 128k output.mp4

-i input.mov specifies the input file.

-c:v libx264 selects the H.264 video codec for encoding. Instad also the H.265 video codec (libx265) could be used.

-b:v 1205k sets the target video bitrate to 1237 kbps.

-c:a aac specifies the audio codec for encoding, which is AAC (Advanced Audio Coding) in this case.

-b:a 128k sets the target audio bitrate to 128 kbps.

output.mp4 specifies the output file.

Second Command

Similar to the first command, this one includes a video scaling option. It scales the video to a width of 1920 pixels while maintaining the original aspect ratio.

ffmpeg -i input.mov -c:v libx264 -b:v 1237k -c:a aac -b:a 128k -vf 'scale=1920:1920:force_original_aspect_ratio=decrease' output.mp4

-vf 'scale=1920:1920:force_original_aspect_ratio=decrease' applies a video filter to scale the video to a resolution of 1920×1920 pixels while maintaining the original aspect ratio.

Third Command

This command uses the two-pass encoding process. In the first pass, the entire video is analyzed, regarding its scene complexity, and the information is stored in a log file. This information is then used in the second pass to distribute bits more intelligently, optimizing quality and achieving the desired file size.

ffmpeg -i input.mov -c:v libx264 -b:v 1237k -c:a aac -b:a 128k -vf 'scale=1920:1920:force_original_aspect_ratio=decrease' -pass 1 -f mp4 -y NUL && \
ffmpeg -i input.mov -c:v libx264 -b:v 1237k -c:a aac -b:a 128k -vf 'scale=1920:1920:force_original_aspect_ratio=decrease' -pass 2 output.mp4

-pass 1 specifies that this is the first pass of the two-pass encoding process.

-f mp4 specifies the output format as MP4.

-y NUL redirects the output to null (or NUL on Windows) since you don’t want an actual output file for the first pass. This pass is just to analyze the video.

-pass 2 specifies that this is the second pass of the two-pass encoding process.

More FFmpeg commands

asterix Written by:

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *