Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm converting .flv movie to .gif file with ffmpeg.

ffmpeg -i input.flv -ss 00:00:00.000 -pix_fmt rgb24 -r 10 -s 320x240 -t 00:00:10.000  output.gif

It works great, but output gif file has a very low quality.

Any ideas how can I improve quality of converted gif?

Output of command:

ffmpeg -i input.flv -ss 00:00:00.000 -pix_fmt rgb24 -r 10 -s 320x240 -t 00:00:10.000  output.gif
ffmpeg version 0.8.5-6:0.8.5-0ubuntu0.12.10.1, Copyright (c) 2000-2012 the Libav developers
  built on Jan 24 2013 14:52:53 with gcc 4.7.2
This program is only provided for compatibility and will be removed in a future release. Please use avconv instead.
Input #0, mov,mp4,m4a,3gp,3g2,mj2, from 'input.flv':
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2013-02-14 04:00:07
  Duration: 00:00:18.85, start: 0.000000, bitrate: 3098 kb/s
    Stream #0.0(und): Video: h264 (High), yuv420p, 1280x720, 2905 kb/s, 25 fps, 25 tbr, 50 tbn, 50 tbc
      creation_time   : 1970-01-01 00:00:00
    Stream #0.1(und): Audio: aac, 44100 Hz, stereo, s16, 192 kb/s
      creation_time   : 2013-02-14 04:00:07
[buffer @ 0x92a8ea0] w:1280 h:720 pixfmt:yuv420p
[scale @ 0x9215100] w:1280 h:720 fmt:yuv420p -> w:320 h:240 fmt:rgb24 flags:0x4
Output #0, gif, to 'output.gif':
    major_brand     : mp42
    minor_version   : 0
    compatible_brands: isommp42
    creation_time   : 2013-02-14 04:00:07
    encoder         : Lavf53.21.1
    Stream #0.0(und): Video: rawvideo, rgb24, 320x240, q=2-31, 200 kb/s, 90k tbn, 10 tbc
      creation_time   : 1970-01-01 00:00:00
Stream mapping:
  Stream #0.0 -> #0.0
Press ctrl-c to stop encoding
frame=  101 fps= 32 q=0.0 Lsize=    8686kB time=10.10 bitrate=7045.0kbits/s dup=0 drop=149    
video:22725kB audio:0kB global headers:0kB muxing overhead -61.778676%


share|improve this question

migrated from Feb 22 '13 at 22:25

This question came from our site for professional and enthusiast programmers.

up vote 187 down vote accepted

ffmpeg can now output higher quality GIF. If you are using an older build consider downloading a recent static build or compiling ffmpeg.

ffmpeg example

gif with ffmpeg

This example will skip the first 30 seconds of the input and create a 3 second output. It will scale the output to be 320 pixels wide and automatically determine the height while preserving the aspect ratio. The palettegen and paletteuse filters will generate and use a custom palette generated from your source.

  1. Generate a palette:

    ffmpeg -y -ss 30 -t 3 -i input.flv \
    -vf fps=10,scale=320:-1:flags=lanczos,palettegen palette.png
  2. Output the GIF using the palette:

    ffmpeg -ss 30 -t 3 -i input.flv -i palette.png -filter_complex \
    "fps=10,scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse" output.gif

See High quality GIF with FFmpeg for many more examples and options.

convert example

Another command-line method would be to export the movie to frames using ffmpeg then make the GIF with convert from ImageMagick (or GraphicsMagick).

ffmpeg and convert

  1. Export frames:

     mkdir frames
     ffmpeg -i input -vf scale=320:-1:flags=lanczos,fps=10 frames/ffout%03d.png
  2. Then use convert (or gm convert if you prefer GraphicsMagick) to make your animated GIF:

     convert -loop 0 frames/ffout*.png output.gif

With convert you can control the delay between frames with -delay if desired.

share|improve this answer
Added some example results (just still frames though). Here, the first file is 4.1 MB, the second around 8 MB. – slhck Feb 22 '13 at 21:44
@slhck Nice addition. I should remember how much answers dealing with video and images are improved with actual images. – LordNeckbeard Feb 22 '13 at 21:46
@LordNeckbeard, you are awesome! much thanks for -vf scale=320:-1,format=rgb8,format=rgb24 – Kamil Hismatullin Feb 22 '13 at 21:53
@LordNeckbeard - the convert option worked brilliantly - also using an ancient version of ffmpeg directly got a file size of 300Mb with rubbish output, whilst the high quality one from convert was 13Mb... – Wilf Jul 5 '14 at 23:00
By the way, for the convert command for converting from the PNG frames I ended up using convert -delay 5 -loop 0 -dither None -colors 80 "frames/ffout*.png" -fuzz "40%" -layers OptimizeFrame "output.gif", which reduces the overall file size quite a bit – Wilf Jul 24 '14 at 13:58

