Compress a video with FFmpeg

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.

Smaller Re-encode

You want a smaller file and the exact size does not matter.

ffmpeg -i in.mp4 -c:v libx264 -crf 28 -c:a copy out.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 size Re-encode

The file has to fit an upload or attachment limit.

ffmpeg -y -i in.mp4 -c:v libx264 -b:v 1424k -pass 1 -an -f null - && ffmpeg -y -i in.mp4 -c:v libx264 -b:v 1424k -pass 2 -c:a aac -b:a 128k out.mp4

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
Alley with stone blocks and the girl, from the uncompressed input
34.6 MB The input, for reference. Every score on this page is measured against it.
-crf 28 Hard to tell apart at a glance
The same alley frame after CRF 28, texture on the stones mostly intact
11.9 MB VMAF 93.5. Close to the input at a third of the size. The stone texture holds.
-b:v 1424k, two-pass Softer, still clean
The same alley frame after two-pass at 1424k, slightly softer stone texture
7.9 MB VMAF 89.7. The 8 MB target. The girl, who is moving, smears a little. The still stonework holds.
-b:v 400k at 1920x818 Blocky
The same alley frame at 400k and full 1920 width, blocky smears across the girl and the ground
2.6 MB VMAF 55.1. Too few bits for 1.6 million pixels a frame. The girl and the ground break into blocks.
-b:v 400k at 854x364 Blurry, not blocky
The same alley frame at 400k after a downscale to 854 wide, blurry but without blocks
2.5 MB VMAF 66.5. Same bitrate, a fifth of the pixels. Shown here scaled back to 1920 wide.

Footage: Sintel, © Blender Foundation, CC BY 3.0.

Run it

Run it on Rendobar

Send the same command from your code. The job fetches each input from its URL and returns the output file's URL when it finishes.

job.ts
import { createClient } from "@rendobar/sdk";
const rb = createClient({ apiKey: process.env.RENDOBAR_API_KEY });
const job = await rb.jobs.run({
type: "ffmpeg",
inputs: {
"in.mp4": "https://cdn.rendobar.com/examples/compress-video/in.mp4",
},
params: {
command: "ffmpeg -i in.mp4 -c:v libx264 -crf 28 -c:a copy out.mp4",
},
});
console.log(job.output.file.url);

Install with npm i @rendobar/sdk. jobs.run() submits and waits, so it returns the finished job in one call.

terminal
curl https://api.rendobar.com/jobs \
-H "Authorization: Bearer $RENDOBAR_API_KEY" \
-H "Content-Type: application/json" \
-d '{
"type": "ffmpeg",
"inputs": {
"in.mp4": "https://cdn.rendobar.com/examples/compress-video/in.mp4"
},
"params": {
"command": "ffmpeg -i in.mp4 -c:v libx264 -crf 28 -c:a copy out.mp4"
}
}'

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.

  1. Compress with little visible loss

    Re-encode
    ffmpeg -i in.mp4 -c:v libx264 -crf 23 -c:a copy out.mp4

    21.5 MB, 62% of the input. VMAF 97.7. CRF 23 is libx264's default when you give no -crf.

    7.8 s run 21.5 MB 40.00 s long

  2. Compress to under 25 MB

    Re-encode
    ffmpeg -y -i in.mp4 -c:v libx264 -b:v 4722k -pass 1 -an -f null - && ffmpeg -y -i in.mp4 -c:v libx264 -b:v 4722k -pass 2 -c:a aac -b:a 128k out.mp4

    24.5 MB, VMAF 98.4. Same math as the 8 MB command. 25 MB over 40 s, cut by 3%, minus 128k.

    9.8 s run 24.5 MB 40.00 s long

  3. Compress smaller with H.265

    Re-encode
    ffmpeg -i in.mp4 -c:v libx265 -crf 28 -tag:v hvc1 -c:a copy out.mp4

    9.2 MB at VMAF 94.3. Smaller than H.264 CRF 28 and scored higher. -tag:v hvc1 writes the hvc1 tag (why it matters). Without it FFmpeg 8.0 wrote hev1.

    13.8 s run 9.2 MB 40.00 s long

  4. Downscale to 1280 and compress

    Re-encode
    ffmpeg -i in.mp4 -vf scale=1280:-2 -c:v libx264 -crf 28 -c:a copy out.mp4

    1280x546, 5.9 MB. VMAF 83.7 measured at 1920, close to CRF 33 at full size (6.9 MB, VMAF 84.3).

    3.6 s run 5.9 MB 40.00 s long

  5. Make it tiny, under 3 MB

    Re-encode
    ffmpeg -y -i in.mp4 -vf scale=854:-2 -c:v libx264 -b:v 400k -pass 1 -an -f null - && ffmpeg -y -i in.mp4 -vf scale=854:-2 -c:v libx264 -b:v 400k -pass 2 -c:a aac -b:a 96k out.mp4

    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 run 2.5 MB 40.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.

How much each CRF step saves runs CRF 18 to 32 on a second clip and times each encode.

How do I compress to a specific file size?

CRF cannot promise a size, so give the encoder a bitrate instead. Get the duration first:

ffprobe -v error -show_entries format=duration -of csv=p=0 in.mp4

40.000000

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:

ffmpeg -y -i in.mp4 -c:v libx264 -b:v 1472k -pass 1 -an -f null - && ffmpeg -y -i in.mp4 -c:v libx264 -b:v 1472k -pass 2 -c:a aac -b:a 128k out.mp4

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.

Related: Resize a video, Encode H.264 and H.265, Web-playable MP4.

Your command. Our infrastructure.

Bring your own commands. Run them on scalable CPU and GPU infrastructure, and pay only for compute time.