Convert mp4 to webm with transparency?

mikemaccana picture mikemaccana · Jan 8, 2016 · Viewed 9.1k times · Source

I know how to convert mp4 to webm with ffmpeg:

ffmpeg -y -i me939371029.mp4 -r 30  out3.webm

But I'd like to use webm transparency. That guide uses Blender, but Blender's a desktop tool that's not easily automated and only outputs PNGs that must be converted to video. I'd like a command line app that accepts video in, a color, and a video out. E.g.:

some-app -i video.mp4 -transparent ff0000 -o video.webm

I'd be surprised if something like this wasn't already in ffmpeg, but I can't seem to find it.

Answer

Gyan picture Gyan · Jan 8, 2016

Assuming the color to be keyed out is 00ff00, use

ffmpeg -i input.mp4 -c:v libvpx -vf "colorkey=0x00ff00:0.1:0.1,format=yuva420p" out.webm

In colorkey=0x00ff00:0.1:0.1

Parts are seperated by :. The first part is the key color. The color

0x00ff00

is green.

the 2nd is similarity

0.01 matches only the exact key color, while 1.0 matches everything.

and the 3rd is blend percentage

0.0 makes pixels either fully transparent, or not transparent at all.

Higher values result in semi-transparent pixels, with a higher transparency the more similar the pixels color is to the key color.

See the ffmpeg colorkey documentation


Resolving auto_alt_ref error

When you run into an error saying: Transparency encoding with auto_alt_ref does not work Error initializing output stream 0:0 -- Error while opening encoder for output stream #0:0 - maybe incorrect parameters such as bit_rate, rate, width or height

You can disable auto_alt_ref by adding the parameter -auto-alt-ref 0