FFmpeg Colorkey: How to Make a Color Transparent

This guide explains how to use the FFmpeg colorkey filter to target and remove a specific color from a video, rendering it transparent. You will learn the exact command syntax, how to adjust similarity and blend parameters for clean edges, and how to export your final video using a codec that supports alpha channels (transparency).

The Basic Colorkey Syntax

The colorkey filter works by identifying a target color and converting it to transparent black. The basic syntax for the filter is:

colorkey=color:similarity:blend

  • color: The color you want to make transparent. You can specify this as a hex value (e.g., 0x00FF00 for green) or a standard color name (e.g., green, black, white).
  • similarity: A value between 0.01 and 1.0 (default is 0.01). This controls the tolerance level. A higher value matches colors that are similar to your target color.
  • blend: A value between 0.0 and 1.0 (default is 0.0). This controls the smoothness of the edges. A higher value blends the pixels on the border between the transparent and non-transparent areas to prevent harsh edges.

Step-by-Step Implementation

Because standard MP4 (H.264) files do not support transparency, you must export your output file using a codec and format that supports an alpha channel, such as WebM (VP9) or QuickTime (Animation/PNG).

Example 1: Creating a Transparent WebM (Web-Friendly)

WebM is the ideal format for transparent videos on websites. The following command removes a pure green screen (0x00FF00) and exports a transparent WebM video:

ffmpeg -i input.mp4 -vf "colorkey=0x00FF00:0.3:0.1" -c:v libvpx-vp9 -pix_fmt yuva420p output.webm

Command breakdown:

  • -i input.mp4: Specifies the input video.
  • -vf "colorkey=0x00FF00:0.3:0.1": Applies the colorkey filter. It targets green (0x00FF00), sets the similarity tolerance to 0.3, and applies a 0.1 blend to smooth the edges.
  • -c:v libvpx-vp9: Uses the VP9 codec, which supports transparency.
  • -pix_fmt yuva420p: Sets the pixel format to include the alpha channel ("a" in yuva represents alpha/transparency).

Example 2: Creating a Transparent QuickTime MOV (Editing-Friendly)

If you need to import the transparent video into a video editor like Premiere Pro, After Effects, or DaVinci Resolve, a QuickTime container with the PNG or QTRLE codec is preferred.

ffmpeg -i input.mp4 -vf "colorkey=green:0.2:0.05" -c:v qtrle output.mov

Command breakdown:

  • -vf "colorkey=green:0.2:0.05": Targets the color named green with a similarity of 0.2 and a blend of 0.05.
  • -c:v qtrle: Uses the QuickTime Animation codec, which natively supports alpha channels.

Fine-Tuning Your Key

If you see leftover color halos or parts of your subject are disappearing, adjust your settings using these rules of thumb:

  • If the background is not fully removed: Increase the similarity value (e.g., from 0.1 to 0.3).
  • If parts of the subject are disappearing: Decrease the similarity value.
  • If the edges look pixelated or sharp: Increase the blend value (e.g., to 0.1 or 0.2) to soften the transition.