I'm trying to extract a GIF from an MP4 video file.

Now I have two modi with which I do this:

1) the regular one:

ffmpeg -t 7.7 -ss 00:22:53 -i video.mp4 -i palette.png -s 320x174 -filter_complex "fps=15" output.gif

which gives out a file, 1.6MB of size.

and 2)

ffmpeg -t 7.7 -ss 00:22:53 -i video.mp4 -i palette.png -filter_complex "fps=15,scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif

which gives out a way better looking, but 4.5MB (181% increase) big file. It uses a 16x16 palette file, which I created with

ffmpeg -y -t 7.7 -ss 00:22:53 -i video.mp4 -vf fps=15,scale=320:-1:flags=lanczos,palettegen palette.png

Now I can't quite see how 2 GIFs of the same fps, duration and resolution can have such a huge difference in size. My intuition would be, they should be the same. Why is there this difference? And is there any way to cut the size of 2) to the one of 1) without losing quality?


  • 1
    I don't think your first command makes use of the palette file. Create a palette image from the GIF created and compare it with the palette from the 3rd command. – Gyan Jan 27 '16 at 17:56
  • That is on purpose. The point is, that both use 2 different palettes of 256 colors (the first the standard one and the second a generated one), but differ hugely in size. – nfisher Jan 27 '16 at 18:32
  • Yes, fewer colors are used from the standard palette and they are repeatedly used, so LZW can compress it more effectively. With the tailored palette, use is more evenly spread across the 256 available choices. – Gyan Jan 27 '16 at 18:52
  • Imagine you wanted to make a GIF of a grayscale image with a 8 color palette. Palette A had black, white and 6 shades of red and blue. Palette B had a ramp from black to white. The GIF encoder will use only black and white from Palette A, so the compressor has to represent occurrences of two entries. From Palette B, all swatches will come in use in varying degrees. – Gyan Jan 27 '16 at 18:54
  • This may help: blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html – slhck Jan 28 '16 at 8:12

Because of how GIFs work, the more complicated the palette, the resulting gif becomes larger pretty quickly.

I would use GIMP to optimize the resulting GIF from #2 - not 100% sure how it works, but think it basically deletes duplicate information from one frame to the next.

GIMP Screenshot

  • Well, the used palette is 16x16 pixels, which results in 256 data points. As far as i am concerned, the resulting GIFs should not differ at all in size as long as both palettes used are the same size (and everything else is equal). – nfisher Jan 27 '16 at 18:34
  • 2
    Apologies for my incomplete knowledge on the subject, but I believe it has something to do with how the GIF compression system (LZW) works: For instance, it might be that the two different palettes result in differing levels of dithering- Dithering is the random dot pattern that results when colors are approximated by mixing similar and available colors from a limited palette. Dithering is relevant to GIF file size because it interrupts the clean areas of flat color that are conducive to efficient LZW compression, and can make the file size larger than it needs to be. – HydroxCookieHoax Jan 27 '16 at 18:51
  • 1
    You are definitely more read in the subject matter than I am. Thank you for the comment. I suppose that dithering increases file size when more colors of a given palette is used, as Mulvya suggested. – nfisher Jan 28 '16 at 14:31

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.