H.265 (HEVC) is a video compression standard that achieves similar visual quality to H.264 at significantly lower bitrates. It uses larger coding units, flexible block partitioning, and advanced filters to improve compression, especially for high-resolution content.
HEVC supports multiple profiles, bit depths, and rate control modes suited for various workflows. Encoding requires more processing power and careful configuration for optimal result.
Codec Structure and Compression Improvements
H.265 achieves higher compression efficiency through deeper prediction hierarchies, larger coding units, and more flexible block partitioning.
Unlike H.264, which uses fixed 16??16 macroblocks, H.265 employs Coding Tree Units (CTUs) with sizes up to 64??64. These CTUs are subdivided into Coding Units (CUs) and Prediction Units (PUs) for motion compensation. The transform step supports both DST and DCT-II, with variable transform unit sizes (4??4 to 32??32), allowing fine-grained control over spatial frequency retention.
Motion vector prediction in HEVC is enhanced with merge candidates, temporal motion vector prediction, and quarter-pixel accuracy. In-loop filtering is performed using Sample Adaptive Offset (SAO) and Deblocking Filter (DBF) to reduce artifacts and preserve edge detail.
Rate Control and Encoder Modes
Like H.264, H.265 supports multiple rate control modes. When using the libx265 encoder in FFmpeg, the most common approaches are:
CRF Mode (Constant Rate Factor)
This is used for variable bitrate encoding with consistent perceptual quality.
ffmpeg -i input.mp4 -c:v libx265 -preset slow -crf 28 output_hevc_crf28.mp4Explanation:
- -i input.mp4: Specifies the input video file.
- -c:v libx265: Uses the H.265 (HEVC) encoder.
- -preset slow: Controls encoder speed vs compression efficiency; → slow → gives better results.
- -crf 28: Sets the Constant Rate Factor; lower values increase quality and file size.
Two-Pass VBR Mode
For more precise bitrate targeting, especially in streaming applications.
ffmpeg -y -i input.mp4 -c:v libx265 -b:v 3000k -x265-params pass=1 -f null /dev/nullffmpeg -i input.mp4 -c:v libx265 -b:v 3000k -x265-params pass=2 output_vbr.mp4Explanation:
- -b:v 3000k: Targets an average bitrate of 3 Mbps.
- -x265-params pass=1: First pass analyzes scene complexity and stores stats.
- -x265-params pass=2: Second pass performs the actual encoding using collected data.
- Two-pass mode allows more consistent bitrate allocation across complex scenes.
Profile, Level, and Tier Configuration
HEVC defines multiple profiles, including Main, Main10, and MainStillPicture. The Main profile supports 8-bit 4:2:0 video, while Main10 adds support for 10-bit encoding, often used in HDR and professional workflows.
To enforce Main10 profile encoding in FFmpeg:
ffmpeg -i input.mp4 -c:v libx265 -pix_fmt yuv420p10le -profile:v main10 -crf 26 output_main10.mp4Explantation:
- -pix_fmt yuv420p10le: Enables 10-bit color depth with 4:2:0 chroma subsampling.
- -profile:v main10: Specifies the Main10 profile (used for HDR or high-fidelity encoding).
- -crf 26: Balances file size and quality for 10-bit content.
Levels in HEVC constrain frame rate, resolution, and bitrate. The Tier defines decoder complexity → Main Tier for general use, High Tier for higher bitrates (e.g., broadcast-grade content).
Compression Efficiency Compared to H.264
HEVC provides 40"50% bitrate savings compared to H.264 for the same perceptual quality, particularly at 4K and higher resolutions. It handles high spatial complexity and smooth gradients more effectively due to its ability to encode larger homogeneous regions with fewer bits.
However, this comes at the cost of computational complexity. Decoding and encoding require more processing power and memory. For live encoding, this necessitates hardware acceleration or tuned software pipelines.
GOP and Keyframe Configuration
HEVC allows longer GOPs than H.264 due to better reference frame compression. A typical setup for 30fps content:
ffmpeg -i input.mp4 -c:v libx265 -g 60 -keyint_min 60 -sc_threshold 0 -crf 28 output_gop60.mp4Explanation:
- -g: Maximum GOP size.
- -keyint_min: Enforces a minimum interval for I-frames.
- -sc_threshold: Scene change threshold; set to 0 to disable automatic I-frame insertion.
For segment-aligned delivery (e.g., HLS or DASH), use fixed GOP intervals to simplify muxing.
Real-Time and Hardware-Accelerated HEVC Encoding
Software encoding with libx265 is resource-intensive and often unsuitable for live use. Hardware encoders like NVENC, Intel Quick Sync, or Apple VideoToolbox provide real-time HEVC encoding.
Example: NVIDIA NVENC (FFmpeg)
ffmpeg -i input.mp4 -c:v hevc_nvenc -preset p5 -b:v 4000k -rc:v vbr -maxrate 4500k -bufsize 6000k output_nvenc.mp4Explanation:
- -c:v hevc_nvenc: Uses NVIDIA"s hardware encoder for H.265.
- -preset p5: Medium-quality preset (range: p1 to p7).
- -rc:v vbr: Variable Bitrate mode.
- -maxrate and -bufsize: Control rate smoothing and encoder buffering.
Example: Intel QSV
ffmpeg -hwaccel qsv -c:v h264_qsv -i input.mp4 -c:v hevc_qsv -b:v 2500k output_qsv.mp4Explanation:
- -hwaccel qsv: Enables Quick Sync Video acceleration.
- -c:v h264_qsv: Decodes input using Quick Sync H.264.
- -c:v hevc_qsv: Encodes using Quick Sync HEVC at 2.5 Mbps.
Playback and Compatibility Constraints
Although HEVC is supported by modern browsers (Safari, Edge, Chrome with MediaSource), compatibility is not universal. Older devices and some browsers (e.g., Firefox on non-HDR setups) lack native support.
When targeting wide compatibility:
- Consider fallback options like H.264 or AV1.
- Avoid using 10-bit unless targeting high-end displays or HDR pipelines.
- Always set pixel format to yuv420p for baseline hardware support unless 10-bit color is required.
To maximize compatibility:
ffmpeg -i input.mp4 -c:v libx265 -pix_fmt yuv420p -crf 28 -profile:v main output_compatible_hevc.mp4Explanation:
- -pix_fmt yuv420p: Uses 8-bit color for maximum compatibility.
- -profile:v main: Enforces the Main profile (baseline for most decoders).
- -crf 28: Applies balanced quality and file size for general-purpose viewing.
Usage Guidelines
1. Use CRF mode for general encoding
CRF (Constant Rate Factor) mode is ideal for achieving a balance between video quality and file size. Lower CRF values give better quality but larger files. A CRF of 24"28 works well for most uses.
ffmpeg -i input.mp4 -c:v libx265 -crf 26 output.mp42. Use 10-bit encoding for HDR or high-quality videos
For HDR content or advanced post-production, 10-bit encoding preserves more color detail. This is useful in professional workflows or for content with gradients.
ffmpeg -i input.mp4 -c:v libx265 -pix_fmt yuv420p10le -profile:v main10 output_hdr.mp43.Use two-pass encoding for fixed bitrate needs
Two-pass encoding gives better quality at a set bitrate, which is useful for streaming platforms. The first pass analyzes, and the second encodes based on the analysis.
# Pass 1ffmpeg -i input.mp4 -c:v libx265 -b:v 3000k -x265-params pass=1 -f null NUL# Pass 2ffmpeg -i input.mp4 -c:v libx265 -b:v 3000k -x265-params pass=2 output.mp44. Use hardware acceleration for faster encoding
Hardware encoders like NVENC or Intel QSV speed up HEVC encoding using your GPU. This is useful for real-time encoding or faster processing of large files.
ffmpeg -i input.mp4 -c:v hevc_nvenc output_nvenc.mp45. Ensure compatibility for wider playback
To make sure the video works on most devices and browsers, use 8-bit color depth and the Main profile. Avoid 10-bit unless absolutely needed.
ffmpeg -i input.mp4 -c:v libx265 -pix_fmt yuv420p -profile:v main output_compat
