Encode H.264 and H.265 with FFmpeg

For H.264, run ffmpeg -i in.mp4 -c:v libx264 -crf 23 -c:a copy out.mp4. CRF sets the quality: lower is better and bigger, and each step of 5 changed the size by 39 to 45% on our clip. For H.265 use libx265, whose default is CRF 28, and add -tag:v hvc1 so Apple players accept the file.

H.264 Re-encode

Plays on every device. CRF 23 and the default medium preset are a sound start.

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

VMAF 97.6, SSIM 0.981. 8.7 MB for 30 s of 1280x546, about a third of the CRF 14 input.

VMAF
97.6
Profile
High, yuv420p
Run time
3.0 s
Output
8.7 MB
H.265 Re-encode

Less than half the size of H.264 CRF 23, with a small quality loss. Slower to encode.

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

VMAF 94.9 at 4.1 MB. The video track is tagged hvc1. Without the flag it is hev1.

VMAF
94.9
Tag
hvc1
Run time
6.3 s
Output
4.1 MB

Proof

The same frame at four CRF values

A crop of 0:27, the baby dragon on the roof, enlarged 2x with no smoothing. Every encode used libx264 with the default preset. The figure is the file size, the verdict is the VMAF of the whole clip.
-crf 18 VMAF 99.1
Dragon close-up from the CRF 18 encode: scales and wall texture sharp
16.1 MB Scales, the stone grain and the eye highlight all hold. Nearly twice the size of CRF 23.
-crf 23 VMAF 97.6
Dragon close-up from the CRF 23 encode, close to the CRF 18 frame
8.7 MB Hard to tell from CRF 18 at this zoom, in half the bytes.
-crf 28 VMAF 93.8
Dragon close-up from the CRF 28 encode, wall texture smoothed
5.0 MB Still close to CRF 23 on a still frame. The fine grain on the stone wall is thinner.
-crf 33 VMAF 84.6
Dragon close-up from the CRF 33 encode, wall flat and scales blurred
3.0 MB The stone texture flattens and the scales on the head turn soft. Fine for a preview, not for a final file.

Footage: Sintel, © Blender Foundation, CC BY 3.0. Cut to 30 s and scaled to 1280x546 by us.

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/encode-h264/in.mp4",
},
params: {
command: "ffmpeg -i in.mp4 -c:v libx264 -crf 23 -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/encode-h264/in.mp4"
},
"params": {
"command": "ffmpeg -i in.mp4 -c:v libx264 -crf 23 -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. Encode at high quality (CRF 18)

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

    VMAF 99.1, SSIM 0.990. 1.8x the size of CRF 23.

    3.4 s run 16.1 MB 30.00 s long

  2. Encode smaller (CRF 28)

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

    VMAF 93.8, SSIM 0.965. 43% smaller than CRF 23.

    2.5 s run 5.0 MB 30.00 s long

  3. Same quality, smaller file (slower preset)

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

    VMAF 97.7 against 97.6 for medium, in a file 9.6% smaller.

    15.4 s run 7.9 MB 30.00 s long

  4. Encode as fast as possible

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

    VMAF 97.7, but 24.5 MB: 2.8x the medium file for the same quality.

    713 ms run 24.5 MB 30.00 s long

  5. Put a keyframe exactly every 2 seconds

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

    15 keyframes at 0, 2, 4 ... 28 s and no others. -g counts frames, so 48 is 2 s at 24 fps.

    3.0 s run 9.4 MB 30.00 s long

  6. Cap the bitrate for streaming

    Re-encode
    ffmpeg -i in.mp4 -c:v libx264 -crf 23 -maxrate 2M -bufsize 4M -c:a copy out.mp4

    The busiest second of video holds 3.5 Mb, against 7.8 Mb without the cap. VMAF 96.4, down from 97.6.

    2.9 s run 6.6 MB 30.00 s long

Gotchas

Where it goes wrong

Which CRF should I use?

Start at 23, the libx264 default, and move in steps of 2 or 3. On the test clip, with the default preset:

  • CRF 18: 16.1 MB, VMAF 99.1. For a master or a file you will edit again.
  • CRF 23: 8.7 MB, VMAF 97.6. The general-purpose setting.
  • CRF 28: 5.0 MB, VMAF 93.8. Detail starts to go in textured areas.
  • CRF 33: 3.0 MB, VMAF 84.6. The texture loss shows without zooming in.

libx265 uses a different scale. Its default CRF 28 gave 4.1 MB at VMAF 94.9, smaller and better than libx264 at the same number. Do not carry a CRF value from one encoder to the other.

Does a slower preset give better quality?

At the same CRF, a slower preset mostly gives a smaller file at the same quality. veryslow scored VMAF 97.7 against 97.6 for medium, in a 9.6% smaller file. ultrafast also scored 97.7 but needed 2.8x the bytes.

CRF is not exactly the same quality across presets, though:

ffmpeg -i in.mp4 -c:v libx264 -crf 23 -preset veryfast -c:a copy out.mp4

7.5 MB, 15% smaller than medium, at VMAF 95.2 instead of 97.6. The file is smaller because it is worse.

Compare presets by a quality metric, not by size at one CRF. Pick the slowest preset your time budget allows, then set CRF for the size you want.

Why won't my H.265 MP4 play in QuickTime or on an iPhone?

Check the codec tag. libx265 in MP4 is tagged hev1 by default. The FFmpeg change that added the hvc1 option says QuickTime requires hvc1. The two files we encoded differ only in that tag: same size, same video MD5.

Fixing an existing file does not need a re-encode:

ffmpeg -i hev1.mp4 -c copy -tag:v hvc1 out.mp4

Run on the hev1 file, it wrote the hvc1 version in a copy. The video packets hash the same before and after.

Why does -g 48 not give a keyframe every 2 seconds?

-g is the longest allowed gap, not a fixed interval. libx264 still adds a keyframe at each scene cut. On the test clip -g 48 gave 20 keyframes, and 9 of them fell between the 2 s marks, because each scene cut restarts the count.

Add -sc_threshold 0 to turn scene-cut keyframes off. That gave exactly 15, every 2.000 s, which is what HLS and DASH segmenters need. The cost: 9.4 MB instead of 8.7 MB at the same CRF.

What a short keyframe interval costs measures the bytes each shorter GOP adds and the flags that keep HLS segments aligned.

Related: Compress a video, Convert to WebM, Web-playable MP4, Convert to MP4.

Your command. Our infrastructure.

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