Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

colors messed up (distorted) when making a gif from png files using ffmpeg

Tags:

png

ffmpeg

gif

I have a sequence of png images: image_00.png, image_01.png, image_02.png, etc. I want to convert them to a gif, so I tried the command

ffmpeg -i image_%02d.png video.gif

Unfortunately, the resulting gif has distorted colors. More specifically, it added a weird sort of yellow haze around some objects in the video.

I also tried using the command above with all possible pixel format options (which I determined using the command ffmpeg -h encoder=gif): rgb8, bgr8, rgb4_byte, bgr4_byte, gray, pal8. For example ffmpeg -i image_%02d.png -pix_fmt rgb8 video.gif. Unfortunately, all of the resulting gifs had some sort of color distortion.

I also observed that this distortion does not occur if I convert the images to mp4 instead of gif. However, if I try converting that mp4 to a gif, I end up with the distortion again.

How can I produce this gif without color distortion?

like image 255
Trevor Avatar asked Nov 13 '19 07:11

Trevor


People also ask

Can FFmpeg make GIFs?

By default, FFmpeg uses a generic 256-color palette for every GIF encoding and doesn't take into account the colors in the input video. However, we can use the palettegen and paletteuse filters of FFmpeg to generate a custom palette from the colors in our original video to create a higher-quality GIF.


1 Answers

I was able to fix this problem by first generating a palette for all of the png images. I then used that palette to create the gif:

ffmpeg -i image_%02d.png -vf palettegen palette.png
ffmpeg -i image_%02d.png -i palette.png -lavfi paletteuse video.gif

For more info, see the "palettegen" and "paletteuse" sections of https://ffmpeg.org/ffmpeg-filters.html

like image 123
Trevor Avatar answered Oct 18 '22 21:10

Trevor