Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

FFMPEG GIF with transparency from png image sequence

Tags:

ffmpeg

gif

I've been trying to use ffmpeg to create a gif with a transparent background, but whenever the movement goes on top of the background, the pixels stay there. It's a tree with a wind animation, this is how it ends up: https://i.imgur.com/pq4ArBG.png

I first try to create the palette, and then the gif:

ffmpeg -i Tree_%04d.png -vf palettegen=reserve_transparent=1 palette.png

ffmpeg -framerate 30 -i Tree_%04d.png -i palette.png -lavfi paletteuse=alpha_threshold=128 treegif.gif

It seems the previous frames simply stay there, but I can't figure out how to dispose of them.

like image 975
Nick S Avatar asked Nov 30 '18 23:11

Nick S


People also ask

Does GIF and PNG support transparency?

PNG files almost always offer better compression and a reduced file size compared to GIF. The PNG format also supports variable transparency and millions of colors while GIF only supports 256 colors and does not offer alpha channels.

Can a .GIF file can contain transparency in an image?

The GIF and PNG formats also both support transparency. If you need any level of transparency in your image, you must use either a GIF or a PNG. GIF images (and also PNG) support 1-color transparency. This basically means that you can save your image with a transparent background.

Can GIF preserve transparency?

Background transparency, supported by the GIF and PNG formats, preserves transparent pixels in the image and allows the background of the web page to show through the transparent areas of your image.

Why does my transparent GIF have a white outline?

This happens because the edges of the image need to gently transition into the background colour. In this example Photoshop has assumed that the background will be white, so it has faded the edges towards white.


1 Answers

In the ffmpeg source code (libavcodec/gif.c) there are some command line flags - these are "-gifflags -offsetting", "-gifflags -transdiff" and "-gifimage". I tried these on some sample frames with transparency, and I think the first of these produces the effect you describe:

ffmpeg -i Tree_%04d.png -vf palettegen=reserve_transparent=1 palette.png
ffmpeg -framerate 30 -i Tree_%04d.png -i palette.png -lavfi paletteuse=alpha_threshold=128 -gifflags -offsetting treegif.gif
like image 97
kshepherd Avatar answered Dec 17 '22 14:12

kshepherd