A green-screen product promo needs two fixes: the screen has to go, and the green that leaked onto hair and hands has to go too. Written as one FFmpeg filter chain, the order that reads naturally is "clean the colour, then cut it out". That order breaks the key. On one frame of a real green-screen clip, putting despill in front of chromakey took the share of fully transparent pixels from 25.9% down to 0.1%, so the backdrop basically stayed in the frame. The short version: pull the alpha first, despill the foreground second, composite last.
Despill in front of the key leaves the screen in place
The footage is borrowed rather than shot for this post: "Filming with the green screen" from Wikimedia Commons, published by VOA Learning English and in the public domain, cut down to 3 seconds and re-encoded as 540×960 H.264 at 30 fps. The backdrop is a blown-out green, mean RGB (115.7, 255.0, 154.8), so the key colour below is the measured screen colour, not a textbook green. Frame 45 was keyed twice, in a single run on one Apple silicon Mac with FFmpeg, with and without despill in front:
$ ffmpeg -i input.mp4 -vf "select=eq(n\,45),format=rgba,chromakey=color=0x74FF9B:similarity=0.14:blend=0.08" -vframes 1 key_only.png
$ ffmpeg -i input.mp4 -vf "select=eq(n\,45),format=gbrp,despill=type=green,format=rgba,chromakey=color=0x74FF9B:similarity=0.14:blend=0.08" -vframes 1 despill_first.png
Reading the alpha channel of both PNGs gave this:
key_only.png alpha=0 占比 0.259 alpha<128 占比 0.707
despill_first.png alpha=0 占比 0.001 alpha<128 占比 0.237
占比 means "share of pixels". Pixels that are more than half transparent dropped from 70.7% to 23.7%, and pixels that are fully gone dropped from 25.9% to 0.1%. Nothing errors out. The only symptom is a grey-ish slab where the new background should be, which is easy to blame on the similarity value and spend an evening tuning.
The screen stops being green once despill runs
Despill pulls green down wherever it beats the other two channels, and nothing is greener than the screen itself. A self-made test card makes this measurable. It is 1280×720 with a screen colour of RGB (20, 175, 65), and the spill on the foreground was added on purpose with a simple model, so it shows the direction of the effect rather than how a real studio behaves. Do the arithmetic for one screen pixel under an average-limit despill, G = min(G, (R + B) / 2): the cap is (20 + 65) / 2 = 42.5, so G drops from 175 to about 42 and the backdrop becomes roughly (20, 42, 65). The colour-difference key used here, a = 1 − clip((G − max(R, B) − 20) / 70), only starts cutting once G clears the larger of R and B by 20. Now G sits 23 below B, so every pixel keeps full opacity, even though the true alpha averages only 0.330. The new background never shows through; that is where the grass column in the table below comes from (colour error is ΔE76 throughout). The real clip went the same way as far as the alpha numbers show, and the likely cause is the same: once despilled, the backdrop no longer reads as 0x74FF9B. That part is an inference, not something measured on the frame's colours.
Despill after compositing breaks the new background
The other tidy-looking option is to composite first and run despill over the whole frame at the end. The filter cannot tell your foreground from your new background. On the test card the grass behind the subject went from G 150 to 65, and a yellow bar in the background, RGB (235, 200, 40), came out as (235, 138, 40), which is orange. With a brand colour in the plate, that is worse than a green fringe.
Pull the alpha first and despill only the foreground
Same test card, same colour-difference key, same average-limit despill, composited onto the same background, compared with the clean foreground and true alpha:
| Order | Edge ΔE | Grass ΔE | Whole frame ΔE |
|---|---|---|---|
| No despill | 23.1 | 0.0 | 5.41 |
| Key, then despill the foreground | 13.2 | 0.0 | 3.58 |
| Despill, then key | 39.9 | 80.7 | 49.96 |
| Composite, then despill everything | 27.6 | 62.7 | 25.00 |
| Key, subtract the screen colour, then despill | 11.7 | 0.0 | 3.44 |
The last row adds one step: once you have the alpha, solve for the foreground with F = (C − (1 − α) · screen) / α before despilling, so the screen colour mixed into semi-transparent edges is removed by arithmetic instead of by a colour filter. It is the best row, but only 11.7 against 13.2 on the edges. For a 30-second product video, a reasonable plan is to ship the simpler version first and add the unmix step only if hair edges still look off.
An AI matte still needs the despill step
The AI side of this comparison came from ImgIng https://imging.ai/ , the video matting Beta that runs in a desktop browser and, with a transparent background selected, exports WebM (VP9 alpha) only. On the same frame of the VOA clip, composited onto grey, 2.7% of hair-tip pixels and 4.9% of hand pixels were still visibly green in the compatible mode, and 7.7% and 14.8% in the quality mode. Adding one average-limit pass after the matte took all four to 0%. The usual reason is that an AI matte hands you transparency while the colour still comes from the source frame, so the spill rides along. The chroma key looked better on this count only because it thinned the hair: its hair tips had 14,748 semi-transparent pixels against 5,963 for ImgIng's quality mode, about 2.5 times as many.
What is still open: the real clip has no clean ground truth, so the AI matte's error against the true edge was not measured, only the share of green pixels. If your pipeline keys with FFmpeg today, the quickest check is to render one frame in both orders and count alpha=0 pixels. If the current chain sits near zero and the swapped one jumps up, despill is running too early.
Top comments (0)