If you would prefer to avoid intermediate image files, the commands provided by LordNeckBeard can be piped between ffmpeg and ImageMagick's convert so that no intermediate files are required:

ffmpeg -i input -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -loop 0 - output.gif

The -f image2pipe tells ffmpeg to split the video into images and make it suitable to be piped out, and -vcodec ppm specifies the output format to be ppm (for some reason if the format is png, either convert does not read all the images from the pipe, or ffmpeg does not output them all). The - for both commands specifies that a pipe will be used for output and input respectively.

To optimize the result without saving a file, you can pipe the output from convert to a second convert command:

ffmpeg -i input -vf scale=320:-1 -r 10 -f image2pipe -vcodec ppm - | convert -delay 5 -loop 0 - gif:- | convert -layers Optimize - output.gif

The gif:- tells convert to pipe its output as gif formatted data and -layers Optimize tells the second convert to perform optimize-frame and optimize-transparancy methods (see the ImageMagick Introduction to Animation Optimization). Note that the output from the -layers Optimize may not always provide a smaller file size, so you may want to try converting to a gif without optimization first to be sure.

Remember that during this whole process everything is in memory so you may need sufficient memory if the images are quite large.

share|improve this answer
This set of commands also works with avconv – raphael Dec 9 '15 at 2:00
Awesome move on not generating temporary files. – Caio Cunha May 16 at 18:53
You should merge the last two convert commands: convert -delay 5 -loop 0 -layers Optimize - output.gif – Clément Jul 5 at 5:50

As of ffmpeg 2.6, we can do even better:


ffmpeg -i input.flv -vf "$filters,palettegen" -y $palette
ffmpeg -i input.flv -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y output.gif


share|improve this answer

I made my own version of the script, which parameterizes the output resolution and frame rate as well. Usage:

Running ./gifenc output.gif 720 10 will output 720p wide 10fps gif from the movie you gave it. You might need to do chmod +x for the file.




ffmpeg -v warning -i $1 -vf "$filters,palettegen" -y $palette
ffmpeg -v warning -i $1 -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y $2

You can read the details on my Github

Assumtions: ffmpeg is installed, script is in same folder than other files.

share|improve this answer
Thank you so much for your script. I just tested it and it works great! – orschiro Feb 5 at 9:32

made a script, tested and works.


./ ./vokoscreen-2015-05-28_12-41-56.avi





# default settings, modify if you want.

START_AT_SECOND=0; # in seconds, if you want to skip the first 30 seconds put 30 here

LENGTH_OF_GIF_VIDEO=9999999; # in seconds, how long the gif animation should be

echo "Generate a palette:"
ffmpeg -y -ss $START_AT_SECOND -t $LENGTH_OF_GIF_VIDEO -i $INPUT -vf fps=10,scale=320:-1:flags=lanczos,palettegen palette.png

echo "Output the GIF using the palette:"
ffmpeg -ss $START_AT_SECOND -t $LENGTH_OF_GIF_VIDEO -i $INPUT -i palette.png -filter_complex "fps=10,scale=320:-1:flags=lanczos[x];[x][1:v]paletteuse" $INPUT.gif

btw: vokoscreen is an EXCELLENT ScreenCapturing tool for Linux :)

THANKS A LOT Michael Kohaupt :) Rock steady.

some file size stats:

5.3M = vokoscreen-2015-04-28_15-43-17.avi -> vokoscreen-2015-05-28_12-41-56.avi.gif = 1013K

see the results here.

share|improve this answer

For Windows users that want a batch file.


set start_time=5
set duration=60
set palette="c:\temp\palette.png"
set filters="fps=15,scale=-1:-1:flags=lanczos"
ffmpeg -v warning -ss %start_time% -t %duration% -i %1 -vf "%filters%,palettegen" -y %palette%
ffmpeg -v warning -ss %start_time% -t %duration% -i %1 -i %palette% -lavfi "%filters% [x]; [x][1:v] paletteuse" -y %2

Source: High quality GIF with FFmpeg: Extracting just a sample

share|improve this answer

I usually do this in photoshop, then I can visually see the effects of color depth and resolution on the quality of the end result gif. A basic tutorial is here:

It helps me to see the results I'm going to get vs. a command line method.

share|improve this answer
thanks for answer! hmm...but what if you have 200 video files or you want to integrate video convertation feature to your site? I think 'command line method' is the best way to solve that kinds of problems. – Kamil Hismatullin Feb 22 '13 at 21:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .