Use FFmpeg Floodfill Filter to Color Video Areas

This article provides a straightforward guide on how to use the floodfill filter in FFmpeg to select a specific coordinate in a video and fill its surrounding, matching color region with a solid color. You will learn the exact syntax, the parameters required, and a practical command-line example to execute this effect.

Understanding the Floodfill Filter

The floodfill filter in FFmpeg works by taking a starting pixel coordinate \((x, y)\), checking if the color at that coordinate matches a specified target color, and then filling that entire connected region of similar pixels with a new destination color.

To ensure predictable results, it is best to convert your video to the RGB color space before applying the filter, and then convert it back to YUV for standard video compatibility.

Filter Parameters

The filter uses the following main parameters: * x: The horizontal coordinate of the starting pixel. * y: The vertical coordinate of the starting pixel. * s0, s1, s2: The source color values (RGB components) to match at the starting point. * d0, d1, d2: The destination color values (RGB components) to fill the area with.

Practical Command Example

The following command selects a point at coordinates x=100, y=150, targets a black background (0, 0, 0 in RGB), and fills the connected black area with solid red (255, 0, 0):

ffmpeg -i input.mp4 -vf "format=rgb24,floodfill=x=100:y=150:s0=0:s1=0:s2=0:d0=255:d1=0:d2=0,format=yuv420p" -c:a copy output.mp4

How the Command Works

  1. format=rgb24: Converts the video frame pixel format to RGB. This makes defining source (s0, s1, s2) and destination (d0, d1, d2) colors highly intuitive using standard 0–255 RGB values.
  2. floodfill=x=100:y=150...: Starts at pixel (100, 150). It looks for the color defined by s0=0 (Red), s1=0 (Green), and s2=0 (Blue). It then floods that contiguous area with d0=255 (Red), d1=0 (Green), and d2=0 (Blue).
  3. format=yuv420p: Converts the pixel format back to YUV420p, which is the standard format required for compatibility with most media players.
  4. -c:a copy: Copies the audio stream without re-encoding to save processing time.