Ultimate Guide to the x264 Video Codec: Features, Settings, and Best Practices

Tuning x264: Optimal Presets and Parameters for Quality vs. Speedx264 remains one of the most widely used H.264/AVC encoders because it offers a rich set of tunable parameters that let users control the tradeoff between visual quality, encoding speed, and output bitrate. This article explains how the most important x264 options work, gives practical presets and parameter sets for common workflows, and shows how to approach tuning for optimal quality-per-bit and encoding efficiency.


Overview: What x264 tuning means

x264 exposes two primary axes you can tune:

  • Encoding speed (how long it takes to produce the file). Faster = less CPU time.
  • Compression efficiency / visual quality (how good the result looks at a given bitrate). Slower = usually better compression.

x264 organizes many internal decisions into two high-level mechanisms:

  • Presets: coarse-grain bundles of many parameters (ultrafast → placebo).
  • Individual parameters: fine-grain controls such as motion estimation method, reference frames, ratecontrol options, and macroblock-level decisions.

A good workflow uses a preset as a starting point and then adjusts a few specific parameters to suit content and constraints.


How presets work

Presets in x264 (e.g., ultrafast, superfast, veryfast, faster, fast, medium, slow, slower, veryslow, placebo) are shorthand collections that alter dozens of internal knobs. Lower presets (ultrafast → veryfast) favor speed by disabling/relaxing expensive analysis (e.g., fewer ME iterations, simpler RDO decisions, smaller motion search range). Higher presets (slow → placebo) enable deep analysis (multiple ME ranges, thorough RDO, more reference frames, trellis quantization) to improve compression.

  • Use veryfast–fast for real-time or near real-time encoding (streaming, live).
  • Use medium for a balance between speed and quality (default).
  • Use slow–veryslow when encoding time is less critical and file size/quality matters (archival, final encodes).
  • Placebo yields negligible additional quality versus veryslow/slower but with massive slowdowns—avoid for practical use.

Key x264 parameters and their effects

Below are the parameters you’ll most commonly touch, grouped by their impact.

Rate control and quality-targeting
  • CRF (Constant Rate Factor): Primary quality-based mode. Lower CRF → higher quality + larger file. Common CRF range:
    • 18–22 for high-quality general-purpose video (crf=18 often near visually lossless for many sources).
    • 20–23 good balance for streaming and consumer content.
  • bitrate (ABR/CBR): Use when output must meet an explicit bitrate (streaming constraints, broadcast).
  • tune: content-specific optimizations: examples include film, animation, grain, psnr, ssim. Use:
    • --tune film for natural motion/film grain,
    • --tune animation for cartoons,
    • --tune grain to preserve noise/grain (keeps higher data for grainy areas).
  • --preset: sets many defaults including ME method.
  • --me (umh / esa / esa / hex / tesa / dia): more thorough ME yields better quality at cost of speed. Common choices:
    • dia (diamond) — fast, default for real-time.
    • hex — good balance.
    • umh (uneven multi-hexagon) — slower, better for low-bitrate quality.
  • --subme (0–11): sub-pixel ME/optimization level. Higher values improve detail prediction; common picks:
    • 6–7 for faster encodes,
    • 8–10 for high quality,
    • 9–11 for veryslow/preset-placebo workflows.
  • --rc-lookahead: frames to look ahead for rate-control / B-frame decisions. Increasing helps quality but costs CPU/memory.
Prediction, references, and B-frames
  • --ref (number of reference frames): More refs improve compression but increase decode complexity and encoding time. 3–8 is common; older devices may prefer <=4.
  • --bframes: B-frame count (0–16). Using 2–4 B-frames is a standard quality/compatibility tradeoff.
  • --b-adapt (0/1/2): adaptive B-frame selection. 2 = full; recommended for quality.
  • --weightp / --weightb: weighted prediction for P- and B-frames; useful for fades/crossfades.
Rate-distortion, trellis, and psychovisual
  • --aq-mode and --aq-strength: Adaptive quantization distributes bits to improve perceived quality (foreground vs. background). Default aq-mode=1 often good; strengthen moderately for complex scenes.
  • --trellis (0–2): post-quantization R-D optimization. 1–2 improves PSNR/visual quality at cost of speed.
  • --psy / --psy-rd: psychovisual tuning that tries to match perceived quality.
  • --deblock and --no-deblock: filtering to reduce blocking; typically leave default but adjust for strong sharpness or when preserving film texture.
Misc and compatibility
  • --profile (baseline/main/high): choose based on playback devices. High profile allows more features (CABAC, 8×8 transforms) but may have compatibility tradeoffs.
  • --level (e.g., 3.1, 4.0): controls decoder resource guarantees.
  • --vbv-maxrate / --vbv-bufsize: enforce bitrate and buffer constraints for streaming/compat devices.

Practical parameter sets

