Ffmpeg Chromakey
Context: $ARGUMENTS
Quick start
- Green screen over new bg: → Step 1 (pick filter) → Step 2 (tune) → Step 3 (despill) → Step 4 (overlay)
- Export transparent mezzanine (ProRes 4444 / VP9-alpha / PNG seq): → Step 3, then encode with alpha-capable codec
- GPU path (NVIDIA): → Step 1 variant using
chromakey_cuda
When to use
- Subject shot on green/blue screen needs a new background
- Keyed subject needs to be delivered with alpha for downstream compositing (After Effects, Resolve, web)
- Uneven green (fluorescent falloff, wrinkles): HSV key instead of chromakey
- Subject has a static plate-background (e.g. locked-off camera):
backgroundkeybeats chroma - Bright-on-dark or dark-on-bright with no color cue:
lumakey
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
similarity— how wide the color tolerance is. 0 = exact match only, 1 = everything. Typical0.05–0.20. Start0.10.blend— edge softness (feather). 0 = hard binary edge (aliased, bad), >0 = graded alpha. Typical0.02–0.10. Start0.05.
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]
type=greenortype=bluemix= strength of spill removal (0.2 subtle, 0.6 aggressive)expand= how far from detected pixels to push correction (0.0–0.3)
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
scripts/key.py— key / composite / identity-transparent subcommands; picks an alpha-capable codec based on output extension.
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