FFmpeg Colorkey: Make a Color Transparent

The colorkey filter in FFmpeg allows you to remove a specific color from an image or video, turning those pixels transparent. This technique is commonly used for chroma keying (green or blue screen effects) to isolate subjects or prepare footage for compositing. This guide provides the core syntax, parameter options, and practical command-line examples to quickly make any color transparent and export the result using alpha-capable formats or overlays.

Understanding the Colorkey Filter Syntax

The basic syntax for the colorkey filter is:

colorkey=color:similarity:blend
  • color: The target color you want to turn transparent. You can specify this using a hex code (e.g., 0x00FF00 or #00FF00) or standard color names (e.g., green, black, white).
  • similarity: A value between 0.01 and 1.0 (default is 0.01). It determines the tolerance for color matching. A higher value removes colors that are close to the target color, compensating for uneven lighting.
  • blend: A value between 0.0 and 1.0 (default is 0.0). It creates a smooth falloff or feathering around the edges to reduce harsh fringing.

Exporting Video with Transparency

Most standard video containers and codecs (such as MP4 with H.264) do not support alpha channels. To preserve transparency, you must export to an alpha-supported codec such as VP9 in WebM, ProRes 4444, or QuickTime Animation.

Exporting to WebM (VP9)

ffmpeg -i input.mp4 -vf "colorkey=0x00FF00:0.3:0.1" -c:v libvpx-vp9 -pix_fmt yuva420p output.webm
  • -vf "colorkey=0x00FF00:0.3:0.1" targets pure green, with a 0.3 similarity threshold and 0.1 edge blend.
  • -pix_fmt yuva420p ensures the pixel format includes an alpha channel (a).

Exporting to Apple ProRes 4444 (.mov)

ffmpeg -i input.mp4 -vf "colorkey=0x00FF00:0.25:0.05" -c:v prores_ks -profile:v 4 -pix_fmt yuva444p10le output.mov

Making a Color Transparent in an Image

For still images, PNG and WebP easily support alpha channels:

ffmpeg -i input.jpg -vf "colorkey=white:0.1:0.0" output.png

This command replaces the white background of an image with transparent pixels and saves it as a PNG.

Replacing the Background Directly (Overlay)

If your ultimate goal is to put a new background behind the keyed video without saving an intermediate transparent file, combine colorkey with the overlay filter in a single command:

ffmpeg -i background.mp4 -i foreground.mp4 -filter_complex "[1:v]colorkey=0x00FF00:0.3:0.1[keyed];[0:v][keyed]overlay[out]" -map "[out]" -c:v libx264 output.mp4

In this pipeline:

  1. [1:v] takes the foreground video and applies colorkey to remove green, outputting to a stream labeled [keyed].
  2. [0:v][keyed]overlay layers the keyed video directly on top of the background.
  3. The final result can be saved as a standard H.264 MP4 because the transparency has been filled by the background layer.