How to Use FFmpeg Premultiply Filter
This article explains how to use the FFmpeg premultiply
filter to multiply RGB color channels by an alpha (transparency)
channel. You will learn how to apply this filter to single inputs
containing built-in alpha channels, composite separate color and alpha
inputs, and reverse the process using the unpremultiply option.
Understanding the Premultiply Filter
In digital imaging, “straight” alpha keeps RGB color data independent of transparency, while “premultiplied” alpha multiplies the RGB values by the alpha value beforehand. Premultiplication is essential for preventing dark or light fringing artifacts around the edges of transparent objects during compositing.
FFmpeg’s premultiply filter offers two primary modes of
operation depending on your input files: * Inplace processing
(Single Input): Premultiplies an existing alpha channel within
a single file. * Two-stream processing (Double Input):
Takes color data from the first stream and an alpha map from a second
stream to create a premultiplied output.
Method 1: Premultiply a Single Input (Inplace)
If you have an image or video file that already contains an
unassociated (straight) alpha channel, you can premultiply it directly
by setting the inplace option to 1.
Use the following command:
ffmpeg -i input.png -vf "premultiply=inplace=1" output.pnginput.png: The source file containing RGB and Alpha channels.-vf "premultiply=inplace=1": Instructs FFmpeg to use the single input stream and multiply its RGB values by its own alpha channel.
Method 2: Premultiply Using Separate Color and Alpha Inputs
If your color data and alpha matte are in two separate files, you can
merge and premultiply them using filter_complex. By
default, the inplace parameter is set to 0,
which expects two input streams.
Use the following command:
ffmpeg -i color.mp4 -i alpha.mp4 -filter_complex "[0:v][1:v]premultiply=inplace=0[out]" -map "[out]" output.mov-i color.mp4: The primary input containing the color (RGB) channels.-i alpha.mp4: The secondary input containing the grayscale transparency map.[0:v][1:v]premultiply=inplace=0[out]: Takes the video from the first input (0:v) and the alpha from the second input (1:v), premultiplies them, and outputs the result to the[out]label.
Method 3: Unpremultiplying (Reverse Process)
If you have a premultiplied image or video and need to restore it to
straight alpha (for editing or color grading), you can use the
inverse parameter.
To unpremultiply a single input file, run:
ffmpeg -i premultiplied_input.png -vf "premultiply=inplace=1:inverse=1" straight_output.pnginverse=1: Tells the filter to divide the RGB channels by the alpha channel instead of multiplying them.inplace=1: Performs this action on the single input file’s internal channels.