Compare Videos with FFmpeg maskedmin Filter
This article explains how to use the maskedmin filter in
FFmpeg to compare two video streams and output the minimum pixel values.
You will learn the basic concept of how this three-input filter works,
the required command-line syntax, and a practical example to implement
it in your video processing projects.
Understanding the maskedmin Filter
The maskedmin filter is a pixel-level comparison filter
in FFmpeg. Unlike simple two-stream comparison filters,
maskedmin requires three video inputs of identical
resolutions and pixel formats:
- Primary Stream (Source 1): The base video stream.
- Reference Stream (Source 2): The video stream compared against the primary stream.
- Mask Stream: The stream that controls where the comparison takes place.
For each pixel, the filter checks the value of the mask stream. If the mask pixel value is high (usually above a default threshold), the filter outputs the minimum pixel value between the Primary and Reference streams. If the mask pixel value is low, the filter outputs the pixel value from the Primary stream.
Basic Command Syntax
To use the maskedmin filter, you must pass three video
inputs to -filter_complex and map them to the filter. Here
is the standard command-line template:
ffmpeg -i video1.mp4 -i video2.mp4 -i mask.mp4 -filter_complex "[0:v][1:v][2:v]maskedmin=planes=15[out]" -map "[out]" output.mp4Parameter Breakdown
[0:v][1:v][2:v]: Specifies the three input streams in order (Primary, Reference, and Mask).maskedmin: Calls the filter.planes: Specifies which color planes to process. The value is a bitmask (e.g.,15processes all planes: Y, U, V, and Alpha). If omitted, the filter defaults to processing all available planes.
Practical Example
If you want to compare two identical-sized videos and use a static grayscale image as a mask (to restrict the minimum-value comparison to a specific region of the frame), you can loop the image as the third input:
ffmpeg -i main_video.mp4 -i ref_video.mp4 -loop 1 -i mask_image.png -filter_complex "[0:v][1:v][2:v]maskedmin[out]" -map "[out]" -shortest output.mp4In this setup, the minimum pixel values between
main_video.mp4 and ref_video.mp4 will only be
rendered in the output where the mask_image.png is white or
bright. Where the mask image is black, the output will default to the
pixels of main_video.mp4.