Transcode Video with FFmpeg libx265 Custom Tuning
Transcoding videos using the High Efficiency Video Coding (HEVC)
standard with the libx265 encoder in FFmpeg allows for
superior compression and quality. This article provides a
straightforward guide on how to configure FFmpeg to transcode video
files using libx265 while applying custom tuning options to
optimize the output for specific content types, such as animation, film,
or fast-paced action.
Basic FFmpeg Command with libx265
To transcode a video using the libx265 encoder, use the
following basic command structure:
ffmpeg -i input.mp4 -c:v libx265 -crf 22 -preset medium output.mp4-c:v libx265: Specifies the H.265/HEVC video encoder.-crf 22: Sets the Constant Rate Factor (CRF) to control quality. Lower numbers mean higher quality (typically 18–28 is recommended).-preset medium: Controls the trade-off between encoding speed and compression efficiency.
Using Built-in libx265 Tuning Options
The libx265 encoder provides built-in tuning options
(-tune) to optimize the encoder for specific types of
source material. You can apply these by adding the -tune
flag to your command:
ffmpeg -i input.mp4 -c:v libx265 -crf 20 -preset slow -tune grain output.mp4Below are the most common built-in tuning options:
grain: Preserves natural film grain and high-frequency noise. Use this for older movies or film scans.animation: Optimizes settings for hand-drawn or computer-generated 2D animation by reducing banding and improving flat-color areas.zero-latency: Minimizes encoding delay, which is ideal for real-time streaming or video conferencing.fast-decode: Disables complex encoding tools to make the final video easier to decode on low-power devices.psnr/ssim: Optimizes the encoder for mathematical benchmark metrics rather than visual quality (not recommended for general viewing).
Custom Tuning with
-x265-params
For precise control, you can bypass or supplement the built-in
presets by passing custom parameters directly to the underlying
libx265 library using the -x265-params flag.
Multiple parameters are separated by colons (:).
For example, to prevent blockiness in dark scenes and disable Sample Adaptive Offset (SAO), which can sometimes cause blurriness, use the following command:
ffmpeg -i input.mp4 -c:v libx265 -crf 21 -preset slow -x265-params aq-mode=3:no-sao=1 output.mp4Highly Effective Custom Parameters:
no-sao=1: Disables Sample Adaptive Offset. While SAO reduces file size, it often introduces a “plastic” or blurry look. Disabling it retains sharper details.aq-mode=3: Enables Adaptive Quantization Mode 3 (bias to dark scenes). This prevents color banding and blocking in dark gradients.deblock=-1:-1: Adjusts the in-loop deblocking filter. Setting this to negative values (e.g.,-1:-1or-2:-2) increases sharpness at the expense of potential blockiness in low-bitrate encodes.limit-sao=1: A middle ground for SAO that limits its application to areas where it does not destroy fine detail.