Re-encode with -crf 28. Higher CRF means a smaller file and a softer picture, and on the test clip 28 cut the size to a third. When the file has to fit a limit, work out a bitrate from the size and the duration and encode in two passes.
SmallerRe-encode
You want a smaller file and the exact size does not matter.
ffmpeg-iin.mp4-c:vlibx264-crf28-c:acopyout.mp4
34.6 MB down to 11.9 MB, a third of the size. VMAF 93.5 against the input. Audio copied as it was.
Size
34% of the input
VMAF
93.5
Run time
6.8 s
Output
11.9 MB
Target sizeRe-encode
The file has to fit an upload or attachment limit.
7.91 MB, under the 8 MB line. VMAF 89.7. The bitrate is 8 MB over 40 s, cut by 3%, minus 128k for audio.
Target
8 MB, landed 7.91 MB
VMAF
89.7
Run time
7.8 s
Output
7.9 MB
Proof
The same frame at five file sizes
A 960x410 crop at full resolution, 6 s into the clip, where stone and wood texture shows the difference. Each label names the setting, the figure is the whole file.
in.mp4
34.6 MBThe input, for reference. Every score on this page is measured against it.
-crf 28Hard to tell apart at a glance
11.9 MBVMAF 93.5. Close to the input at a third of the size. The stone texture holds.
-b:v 1424k, two-passSofter, still clean
7.9 MBVMAF 89.7. The 8 MB target. The girl, who is moving, smears a little. The still stonework holds.
-b:v 400k at 1920x818Blocky
2.6 MBVMAF 55.1. Too few bits for 1.6 million pixels a frame. The girl and the ground break into blocks.
-b:v 400k at 854x364Blurry, not blocky
2.5 MBVMAF 66.5. Same bitrate, a fifth of the pixels. Shown here scaled back to 1920 wide.
Returns immediately with a job id. Poll GET /jobs/{id} or register a webhook rather than blocking on the request.
Every variant
Pick the command for your case
Each one ran on the same input. The line under it is what came out. Run times are the best of three runs with FFmpeg 8.0 on one desktop machine, so read them as relative.
Compress with little visible loss
Re-encode
ffmpeg-iin.mp4-c:vlibx264-crf23-c:acopyout.mp4
21.5 MB, 62% of the input. VMAF 97.7. CRF 23 is libx264's default when you give no -crf.
854x364, 2.5 MB, VMAF 66.5. At this size, fewer pixels look better than more. Compare the last two pictures above.
3.4 s run2.5 MB40.00 s long
Gotchas
Where it goes wrong
Which CRF should I use?
Each step of 5 on the CRF scale cut the file by 40% to 45% on the test clip. Same input, -preset medium, scored against the input:
CRF 18: 35.8 MB, VMAF 99.3. Bigger than the 34.6 MB input.
CRF 23: 21.5 MB, VMAF 97.7.
CRF 28: 11.9 MB, VMAF 93.5.
CRF 33: 6.9 MB, VMAF 84.3.
CRF 18 is often called visually lossless. On a file that is already compressed it buys nothing. It came out bigger than the input and still scored below 100, because a re-encode can only lose quality.
Start at 23. Move to 28 if the file is still too big, and stop before 33, where VMAF fell below 85.
Then take the target in kilobits, divide by the seconds and subtract the audio bitrate. 8 MB is 64,000 kilobits. 64,000 / 40 = 1600k in total, minus 128k audio leaves 1472k for video.
That exact figure missed. Both encoders overshot their average a little:
8,154,523 bytes, 1.9% over 8 MB. Video came out at 1495k and AAC at 130k. With 3% taken off the total (1424k video) the file was 7,912,998 bytes, under the line.
The first pass reads the whole clip so the second can spend bits where the picture needs them. -f null - throws the first pass away without a platform path like /dev/null or NUL.
Two-pass hits the size and says nothing about how the file looks. Hitting a target file size with a quality floor walks through a CRF search that keeps the size and scores each candidate with VMAF.
Does a slower preset make a smaller file?
At the same CRF the presets do not give the same quality, so compare size and score together. CRF 23 on the test clip:
ultrafast: 64.0 MB, VMAF 97.1. Almost twice the input.
veryfast: 17.8 MB, VMAF 94.6. Smaller, but it scored lower, so the saving came from quality.
medium (the default): 21.5 MB, VMAF 97.7.
slow: 21.0 MB, VMAF 97.7.
veryslow: 19.9 MB, VMAF 97.7. 7% smaller than medium at the same score.
Slower presets bought a few percent at equal quality and took much longer. ultrafast made the biggest file of all, so never use it to make a file smaller.
All nine x264 presets at CRF 23 shows the same ultrafast cliff on a second clip, and that the middle presets do not shrink in order of effort.
Should I lower the resolution to hit a size?
Only when the budget is tight. At 1472k, about the 8 MB target, full 1920 width scored VMAF 90.2 and the same bitrate at 1280 wide scored 89.2, both scaled back to 1920 to compare. The full-size picture won.
At 400k it flipped. Full width scored 55.1 and broke into blocks, 1280 wide scored 65.0, and 854 wide scored 66.5. On this clip the switch came somewhere between 2.6 MB and 8 MB for 40 s. For a small target, encode both sizes and compare.