Below are starting points you can copy and adjust. Replace input/output names and add ffmpeg container options as needed.

  1. Fast real-time streaming (low-latency)
  • Use when you must prioritize speed and low latency (game capture, live).
  • Recommended ffmpeg example:
    
    ffmpeg -i in.mp4 -c:v libx264 -preset veryfast -tune zerolatency -crf 23 -g 50 -bf 2 -b:v 4000k -maxrate 4000k -bufsize 8000k out.mp4 

    Key choices: veryfast, tune zerolatency, moderate CRF, GOP size tuned to fps, VBV to limit spikes.

  1. Balanced (good quality, reasonable encode time)
  • Good default for uploads, VOD.
    
    ffmpeg -i in.mp4 -c:v libx264 -preset medium -crf 20 -tune film -b:v 0 -profile:v high -level 4.0 -bf 3 -ref 4 -aq-mode 1 -aq-strength 1.0 out.mp4 

    This gives a solid compromise of speed and compression efficiency.

  1. High-quality offline encode (archival / final master)
  • Use when encode time is not critical.
    
    ffmpeg -i in.mov -c:v libx264 -preset veryslow -crf 18 -tune film -profile:v high -level 4.1 -ref 6 -bf 5 -b-adapt 2 -me umh -subme 10 -trellis 2 -aq-mode 3 -aq-strength 1.2 -rc-lookahead 60 out.mkv 

    Enables UMH motion search, high subme, trellis, stronger AQ — much slower but best compression for given quality.

  1. Preserve grain/noise
    
    ffmpeg -i in.mov -c:v libx264 -preset slow -crf 18 -tune grain -aq-mode 2 -aq-strength 1.3 -deblock 0:0 out.mkv 

    Use tune grain and increase AQ; disabling strong deblocking keeps grain.


Optimization strategy: find the best quality vs. speed point

  1. Define constraints: target device compatibility (profile/level), max encode time, max bitrate/file size, perceptual priorities (grain, text, animation).
  2. Start with a preset appropriate to time constraints (medium for general, slow for quality).
  3. Choose CRF for desired quality; encode short representative clips at different CRFs to find the CRF that meets quality/size expectations.
  4. If speed is a problem, progressively move to a faster preset or reduce quality-specific knobs: lower subme, reduce refs, switch ME to hex/dia, disable trellis.
  5. If you need more efficiency (smaller file for same visual quality), move to slower presets and increase subme/refs/trellis/aq.
  6. Use objective metrics (PSNR/SSIM/VMAF) plus visual inspection — VMAF correlates well with perceived quality for many scenes.

Quick reference table: common tradeoffs

Goal Preset Key params to raise Key params to lower
Max speed / low latency veryfast / superfast dia ME, subme 2–4, refs 1–2, bframes 0–2 trellis, umh, subme, ref
Balanced quality/speed medium subme 6–8, ref 3–4, bframes 2–4, aq-mode 1 avoid placebo/very slow
High quality / offline slow → veryslow umh ME, subme 9–10, ref 6–8, trellis 2, aq-mode 2–3, rc-lookahead 40–60 use fewer speed-saving shortcuts
Preserve film grain slow tune grain, aq-strength ↑, deblock mild strong deblocking, heavy denoise

Common pitfalls and tips

  • Placebo preset yields very little gain over veryslow for massively longer times—avoid unless testing.
  • Using too many reference frames or high-level features may break compatibility with low-end decoders (smartphones, older hardware).
  • For streaming, configure VBV (maxrate/bufsize) and consider CBR-like settings to avoid bitrate spikes.
  • For animation, --tune animation often outperforms film-tuned defaults because motion characteristics differ.
  • Always test with representative clips — static test clips can mislead; fast motion, grain, and scene cuts stress different encoder parts.
  • When choosing CRF, remember it’s content-dependent: fast-action scenes usually require lower (better) CRF than simple talking-head clips to reach similar perceived quality.

Measuring results: objective and subjective checks

  • Objective: PSNR, SSIM, and VMAF. Use VMAF for perceptual fidelity to human observers.
  • Subjective: Watch on target device(s) at intended viewing conditions. Look for macroblocking, blurring of fine detail, color banding, and motion artifacts.
  • Perform AB tests between candidates at equal file size (re-encode one to match bitrate/size of the other) to compare visual quality for fair evaluation.

Example workflow for tuning a single title

  1. Encode 10–30 second representative clips with presets (medium, slow, veryslow) at CRF 20, 18, 18 respectively.
  2. Compare file sizes and run VMAF to pick the best size-quality point.
  3. Adjust subme/refs/trellis if you need further small gains.
  4. For final encode, set profile/level and VBV settings for target device and run full-length encode.

Final notes

  • Start with presets; they encapsulate expert-chosen defaults. Tune only what’s necessary.
  • CRF + preset is the simplest and most effective starting point for most workflows.
  • Tradeoffs are context-dependent: faster encodes are cheaper but lose compression efficiency; slow encodes save bitrate for the same quality.
  • When in doubt, run short tests, use VMAF + visual checks, and choose the smallest set of parameter changes that deliver the needed improvement.

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *