How to Multiply Pixel Values of Two Videos in FFmpeg
Multiplying the pixel values of two video streams is a blending
technique equivalent to the "Multiply" blend mode found in image and
video editing software. In FFmpeg, this effect is achieved using the
blend filter set to the multiply mode. This
article provides a concise guide and the exact commands needed to align,
scale, and multiply pixel values across two input videos.
Basic Multiply Command
To multiply two videos that already share the same resolution, pixel
format, and frame rate, use the -filter_complex option with
the blend filter:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]blend=all_mode='multiply'" -c:a copy output.mp4In this command:
[0:v]refers to the video stream ofinput1.mp4.[1:v]refers to the video stream ofinput2.mp4.blend=all_mode='multiply'applies the mathematical multiplication formula(A * B) / 255to each color channel (assuming standard 8-bit video). White pixels (255) leave the other video unchanged, while black pixels (0) result in pure black.-c:a copycopies the audio stream from the primary input without re-encoding.
Handling Different Resolutions
The blend filter requires both inputs to have identical
dimensions. If the inputs differ in size, insert a
scale2ref or scale filter to match the
dimensions before blending:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[1:v][0:v]scale2ref[v2][v1];[v1][v2]blend=all_mode='multiply'" -c:a copy output.mp4This scales the second video ([1:v]) to match the width
and height of the first video ([0:v]).
Adjusting the Multiplication Intensity
You can adjust the strength of the multiplication effect using the
all_opacity parameter, which controls the opacity of the
top layer (input 2) between 0.0 (no effect) and
1.0 (full effect):
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v][1:v]blend=all_mode='multiply':all_opacity=0.75" -c:a copy output.mp4Ensuring Format Compatibility
If your source videos use different pixel formats (such as one being
RGB and the other YUV), convert both to a unified format like
yuv420p to prevent rendering artifacts or filter
errors:
ffmpeg -i input1.mp4 -i input2.mp4 -filter_complex "[0:v]format=yuv420p[v1];[1:v]format=yuv420p[v2];[v1][v2]blend=all_mode='multiply'" -c:v libx264 -pix_fmt yuv420p output.mp4