Set FFmpeg Color Primaries and Matrix to BT.709

This article provides a quick guide on how to explicitly set the container color primaries, transfer characteristics, and matrix coefficients to BT.709 using FFmpeg. You will learn the correct command-line arguments to flag these color properties during both video re-encoding and lossless remuxing, ensuring consistent color playback across various media players and devices.

To define the color space properties in FFmpeg, you need to configure three specific parameters: the color primaries, the transfer characteristics (gamma curve), and the matrix coefficients. For BT.709 (standard HD video), all three values are set to bt709.

Method 1: Setting BT.709 During Video Re-encoding

If you are encoding or transcoding a video (for example, using the H.264 codec), you can pass the color flag arguments directly to the video encoder.

Run the following command:

ffmpeg -i input.mp4 -c:v libx264 -color_primaries bt709 -color_trc bt709 -colorspace bt709 -c:a copy output.mp4

Parameter Breakdown: * -color_primaries bt709: Identifies the color primaries as BT.709 (ITU-R BT.709). * -color_trc bt709: Sets the transfer characteristics to BT.709. * -colorspace bt709: Sets the matrix coefficients (YUV-to-RGB conversion matrix) to BT.709. * -c:a copy: Copies the audio stream without re-encoding to save processing time.

Method 2: Tagging BT.709 Without Re-encoding (Lossless)

If your video is already encoded but lacks the correct metadata tags, you can write the BT.709 metadata into the video bitstream without re-encoding the video. This process is instant and lossless.

For H.264 video streams, use the H.264 bitstream filter:

ffmpeg -i input.mp4 -c copy -bsf:v h264_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1 output.mp4

For H.265 / HEVC video streams, use the HEVC bitstream filter:

ffmpeg -i input.mp4 -c copy -bsf:v hevc_metadata=colour_primaries=1:transfer_characteristics=1:matrix_coefficients=1 output.mp4

How it works: The number 1 is the standard index value representing BT.709 in the ISO/IEC 23001-8 / ITU-T H.273 specification. Using these bitstream filters modifies the sequence parameter set (SPS) headers of the video stream directly, forcing media players to interpret the file as BT.709.