How to Compress a Video to a Target Size With FFmpeg
How to calculate the bitrate for a size limit, when to use CRF instead, which codec to choose, and the FFmpeg commands to run, with a worked example.

How to Compress a Video to a Target Size With FFmpeg
Many services limit uploads: 25 MB for email attachments, set limits for chat apps and course platforms. Re-encoding the video at the right bitrate gets it under the limit with the best possible quality. The video bitrate and FFmpeg command builder works out the numbers and writes the commands.
The size formula
File size is bitrate multiplied by duration. So the total bitrate you can afford is size × 8 / duration. In kilobits per second, video bitrate = target size in MB × 8,000 / duration in seconds − audio bitrate. A 3 minute 20 second clip, 200 seconds long, that must fit in 25 MB with 128 kbps audio, can use about 872 kbps for video.
Two-pass encoding
When aiming for a size, two-pass encoding is best. The first pass analyses the whole video; the second pass spends more bits on complex scenes and fewer on simple ones, hitting the target size closely. The builder writes both commands.
CRF: aiming for quality instead
If you do not have a strict size limit, constant rate factor (CRF) is easier. You choose a quality level and the encoder uses whatever bitrate it needs. For x264 the default is 23 and the useful range is about 18 (visually lossless) to 28. For x265 the default is 28. Raising CRF by about 6 roughly halves the file size.
Bits per pixel
Bits per pixel = bitrate / (width × height × frames per second). For H.264, values below about 0.04 tend to show blocky detail in motion. If the builder warns you, reduce the resolution: 720p at 872 kbps usually looks better than 1080p at the same bitrate.
Choosing a codec
H.264 plays on every device and browser. H.265 (HEVC) gives similar quality at 30% to 50% lower bitrate but is not supported everywhere. AV1 is the most efficient and is supported by modern browsers and phones, but encoding is slower. VP9 in WebM suits web pages.
Running FFmpeg
Install FFmpeg from its official site or a package manager. Open a terminal in the folder with your video, paste the first command, wait, then paste the second. On Windows, replace /dev/null with NUL. The output file is named after the input with -small added.
A worked example
A 200-second 4K phone video must be sent by email under 25 MB. Choose target size 25 MB, duration 3:20, audio 128 kbps, H.264 and 720p. The video bitrate is 872 kbps. At 720p the bits per pixel more than double compared with 1080p, which looks clearly better on a phone or laptop screen.
Audio
128 kbps AAC suits most content; 96 kbps is enough for speech.
Questions people ask
Will compressing again lose quality? Yes, every lossy re-encode loses some detail, so always start from the original file. Can I cut the video too? Add -ss start and -to end times before the output name, or trim first in an editor. Is there a simpler option? Many apps use FFmpeg underneath; this builder simply shows you the exact settings so you are in control. What about vertical phone videos? Scaling with -2 keeps the aspect ratio for both orientations.
Hardware encoders
Graphics cards include fast hardware encoders, such as h264_nvenc or hevc_videotoolbox, which are much quicker but slightly less efficient at the same bitrate. They are useful for long videos when speed matters more than size.
Limits
Final sizes may differ by a few per cent because of container overhead and rate control. If you are just over the limit, reduce the target size slightly and re-run.


&c=%23c2410c&k=education)
