FFmpeg Mix Filter: Blend Videos with Custom Weights

This article explains how to use the FFmpeg mix video filter to blend multiple video inputs into a single output using custom weight factors. You will learn the basic syntax of the filter, how weight factors influence the opacity and visibility of each layer, and how to structure a complete command line execution for different blending ratios.

Understanding the Mix Filter Syntax

The mix filter takes multiple video streams and combines them frame-by-frame. The basic syntax for the filter is:

mix=inputs=N:weights='W1 W2 ... WN':scale=S
  • inputs: Specifies the number of input video streams to blend (default is 2).
  • weights: A space-separated list of weight factors assigned to each input video.
  • scale: A divisor applied to the sum of the weighted inputs. By default, this is set to the sum of all weights, which normalizes the output brightness.

Blending Two Videos with Custom Weights

To blend two videos together, you must specify both video inputs and define their respective weights. For example, if you want the first video to be more prominent than the second, you can assign a weight of 0.7 to the first and 0.3 to the second.

Here is the complete FFmpeg command:

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

In this command:

  • -i video1.mp4 and -i video2.mp4 load the two source videos.
  • [0:v][1:v] routes the video streams of the first and second inputs into the filter.
  • weights='0.7 0.3' ensures the first video retains 70% opacity while the second video contributes 30% to the final blend.

Blending Three or More Videos

You can extend the mix filter to handle three or more inputs by increasing the inputs parameter and adding corresponding weights. To blend three videos with custom weights of 50%, 25%, and 25% respectively, use the following command:

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

Important Technical Considerations

  • Resolution and Frame Rate: The mix filter requires all input videos to have the same resolution, pixel format, and frame rate. If your input videos differ, you must scale and format them first using filters like scale and fps.
  • Duration: The output video duration will default to the duration of the shortest input video.
  • Audio: The mix filter only processes video streams. If you need to combine the audio streams as well, you must use the amix filter separately in your filter graph.