How to Unpremultiply Alpha in FFmpeg
This article explains how to use the unpremultiply
filter in FFmpeg to reverse channel premultiplication. You will learn
what premultiplied alpha is, why you need to reverse it, and how to use
the FFmpeg command-line syntax with practical examples to restore your
video’s color channels to their straight (unpremultiplied) state.
Understanding Premultiplied Alpha
In digital video and imagery, transparent assets can represent alpha (transparency) in two ways: * Straight Alpha: The RGB color channels and the Alpha channel are kept completely independent. * Premultiplied Alpha: The RGB color values are already multiplied by the Alpha value. This is often used to render semi-transparent edges more efficiently during compositing.
If you perform color correction or resizing on a premultiplied video, you can introduce dark halos or color fringing around transparent edges. Reversing this multiplication—known as “unpremultiplying”—restores the original color values so you can process the image accurately.
The FFmpeg
unpremultiply Filter Syntax
The unpremultiply filter in FFmpeg divides the color
channels (RGB) of a video by its alpha channel.
The basic filter syntax is:
unpremultiply[=inplace]Filter Options
inplace: A boolean flag (0 or 1). Setting this to1enables in-place processing, which is faster and uses less memory. The default value is0.
Practical Command Examples
Example 1: Basic Unpremultiplication
To reverse premultiplication on an input video or image sequence and output the result, use the following command:
ffmpeg -i input_premultiplied.mov -vf "unpremultiply=inplace=1" output_straight.movIn this command: * -i input_premultiplied.mov specifies
the source video that contains premultiplied alpha. *
-vf "unpremultiply=inplace=1" applies the video filter,
processing the pixel division in place for optimal performance.
Example 2: Unpremultiplying and Applying Other Filters
If you need to color-correct or scale your video, you should unpremultiply first, apply your filters, and then premultiply again (if your delivery format requires it).
Here is how to unpremultiply, adjust the gamma/contrast using the
eq filter, and output the video:
ffmpeg -i input_premultiplied.mov -vf "unpremultiply=inplace=1,eq=gamma=1.2:contrast=1.1" output_corrected.movBy placing unpremultiply at the beginning of the
filterchain, you ensure that the color adjustments do not corrupt the
semi-transparent edge pixels.