C.W.K.
Stream
Lesson 01 of 10 · published

H.264 (libx264) — The Universal Codec

~12 min · h264, libx264, compatibility

Level 0Viewer
0 XP0/73 lessons0/15 achievements
0/100 XP to next level100 XP to go0% complete

The 'just works' codec

H.264 (a.k.a. AVC) ships in every browser, every phone, every TV, every editor, every cloud transcoder, every set-top box made in the last 15 years. When you don't know what to use, use H.264. libx264 is its open-source CPU encoder and is widely considered the gold standard for software encoding quality.

The three knobs

  • -crf — quality target. 18 ≈ visually transparent, 23 = default, 28 = web-acceptable but soft. Don't go above 28 unless you're targeting low-bandwidth.
  • -preset — speed/efficiency tradeoff. Slower presets = smaller files at the same quality. Names: ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo. slow is the sweet spot for one-off jobs.
  • -tune — content-aware optimization: film, animation, grain, stillimage, fastdecode, zerolatency. Use sparingly — defaults are usually fine.

Profiles and levels

For maximum compatibility (Apple TV, smart TVs, old Android phones), set -profile:v high -level 4.0. For 4K, you need at least -level 5.1. The encoder picks reasonable defaults; you only override when a target device chokes.

Code

The libx264 recipes you'll reuse forever·bash
# Daily upload — small, looks great
ffmpeg -i in.mp4 \
  -c:v libx264 -crf 20 -preset slow \
  -pix_fmt yuv420p \
  -c:a aac -b:a 192k \
  -movflags +faststart \
  out.mp4

# Archive master — visually transparent
ffmpeg -i in.mp4 \
  -c:v libx264 -crf 18 -preset slower \
  -pix_fmt yuv420p \
  -c:a aac -b:a 256k \
  master.mp4

# Animation tune (cleaner gradients on cartoons / motion graphics)
ffmpeg -i animation.mov \
  -c:v libx264 -crf 18 -preset slow -tune animation \
  -c:a aac -b:a 192k \
  out.mp4
Maximum compatibility (10-year-old TVs)·bash
# yuv420p + High@4.0 = plays on essentially any H.264 device
ffmpeg -i in.mp4 \
  -c:v libx264 -profile:v high -level 4.0 -crf 20 -preset slow \
  -pix_fmt yuv420p \
  -c:a aac -b:a 192k -ac 2 \
  -movflags +faststart \
  legacy.mp4

External links

Exercise

Encode the same source clip with three preset choices: ultrafast, medium, slow — all at CRF 22. Compare wall-clock time and file size. Calculate compression ratio for each. The slow preset should produce the smallest file (≈10–15% smaller than medium). Save these numbers; lesson 6 will compare against two-pass encoding.

Progress

Progress is local-only — sign in to sync across devices.
Spotted a bug or have feedback on this page?Report an Issue

Comments 0

🔔 Reply notifications (sign in)
Sign inPlease sign in to comment.

No comments yet — be the first.