Remove Green Screen Background with FFmpeg on Linux

This article provides a straightforward guide on how to remove a green screen (chroma key) background from a video using FFmpeg on Linux. You will learn the exact commands to make a green background transparent, replace it with a static image, or overlay your subject onto a completely different video background.

The Core Chroma Key Command

To remove a green screen background, FFmpeg utilizes the chromakey video filter. This filter identifies a specific color and turns it transparent.

The basic syntax for making a green background transparent is:

ffmpeg -i input.mp4 -vf "chromakey=0x00FF00:0.1:0.1" -c:v png -c:a copy output.mov

Note: Standard MP4 containers do not support transparency (alpha channels). When outputting a video with a transparent background, you should use a format like QuickTime MOV with a PNG or ProRes codec, or WebM.

Understanding the Parameters

To get a clean cut without jagged edges or "green spill" around your subject, you need to tweak the three main parameters of the chromakey filter:

  • Color (0x00FF00): The hex code of the color you want to remove. While 0x00FF00 is pure green, your actual video background might be closer to 0x32CD32 (Lime green) or 0x00B140 (Chroma key green).
  • Similarity (0.1): Controls how close a color must be to the target color to be removed. A higher value removes a wider range of green shades. This is useful if your green screen has uneven lighting.
  • Blend (0.1): Controls the smoothness of the edges. A higher value blends the edges of your subject with the transparency, reducing harsh pixelation.

Replacing the Green Screen with an Image

If you want to instantly replace the green screen with a static background image, you need to input both the video and the image, then use the overlay filter.

ffmpeg -i input.mp4 -i background.jpg -filter_complex "[0:v]chromakey=0x00FF00:0.15:0.1[ckout];[1:v][ckout]overlay=0:0" -c:a copy output.mp4

In this command, filter_complex manages multiple streams. It applies the chroma key to the video (stream 0), labels that result as [ckout], and then overlays [ckout] directly on top of the background image (stream 1).

Replacing the Green Screen with Another Video

To overlay your green screen footage onto a completely different moving video background, the logic remains the same as the image replacement, but you will target the second video stream instead.

ffmpeg -i input.mp4 -i background_video.mp4 -filter_complex "[0:v]chromakey=0x00FF00:0.15:0.1[ckout];[1:v][ckout]overlay=0:0:shortest=1" -c:a copy output.mp4

The addition of shortest=1 at the end of the overlay parameters ensures that the output video will automatically stop encoding as soon as the shortest input video ends, preventing a frozen frame or an infinite loop of black space.