Configure libx265 Tune in FFmpeg
This article explains how to configure the tune
parameter in FFmpeg when encoding video with the libx265
(HEVC) encoder. You will learn the correct command-line syntax, the
available tuning options—such as grain, animation, and zero-latency—and
how to apply these settings to optimize your video output based on the
specific type of source content you are compressing.
Understanding the Tune Parameter
The -tune parameter in libx265 optimizes
the encoder’s settings for specific types of video content or use cases.
By adjusting internal settings like deblocking filters, psychovisual
rates, and QP offsets, tuning ensures the highest possible visual
quality or performance for your specific media.
There are two primary ways to specify the tune parameter in FFmpeg:
Using the direct
-tuneflag:ffmpeg -i input.mp4 -c:v libx265 -crf 22 -tune grain output.mp4Using the
-x265-paramsflag:ffmpeg -i input.mp4 -c:v libx265 -crf 22 -x265-params tune=grain output.mp4
Available Tune Options for libx265
The libx265 encoder supports several tuning options.
Each option is tailored for a specific scenario:
grain: Optimized for material with significant film grain. It preserves the grain structure by preventing the encoder from smoothing it out, though it requires a higher bitrate.animation: Optimized for hand-drawn anime and cartoons. It improves quality for flat color areas and sharp edges by reducing deblocking and increasing reference frames.zero-latency: Crucial for live streaming and real-time communication. It removes the frame buffer latency by disabling B-frames and frame reordering, ensuring instant encoding and transmission.fast-decode: Optimizes the bitstream for easier decoding on older or low-power playback devices by disabling certain complex encoding tools (like loop filters).psnr/ssim: Used primarily for video quality benchmarking. They optimize the video for objective metric scores rather than subjective human sight.
Practical Examples
Encoding Film with Preserved Grain
To compress a movie while keeping the original cinematic film grain intact:
ffmpeg -i movie.mkv -c:v libx265 -crf 20 -tune grain output.mkvEncoding Animated Content
To encode cartoons or anime to keep edges sharp and colors clean:
ffmpeg -i anime.mp4 -c:v libx265 -crf 18 -tune animation output.mp4Low-Latency Live Streaming
To configure a stream with the lowest possible delay:
ffmpeg -i input.mp4 -c:v libx265 -tune zero-latency -f mpegts udp://127.0.0.1:1234