Ffmpeg chromakey

From damionrashford/media-os · View interactive version

Ffmpeg chromakey is an AI agent skill from the GitHub repository damionrashford/media-os. Chromakey, greenscreen, and compositing with ffmpeg: chromakey, colorkey, hsvkey, lumakey, chromakey_cuda, despill, backgroundkey, overlay with alpha. Use when the user asks to do a greenscreen, remove a green/blue background, chroma key, key out a color, composite a subject onto a new background, f

How to use this skill

Skill file

Ffmpeg Chromakey

Context: $ARGUMENTS

Quick start

When to use

Step 1 — Pick the key filter

| Situation | Filter |

|---|---|

| Clean green/blue screen, even lighting | chromakey (YUV/UV) |

| Need RGB-based pick or chromakey struggles | colorkey (RGB) |

| Uneven lighting, muddy green, hair detail | hsvkey |

| No color — separate on brightness only | lumakey |

| Locked camera, have a clean plate | backgroundkey (subtracts plate) |

| NVIDIA GPU available, must be fast | chromakey_cuda |

Always sample the ACTUAL green from your footage (screenshot a flat patch, eyedrop in Preview / ffplay with -vf format=rgb24). 0x00FF00 is pure green and almost never matches real screens (typical: 0x20B040 – 0x3EB34C).

Step 2 — Tune similarity + blend

Loop: bump similarity until the screen is gone and the subject's edges start chewing → back off one notch → then raise blend until edges look natural.

# Basic chromakey (YUV), preserves alpha on yuva420p
ffmpeg -i fg.mp4 -vf "chromakey=0x20B040:0.12:0.05,format=yuva420p" -c:v qtrle keyed.mov

Step 3 — Despill

Even after a clean key, green light reflected onto the subject's skin / hair / white shirt will show as a sickly fringe. Always run despill after the key, before compositing.

# Key then despill (green). Chain inside the same filter graph.
[0:v]chromakey=0x20B040:0.12:0.04,despill=type=green:mix=0.5:expand=0.1[keyed]

Step 4 — Composite with `overlay`

# Simple green key + composite onto bg.mp4
ffmpeg -i fg.mp4 -i bg.mp4 \
  -filter_complex "[0:v]chromakey=0x00FF00:0.10:0.05[keyed];[1:v][keyed]overlay=shortest=1[out]" \
  -map "[out]" -map 0:a? -c:v libx264 -crf 18 -pix_fmt yuv420p out.mp4

Order: background first ([1:v]), keyed subject on top ([keyed]). shortest=1 ends at the shorter input; drop it to run until the longer input finishes.

Available scripts

Workflow

Key and write a transparent ProRes 4444 mezzanine:

uv run ${CLAUDE_SKILL_DIR}/scripts/key.py identity-transparent \
  --input fg.mp4 --output keyed.mov --color 0x20B040 --similarity 0.12 --blend 0.05

Full composite in one shot:

uv run ${CLAUDE_SKILL_DIR}/scripts/key.py composite \
  --fg fg.mp4 --bg bg.mp4 --output out.mp4 --color 0x20B040 --despill green

Reference docs

Read CODE0 when tuning per-filter options, picking an alpha-capable codec/container, comparing chromakey vs colorkey vs hsvkey, or looking up real-world green/blue hex samples.

Gotchas

Examples

Example 1: Clean green screen → composite over new bg

ffmpeg -i subject.mp4 -i newbg.mp4 \
  -filter_complex "[0:v]chromakey=0x20B040:0.12:0.05,despill=type=green:mix=0.4[k];[1:v][k]overlay=shortest=1[out]" \
  -map "[out]" -map 0:a? -c:v libx264 -crf 18 -pix_fmt yuv420p out.mp4

Example 2: Uneven lighting → HSV key

ffmpeg -i subject.mp4 -i newbg.mp4 \
  -filter_complex "[0:v]hsvkey=h=120:s=0.7:v=0.3:similarity=0.05:blend=0.02,despill=type=green[k];[1:v][k]overlay[out]" \
  -map "[out]" -c:v libx264 -crf 18 out.mp4

Example 3: Transparent ProRes 4444 mezzanine

ffmpeg -i subject.mp4 -vf "chromakey=0x20B040:0.12:0.05,despill=type=green,format=yuva444p10le" \
  -c:v prores_ks -profile:v 4444 -pix_fmt yuva444p10le -c:a copy keyed.mov

Example 4: Transparent WebM (VP9 with alpha)

ffmpeg -i subject.mp4 -vf "chromakey=0x20B040:0.12:0.05,despill=type=green,format=yuva420p" \
  -c:v libvpx-vp9 -pix_fmt yuva420p -b:v 4M -auto-alt-ref 0 keyed.webm

Example 5: PNG image sequence (fully portable alpha)

ffmpeg -i subject.mp4 -vf "chromakey=0x20B040:0.12:0.05,despill=type=green,format=rgba" \
  -c:v png frames/keyed_%05d.png

Example 6: GPU (NVIDIA)

ffmpeg -hwaccel cuda -i subject.mp4 -i newbg.mp4 \
  -filter_complex "[0:v]hwupload_cuda,chromakey_cuda=0x00FF00:0.1:0.05,hwdownload,format=yuva420p[k];[1:v][k]overlay[out]" \
  -map "[out]" -c:v h264_nvenc -cq 20 out.mp4

Example 7: Background subtraction (locked camera + clean plate)

ffmpeg -i subject.mp4 -i plate.mp4 \
  -filter_complex "[0:v][1:v]backgroundkey=threshold=0.08:similarity=0.1:blend=0.05[k];[0:v][k]alphamerge,format=yuva420p[out]" \
  -map "[out]" -c:v qtrle keyed.mov

Example 8: colorkey variant (RGB-based)

ffmpeg -i subject.mp4 -vf "colorkey=0x00FF00:0.30:0.20,format=yuva420p" -c:v qtrle keyed.mov

Troubleshooting

Error: output has black background where the key should be transparent

Cause: container/codec does not support alpha (e.g. MP4+libx264).

Solution: switch to MOV+qtrle, MOV+ProRes 4444, WebM+libvpx-vp9 (yuva420p), or PNG sequence.

Error: green fringe / halo around subject

Cause: reflected green spill; key cannot remove reflected light.

Solution: chain despill=type=green:mix=0.5:expand=0.1 after the key filter, BEFORE the overlay.

Error: jagged / aliased edges

Cause: blend=0 or too-low blend.

Solution: raise blend to 0.02–0.08. If still bad, switch output to format=yuva444p (no chroma subsampling on alpha).

Error: hair / fine detail vanishes

Cause: similarity too high.

Solution: lower similarity, then switch to hsvkey (better tonal separation), and/or downstream roto for hard edges.

Error: `chromakey_cuda` fails with "No device available"

Cause: NVIDIA drivers / CUDA not available, or missing hwupload bracket.

Solution: confirm ffmpeg -hwaccels lists cuda. Always bracket: hwupload_cuda,chromakey_cuda=...,hwdownload,format=yuva420p.

Error: `overlay` output ends early

Cause: shortest=1 set, and the keyed clip is shorter than bg.

Solution: remove shortest=1, or swap input order.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.