FFmpeg Premultiply Filter: How to Premultiply Alpha

This article provides a practical guide on using the premultiply filter in FFmpeg to multiply RGB color channels by their corresponding alpha values. Premultiplying alpha is essential in video compositing and computer graphics to prevent fringing, halo artifacts, and color bleeding around transparent edges. Below, you will learn the mechanics of the premultiply filter, its available options, and how to apply it to single RGBA streams or separate color and matte inputs.

What Does the Premultiply Filter Do?

In straight (unassociated) alpha, the color components (Red, Green, Blue) and the transparency component (Alpha) are stored independently. In premultiplied (associated) alpha, each color value is multiplied by the alpha value:

\[\text{Color}_{\text{premultiplied}} = \text{Color}_{\text{straight}} \times \left(\frac{\text{Alpha}}{\text{Max Alpha}}\right)\]

FFmpeg's premultiply filter performs this calculation directly within the command line. It can process a single stream that already contains an alpha channel or combine two separate streams where one represents the color and the other acts as an alpha matte.

Premultiplying a Single Stream with an Alpha Channel

If your input file already contains an alpha channel (such as a PNG sequence, WebM with alpha, or ProRes 4444), you can use the premultiply filter via simple video filtering (-vf).

ffmpeg -i input.mov -vf "premultiply" -c:v prores_ks -profile:v 4 output.mov

In this command:

  • -i input.mov: Specifies the source media containing an alpha channel (e.g., in rgba or yuva444p pixel format).
  • -vf "premultiply": Applies the premultiplication to the color planes using the file's existing alpha channel.
  • -c:v prores_ks -profile:v 4: Encodes the output using ProRes 4444 to preserve the alpha channel.

Premultiplying Separate Color and Alpha Streams

When your color data and alpha matte are stored in separate files, pass both streams into filter_complex. The premultiply filter uses the first stream as the color source and the second stream as the alpha source.

ffmpeg -i color.mp4 -i matte.mp4 -filter_complex "[0:v][1:v]premultiply[out]" -map "[out]" -c:v png output.mov

In this command:

  • [0:v]: The first input containing the primary color information.
  • [1:v]: The second input containing the grayscale transparency matte.
  • premultiply: Multiplies the color channels of [0:v] by the alpha values derived from [1:v].

Filter Parameters

The premultiply filter accepts two primary parameters to fine-tune how frames are processed:

1. planes

Specifies which planes to filter. By default, it processes planes 1, 2, and 3 (the color planes) while leaving plane 0 or the alpha plane intact, depending on the pixel format. The value is a bitmask where:

  • 1: First plane
  • 7: Planes 0, 1, and 2
  • 15: All four planes

Example:

ffmpeg -i input.mov -vf "premultiply=planes=7" -c:v prores_ks -profile:v 4 output.mov

2. inplace

Determines whether the filter operates in-place on the input video frames.

  • 0 (default): Operates on a newly allocated frame.
  • 1: Enables in-place processing, which can reduce memory consumption and slightly improve processing speed.

Example:

ffmpeg -i input.mov -vf "premultiply=inplace=1" -c:v prores_ks -profile:v 4 output.mov

Reversing Premultiplication

If you ever need to restore a premultiplied file back to straight alpha for editing or color grading, FFmpeg provides the complementary unpremultiply filter, which divides the color channels by the alpha values:

ffmpeg -i premultiplied.mov -vf "unpremultiply" -c:v prores_ks -profile:v 4 straight.mov