FFmpeg HDR / Color
Context: $ARGUMENTS
Quick start
- HDR10 → SDR (safe, works everywhere): → Step 2 (tonemap) + Step 3 + Step 4
- HDR10 → SDR (best quality, Vulkan): → Step 2 (libplacebo) + Step 3 + Step 4
- HLG → SDR: → Step 3 (HLG recipe)
- BT.2020 → BT.709 (SDR-only primaries fix): → Step 3 (zscale primaries)
- Detect what kind of HDR a file is: → Step 1
- Make sure the output is tagged correctly: → Step 4
When to use
- Source is HDR10, HDR10+, HLG, or Dolby Vision and you need an SDR deliverable.
- Colors look washed out, too dark, or neon after transcoding an HDR file.
- Target player assumes BT.709 and you have BT.2020-tagged content.
- Mobile/web/social targets require SDR + Rec.709.
- You need to burn in correct color tags so downstream tools don't mis-interpret.
Step 1 — Detect the HDR type via ffprobe
Inspect color_transfer, color_primaries, color_space, and side_data_list of the video stream.
ffprobe -v error -select_streams v:0 \
-show_entries stream=color_space,color_transfer,color_primaries,color_range:stream_side_data=+ \
-of json input.mkv
Decision map:
| color_transfer | color_primaries | Side data | Type |
| ----------------------------- | ----------------- | ----------------------------- | ---------- |
| smpte2084 | bt2020 | Mastering display metadata | HDR10 |
| smpte2084 | bt2020 | HDR Dynamic Metadata SMPTE2094-40 | HDR10+ |
| arib-std-b67 | bt2020 | (may be absent) | HLG |
| smpte2084 (DoVi profile 5/7/8) | bt2020 | DOVI configuration record | Dolby Vision |
| bt709 / smpte170m | bt709 | — | SDR |
Or use the helper: uv run ${CLAUDE_SKILL_DIR}/scripts/hdrcolor.py detect --input input.mkv.
Step 2 — Pick the tone-map path
tonemapfilter (CPU, zimg): safe default, works on any ffmpeg built with--enable-libzimg. Requires floating-point linear-light input (thezscale=t=linear,format=gbrpf32lesandwich — see Gotchas). Operators:hable(default, recommended),mobius,reinhard,gamma,clip,linear.libplacebofilter (Vulkan, higher quality): better roll-off, built-in debanding, peak detection. Requires-init_hw_device vulkanand ffmpeg built with--enable-libplacebo --enable-vulkan. On Apple Silicon most Homebrew ffmpeg builds lack Vulkan — fall back totonemap.colorspacefilter: does matrix/primaries/trc conversion but not real HDR tone-mapping. Use for BT.2020-SDR → BT.709-SDR, not for PQ/HLG → SDR.
Check availability:
ffmpeg -hide_banner -filters | grep -E "zscale|tonemap|libplacebo|colorspace"
Step 3 — Build the full filter chain
HDR10 → SDR (tonemap, CPU, works everywhere)
ffmpeg -i hdr.mkv \
-vf "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p" \
-c:v libx264 -crf 19 -preset medium \
-c:a copy \
-color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv \
out.mp4
HDR10 → SDR (libplacebo, highest quality)
ffmpeg -init_hw_device vulkan \
-i hdr.mkv \
-vf "format=yuv420p10le,hwupload,libplacebo=tonemapping=bt.2390:colorspace=bt709:color_primaries=bt709:color_trc=bt709:format=yuv420p,hwdownload,format=yuv420p" \
-c:v libx264 -crf 18 -preset medium \
-c:a copy \
-color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv \
out.mp4
HLG → SDR
ffmpeg -i hlg.mkv \
-vf "zscale=t=linear:npl=100:tin=arib-std-b67,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p" \
-c:v libx264 -crf 19 \
-color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv \
out.mp4
Dolby Vision profile 5 (single-layer, IPTPQc2)
Profile 5 is PQ-based and can be tone-mapped like HDR10; the DoVi RPU metadata is discarded. Profiles 7 and 8 need a real DV-aware decoder (e.g. dovi_tool/hdr10plus_tool pre-processing) — ffmpeg alone cannot fully resolve enhancement layers.
ffmpeg -i dovi_p5.mkv \
-vf "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt709,tonemap=tonemap=hable:desat=0,zscale=t=bt709:m=bt709:r=tv,format=yuv420p" \
-c:v libx264 -crf 19 \
-color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv \
out.mp4
BT.2020 → BT.709 (SDR-to-SDR primaries only, no tone-mapping)
ffmpeg -i bt2020_sdr.mkv \
-vf "zscale=primaries=709:transfer=709:matrix=709" \
-c:v libx264 -crf 18 \
-color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv \
out.mp4
SDR → faux HDR (inverse tone-map; warning: marketing only)
ffmpeg -i sdr.mp4 \
-vf "zscale=t=linear:npl=100,format=gbrpf32le,zscale=p=bt2020,tonemap=tonemap=linear:param=4,zscale=t=smpte2084:m=bt2020nc:r=tv,format=yuv420p10le" \
-c:v libx265 -crf 18 -x265-params hdr10=1:colorprim=bt2020:transfer=smpte2084:colormatrix=bt2020nc \
-color_primaries bt2020 -color_trc smpte2084 -colorspace bt2020nc \
out.mkv
Real HDR requires real dynamic range; "upconverting" SDR rarely produces a better image.
Step 4 — Set output color tags and verify
Always pass explicit container flags so downstream players don't guess:
-color_primaries bt709 -color_trc bt709 -colorspace bt709 -color_range tv
Verify they stuck:
ffprobe -v error -select_streams v:0 \
-show_entries stream=color_space,color_transfer,color_primaries,color_range \
-of default=nw=1 out.mp4
Expected:
color_range=tv
color_space=bt709
color_transfer=bt709
color_primaries=bt709
Available scripts
scripts/hdrcolor.py— stdlib-only helper. Subcommands:detect,hdr-to-sdr,hlg-to-sdr,bt2020-to-bt709. Supports--dry-run,--verbose,--method {tonemap,libplacebo},--crf.
uv run ${CLAUDE_SKILL_DIR}/scripts/hdrcolor.py detect --input in.mkv
uv run ${CLAUDE_SKILL_DIR}/scripts/hdrcolor.py hdr-to-sdr --input in.mkv --output out.mp4 --method tonemap --crf 19
uv run ${CLAUDE_SKILL_DIR}/scripts/hdrcolor.py hdr-to-sdr --input in.mkv --output out.mp4 --method libplacebo
uv run ${CLAUDE_SKILL_DIR}/scripts/hdrcolor.py hlg-to-sdr --input hlg.mkv --output out.mp4
uv run ${CLAUDE_SKILL_DIR}/scripts/hdrcolor.py bt2020-to-bt709 --input in.mkv --output out.mp4