How to Use the FFmpeg Mix Filter to Blend Videos

Blending multiple video streams into a single output is a powerful technique for creating transitions, double exposures, and overlay effects. This article provides a straightforward guide on how to use the FFmpeg mix filter to combine two or more video inputs. You will learn the basic syntax of the filter, understand how weight parameters control opacity, and see practical command-line examples to get started immediately.

Understanding the FFmpeg mix Filter

The mix filter in FFmpeg takes multiple video streams and mixes them frame-by-frame. Unlike picture-in-picture overlays, the mix filter blends the pixel values of each input video based on specified weights, allowing you to achieve semi-transparent, layered visual effects.

The basic syntax for the mix filter inside a filtergraph is:

[input0][input1]...mix=inputs=N:weights=W0 W1...:duration=D[out]

Key Parameters:


Practical Examples

1. Blending Two Videos Equally (50/50 Mix)

To blend two videos together with equal visibility, map both inputs to the mix filter and specify the number of inputs as 2.

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]mix=inputs=2:duration=shortest[outv]" -map "[outv]" output.mp4

In this command: * [0:v] and [1:v] represent the video streams of video1.mp4 and video2.mp4. * duration=shortest ensures the output stops rendering as soon as the shorter video ends, preventing frozen frames.

2. Adjusting Opacity with Weights

If you want one video to be more prominent than the other, adjust the weights parameter. In this example, the first video is twice as visible as the second video.

ffmpeg -i video1.mp4 -i video2.mp4 -filter_complex "[0:v][1:v]mix=inputs=2:weights=2 1[outv]" -map "[outv]" output.mp4

Because FFmpeg normalizes the weights, 2 1 is mathematically treated as 0.66 (66% opacity) for the first video and 0.33 (33% opacity) for the second.

3. Mixing Three Videos Simultaneously

The mix filter can handle more than two inputs. To blend three videos together, set inputs=3 and provide three weight values.

ffmpeg -i video1.mp4 -i video2.mp4 -i video3.mp4 -filter_complex "[0:v][1:v][2:v]mix=inputs=3:weights=1 1 1[outv]" -map "[outv]" output.mp4

Each of the three videos in this configuration contributes exactly one-third (33.3%) to the final blended output.