Convert HLG to Rec 709 Using FFmpeg Tonemap

This article provides a straightforward guide on how to convert High Log-Gamma (HLG) HDR video to the standard Rec. 709 SDR format using FFmpeg’s tonemap filter. You will learn the exact command-line syntax and the roles of the specific filters required to achieve accurate color mapping without washing out your video.

To map HLG video to Rec. 709, you must convert the video’s color space and scale its high dynamic range down to standard dynamic range. This is best achieved by combining FFmpeg’s zscale filter (which requires FFmpeg to be compiled with --enable-libzimg) and the tonemap filter.

The FFmpeg Command

Run the following command in your terminal to perform the conversion:

ffmpeg -i input_hlg.mp4 -vf "zscale=transfer=linear,tonemap=tonemap=hable,zscale=transfer=bt709:primaries=bt709:matrix=bt709,format=yuv420p" -c:v libx264 -crf 18 -c:a copy output_rec709.mp4

How the Filter Chain Works

The conversion process is executed sequentially inside the video filter (-vf) argument:

  1. zscale=transfer=linear: The tonemap filter requires a linear light input to calculate brightness adjustments accurately. This step temporarily converts the HLG transfer characteristics into a linear color space.
  2. tonemap=tonemap=hable: This applies the Hable tone-mapping algorithm to compress the high dynamic range of the HLG source into a standard range. You can replace hable with other algorithms like mobius, reinhard, or linear depending on your visual preference.
  3. zscale=transfer=bt709:primaries=bt709:matrix=bt709: Once the brightness is tone-mapped, this step converts the linear light back into the standard Rec. 709 (bt709) transfer curve, color primaries, and matrix coefficients.
  4. format=yuv420p: This ensures the output pixel format is set to YUV 4:2:0 8-bit, which is the standard format for maximum compatibility across consumer playback devices and web browsers.

Additional Encoding Parameters