How to Use FFmpeg Maskedmerge Filter with Custom Masks

This article explains how to use the FFmpeg maskedmerge filter to blend two video sources using a custom mask. You will learn the core concepts of mask-based merging, how to apply a static image as a custom mask, and how to programmatically generate dynamic, animated masks directly inside FFmpeg using video filters.

Understanding the Maskedmerge Filter

The maskedmerge filter combines three input streams: 1. First Input (Base): The background video. 2. Second Input (Overlay): The foreground video. 3. Third Input (Mask): A grayscale video or image.

The filter processes pixels based on the mask’s luminance value (0-255). Black areas (0) in the mask display the base video, white areas (255) display the overlay video, and gray areas create a semi-transparent blend. All three inputs must have identical dimensions and frame rates.

Method 1: Using a Static Image as a Custom Mask

If you have a pre-designed grayscale PNG image to use as your mask, you can load it as the third input.

Use the following command to merge base.mp4 and overlay.mp4 using mask.png:

ffmpeg -i base.mp4 -i overlay.mp4 -loop 1 -i mask.png -filter_complex "[0:v][1:v][2:v]maskedmerge[out]" -map "[out]" -shortest output.mp4

Command Breakdown:

Method 2: Generating a Custom Mask Programmatically

You can create custom shapes and gradients directly inside FFmpeg without external images by using the geq (generic equation) filter to generate a mask stream.

Creating a Static Split-Screen Mask

To split the screen vertically down the middle (left side base, right side overlay), generate a mask that is black on the left and white on the right:

ffmpeg -i base.mp4 -i overlay.mp4 -filter_complex "[0:v]geq=lum='if(gt(X,W/2),255,0)'[mask];[0:v][1:v][mask]maskedmerge[out]" -map "[out]" output.mp4

Creating a Custom Circular Mask

To display the overlay video inside a circle in the center of the screen, use the algebraic equation for a circle:

ffmpeg -i base.mp4 -i overlay.mp4 -filter_complex "[0:v]geq=lum='if(lt(sqrt(pow(X-W/2,2)+pow(Y-H/2,2)),200),255,0)'[mask];[0:v][1:v][mask]maskedmerge[out]" -map "[out]" output.mp4

Method 3: Creating an Animated Mask Transition

You can introduce the time variable (T) to create dynamic transitions, such as a horizontal wipe from left to right over 5 seconds:

ffmpeg -i base.mp4 -i overlay.mp4 -filter_complex "[0:v]geq=lum='if(gt(X,W*T/5),0,255)'[mask];[0:v][1:v][mask]maskedmerge[out]" -map "[out]" output.mp4