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.
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.
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.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With