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.mp4How the Filter Chain Works
The conversion process is executed sequentially inside the video
filter (-vf) argument:
zscale=transfer=linear: Thetonemapfilter requires a linear light input to calculate brightness adjustments accurately. This step temporarily converts the HLG transfer characteristics into a linear color space.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 replacehablewith other algorithms likemobius,reinhard, orlineardepending on your visual preference.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.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
-c:v libx264: Encodes the output video using the H.264 codec.-crf 18: Sets the Constant Rate Factor to 18, balancing high visual quality with a reasonable file size.-c:a copy: Copies the audio stream directly from the source without re-encoding, preserving the original audio quality and saving processing time.