x265 Encoder: Complete Guide to HEVC Video Compression
What x265 is
x265 is an open-source software library that encodes video into the HEVC/H.265 format. It focuses on achieving significantly better compression (smaller file sizes at comparable quality) than AVC/H.264, especially at higher resolutions (1080p, 4K).
Key benefits
- Higher compression efficiency: ~25–50% smaller files than H.264 for similar perceptual quality in many cases.
- Better support for high resolutions: Efficient at 4K and above.
- Wide encoder options: Numerous presets and tuning options for balancing speed vs. quality.
- Active development: Regular updates and improvements from the open-source community.
Core concepts and settings
- CRF (Constant Rate Factor): Primary quality-based mode. Lower CRF = higher quality and larger files. Typical ranges: 18–28 (18 ≈ visually lossless, 23 default equivalent).
- bitrate (CBR/VBR): Targeting a specific bitrate for streaming or constrained storage. VBR lets the encoder allocate bits where needed.
- Presets: Trade encoding speed for compression efficiency. Common presets: ultrafast → placebo. Faster presets encode quickly with worse compression; slower presets take much longer but produce smaller files for the same quality.
- Tune: Profiles for specific content: film, animation, grain, psnr, ssim.
- Profiles & levels: Compatibility constraints for playback devices (Main/Main10 profile; level indicates max resolution/bitrate). Use Main10 for 10-bit color.
- Psy tools: Psychovisual optimizations (psy-rd, psy-rdoq) that improve perceived quality at given bitrates.
- B-frames, reference frames, GOP: Temporal compression controls affecting quality and encode time. Longer GOPs and more references improve compression but increase CPU and reduce fast-forward accuracy.
Typical workflows
- Choose encoding mode: CRF for general-purpose, VBR/target bitrate for streaming.
- Select preset based on available CPU/time (e.g., slow/medium for good balance).
- Set CRF (e.g., 18–22 for high quality; 22–26 for web). For 10-bit source, use Main10 profile and adjust accordingly.
- Tune for the content type (animation vs. live-action).
- Optionally enable grain-preserving or film tuning for archival.
- Validate with visual checks and objective metrics (PSNR/SSIM/VMAF) if needed.
Encoding examples (ffmpeg)
- CRF encode (good balance):
bash
ffmpeg -i input.mp4 -c:v libx265 -crf 23 -preset medium -c:a copy output.mp4
- 10-bit encode:
bash
ffmpeg -i input.mp4 -c:v libx265 -pix_fmt yuv420p10le -crf 23 -preset slow -c:a copy output10bit.mp4
- Target bitrate (2 Mbps VBR with 2-pass):
bash
ffmpeg -y -i input.mp4 -c:v libx265 -b:v 2M -x265-params pass=1 -an -f null /dev/null ffmpeg -i input.mp4 -c:v libx265 -b:v 2M -x265-params pass=2 -c:a aac -b:a 128k output.mp4
Compatibility and playback
- HEVC support varies by device and OS. Newer TVs, mobile devices, and modern players support HEVC; older hardware may not.
- Consider providing an H.264 fallback for broad compatibility, or use adaptive streaming (HLS/DASH) with multiple codecs.
Performance tips
- Use slower presets when encoding large libraries offline for best storage savings.
- Use hardware decoding/encoding (NVENC/QuickSync) for realtime needs—note that hardware encoders usually offer worse quality-per-bit than x265 CPU encode.
- For batch processing, parallelize per-file jobs rather than multithreading a single file excessively.
When to use x265
- Delivering 4K/UHD or HDR content where bitrate savings matter.
- Archival of high-quality source material.
- Bandwidth-constrained streaming where reduced bitrate lowers delivery costs.
Limitations
- Higher CPU encoding time than x264.
- Licensing/royalty considerations for commercial distribution (HEVC patents).
- Playback compatibility is not universal.
If you want, I can suggest optimal CRF/preset combos for specific targets (web streaming, mobile, archival) or generate ffmpeg commands tuned to your source and goals.
Leave a Reply