FFmpeg Chromakey Tutorial: How to Remove Green Screen

This article provides a straightforward guide on how to use the chromakey filter in FFmpeg to remove solid-color backgrounds, such as green screens, from your videos. You will learn the basic command syntax, how to adjust similarity and blend parameters to fine-tune your key, and how to overlay your green screen footage onto a new background image or video.

Understanding the Chromakey Filter

The chromakey filter in FFmpeg makes a specified color in your video transparent. It is primarily used for green screens or blue screens.

The basic syntax for the filter is:

chromakey=color:similarity:blend

Key Parameters:

  • color: The color to be replaced. You can specify this as a hex value (e.g., 0x00FF00 for pure green) or a color name (e.g., green).
  • similarity: A threshold value that decides how close a pixel's color must be to the target color to be made transparent. The range is 0.01 to 1.0 (default is 0.01).
  • blend: Controls the transparency transition at the edges. A higher value creates smoother, softer edges. The range is 0.0 to 1.0 (default is 0.1).

Example 1: Basic Green Screen Removal

To remove a standard green screen and output the video with an alpha (transparent) channel, use the following command. Note that the output format must support transparency (like QuickTime .mov using the png or prores_ks codecs):

ffmpeg -i input.mp4 -vf "chromakey=0x00FF00:0.3:0.1" -c:v qtrle output.mov
  • 0x00FF00 targets the pure green color.
  • 0.3 is the similarity tolerance (often necessary since real-world green screens have shadows and highlights).
  • 0.1 adds a slight blend to soften the edges.

Example 2: Overlaying Green Screen Footage onto a Background

Most of the time, you want to replace the green screen with a new background image or video immediately. You can achieve this in a single command using FFmpeg's filter_complex and the overlay filter.

Overlaying onto a Video Background:

ffmpeg -i background.mp4 -i greenscreen.mp4 -filter_complex "[1:v]chromakey=0x00FF00:0.3:0.1[keyed];[0:v][keyed]overlay=shortest=1[out]" -map "[out]" output.mp4

How this command works:

  1. -i background.mp4: Input 0 (the background).
  2. -i greenscreen.mp4: Input 1 (the green screen foreground).
  3. [1:v]chromakey=...[keyed]: Applies the chromakey filter to Input 1 and names the output stream keyed.
  4. [0:v][keyed]overlay...[out]: Places the keyed stream on top of Input 0.
  5. shortest=1: Tells the output to stop when the shortest input video ends.
  6. -map "[out]": Maps the final combined video stream to the output file.

Tips for Getting a Clean Key

  • Adjust Similarity First: If parts of the green screen are still visible, slowly increase the similarity value (e.g., from 0.3 to 0.35 or 0.4). If parts of your subject start disappearing, lower the similarity.
  • Adjust Blend for Edges: If your subject has a harsh pixelated outline or a green halo, increase the blend value (e.g., 0.15 or 0.2) to smooth out the edges.
  • Exact Color Matching: If your green screen is unevenly lit, use a color picker tool on a screenshot of your video to find the exact hex code of the green closest to your subject, and use that hex code in the color parameter.