Configure libx264 Psycho-Visual RD Strength in FFmpeg

Adjusting the psycho-visual rate-distortion (psy-RD) strength in FFmpeg’s libx264 encoder allows you to control how the encoder prioritizes perceived visual quality—such as detail and texture retention—over mathematical compression metrics. This guide explains how to use the -psy-rd parameter, detailing its syntax, the function of its two primary components, and practical examples for different video types.

Understanding the -psy-rd Parameter

The psycho-visual rate-distortion setting is configured in FFmpeg using the -psy-rd flag. This option accepts two float values separated by a colon:

-psy-rd <psy-rd-strength>:<psy-trellis-strength>

By default, the libx264 encoder applies a setting of 1.0:0.0 for most standard presets. Depending on your source material, you can override these defaults to optimize your output:

1. High-Detail and Film Grain (Film Tune)

For movies with natural film grain or highly detailed textures, you want a strong psy-RD setting to prevent the encoder from smoothing out the image. * Recommended Setting: 1.0:0.15 * FFmpeg Command: bash ffmpeg -i input.mp4 -c:v libx264 -crf 20 -psy-rd 1.0:0.15 output.mp4

2. Animation and Anime (Flat Graphics)

Animation contains flat color gradients and sharp lines. High psy-RD values can cause ringing artifacts around lines. Lowering both values produces cleaner, smoother gradients. * Recommended Setting: 0.4:0.0 * FFmpeg Command: bash ffmpeg -i input.mp4 -c:v libx264 -crf 20 -psy-rd 0.4:0.0 output.mp4

3. Disabling Psycho-Visual Optimizations

If your goal is pure mathematical encoding efficiency (such as testing peak signal-to-noise ratio / PSNR metrics), you can disable psycho-visual optimizations entirely. * Recommended Setting: 0.0:0.0 (Note: This is automatically done if you use -tune psnr). * FFmpeg Command: bash ffmpeg -i input.mp4 -c:v libx264 -crf 20 -psy-rd 0.0:0.0 output.mp4

Interaction with the -tune Flag

FFmpeg’s libx264 has built-in tuning profiles (via the -tune option) that automatically set these parameters for you. For example, -tune film sets -psy-rd to 1.0:0.15, while -tune animation adjusts it to 0.4:0.0.

If you define both -tune and -psy-rd in the same command, the values specified in the explicit -psy-rd flag will override the tuning profile’s defaults, allowing you to fine-tune the preset configurations.