Replace Color in Video Using FFmpeg Chromakey
This article explains how to replace a specific color in a video with
a new color using the FFmpeg chromakey filter. You will
learn the exact command-line syntax required to make a target color
transparent and overlay your video onto a new, solid-colored
background.
To replace a color in a video, you cannot simply swap one color value
for another in a single step. Instead, you must use a two-step process
within an FFmpeg filtergraph: first, use the chromakey
filter to make the target color transparent, and second, use the
overlay filter to place the keyed video on top of a newly
generated background color.
Here is the standard command to achieve this:
ffmpeg -i input.mp4 -f lavfi -i "color=color=blue:size=1920x1080:rate=30" -filter_complex "[0:v]chromakey=color=0x00FF00:similarity=0.1:blend=0.1[keyed];[1:v][keyed]overlay=shortest=1[out]" -map "[out]" -map 0:a? -c:a copy output.mp4How the Command Works
-i input.mp4: Specifies your source video.-f lavfi -i "color=...": Generates a solid background of your desired replacement color (in this case,blue). You should match thesize(resolution) andrate(frame rate) to your input video.chromakey=color=0x00FF00:similarity=0.1:blend=0.1: This filter targets the color you want to remove.color: The hex value of the color to be removed (e.g.,0x00FF00for pure green).similarity: Controls the tolerance. A higher value matches colors close to the target color.blend: Controls the opacity of the transition edges to prevent harsh pixelation.
overlay=shortest=1: Places the transparent video ([keyed]) on top of the solid background color.shortest=1ensures the output video stops when the input video ends.-map 0:a? -c:a copy: Copies the audio from the original video to the output file without re-encoding it.
Fine-Tuning the Color Replacement
If the wrong areas of your video are being replaced, adjust the
similarity and blend parameters.
- For exact digital colors: Keep
similaritylow (around0.01to0.05). - For physical green screens: Increase
similarity(between0.1and0.3) to account for shadows and lighting variances.