Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fastest way to create animated gifs from mp4

I have to batch process a very high number(200,000+) of short mp4s(less then 10s) into animated gifs. I think I have tried every possible command line way of doing it, but it still takes too long to produce quality gifs. Currently I use ffmpeg to unpack image(jpg) frames, and image magick convert to assemble them. It produces high quality gifs, but takes too long and uses too much memory and CPU, even with tweaking with the -limit memory and map limits.

I have also tried creating animated gifs directly using ffmpeg, but the quality is terrible at best. Also, I have tried using gifsicle, which does seem fast, but it only takes gifs as inputs, and using ffmpeg to generate them produces poor quality gifs.

Does anyone have some command line recipes for fast high quality gif creation?

like image 569
stinkypyper Avatar asked Jun 20 '13 18:06

stinkypyper


1 Answers

For first you should create a set of jpg images with maximum quality using mplayer and jpeg option sets to 100 (jpeg:quality=100).

mplayer -ao null -ss 0:00:00 -endpos 10 mts.flv -vo jpeg:outdir=jpeg_dir:quality=100

Next, you need to convert generated jpgs to gifs using convert, just type:

for i in ./jpeg_dir/*.jpg; do convert "$i" "${i%.jpg}.gif"; done

And finally using gifsicle create animated gif:

gifsicle --delay=10 --loop ./jpeg_dir/*.gif > anim.gif

Also, you can use optimization level flag --optimize=03. It can be helpful to reduce file size:

gifsicle --delay=10 --optimize=03 --loop *.gif > anim.gif

Additionally, you can manipulate with number of colors --colors num of current palette and --color-method method to determine most appropriate palette.

As for me most appropriate method is median-cut

median-cut is the median cut algorithm described by Heckbert

Also, I try to manipulate with described flags and found most useful options to achieve better quality of generated gif-image:

gifsicle --delay=3 --optimize=03 --color-method median-cut --loop *.gif > anim.gif

Be accurate with --delay=NUM to correspond animation speed . This param depends on FPS of original video.

like image 60
Alexander Borodulya Avatar answered Sep 19 '22 23:09

Alexander Borodulya