I'm developing android application that converts mp4 files into gifs using ffmpeg. Problem is that generated gifs are huge in size. And another problem is that I can't use anything else than ffmpeg(e.g.imagemagick for convert, or even palletes for now) to reduce generated gif size.

this is the command I'm using: ffmpeg -y -i file.mp4 -pix_fmt rgb24 -r 10 -s 320x480 file.gif

So is there any other way to optimize conversion?

| improve this question | | | | |
  • try magick out.gif -fuzz 30% -layers Optimize result.gif – Foad Apr 25 at 11:52

The standard way to use ffmpeg for GIFs is

Generate a palette from the video

ffmpeg -y -i file.mp4 -vf palettegen palette.png


ffmpeg -y -i file.mp4 -i palette.png -filter_complex paletteuse -r 10 -s 320x480 file.gif

More options documented here.

| improve this answer | | | | |
  • 6
    Weirdly, generating a palette tripled the size of the GIF, although it greatly increased the quality over the default palette. I found the best way is to generate the GIF with ffmpeg as per usual (possibly with a better palette as in this answer) then just run it through an optimization tool (there's ones online too) that can make unchanging parts of the frame transparent or apply other optimizations. – Jason C Oct 27 '16 at 16:48
height=ih/2      # input height halved , can replace with pixils . 
width=-2         # keeps aspect ratio . can replace with pixils . 
fps=25           # frames per a second .


ffmpeg -ss $start_time                             \
       -t  $duration                               \
       -i  "$vid"                                  \
       -vf "$filters,palettegen"                   \
       -y  palette.png                             &&
ffmpeg -ss $start_time                             \
       -t  $duration                               \
       -i  "$vid"                                  \
       -i  palette.png                                \
       -lavfi "$filters [x]; [x][1:v] paletteuse"  \
       -y  "$vid".gif                              &&
rm palette.png 

link to documentation

more info

| improve this answer | | | | |
  • Where is $palette being set in your script? – confetti Aug 15 '18 at 8:47
  • where the command is run – FireInTheSky Aug 15 '18 at 9:08
  • 1
    sorry . there was a mistake with palette file . fixed now . – FireInTheSky Aug 27 '18 at 11:45

I tried all these techniques and I ended up using https://ezgif.com

I have no idea what magic they're using under the hood, but their compression level knob, palette optimization and resolution reduction resulted in the highest visual quality and smallest file size.

I tried -vf palettegen and -filter_complex paletteuse but that removed the ability to -vf "scale=iw*.2:ih*.2" in the same run so I had to do that separately. And ffmpeg created larger files for 0.3 scale than 0.5 which is odd. After an hour of fiddling, I tried ezgif and it worked nicely.

| improve this answer | | | | |

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.