Convert video file into gif file through command line in linux

In this post, learn how to convert video file into gif file through command line in linux. For converting the video file into gif file there are many methods. We tried most of the methods. We used some software package like mplayer and image-magick.

Image-magick has one issue that is related to memory allocation. With image-magick we were able to convert jpg to gif files but the problem was occurring when we were using lot of files to convert.

Below is the sample of error

linux@tuxworld:~/Desktop$ convert -delay 5 output/*.jpg gif-example.gif
convert.im6: memory allocation failed `gif-example.gif’ @ error/quantize.c/QuantizeImage/2724.
convert.im6: memory allocation failed `gif-example.gif’ @ error/gif.c/WriteGIFImage/1636.
linux@tuxworld:~/Desktop

After getting this error many times, we finally decided to go with ffmpeg and gifsicle . Which we found very much effective.

Follow the steps to convert video file

Step 1: We assume you already have video file , here we have created a mp4 file with the help of kazam software in Ubuntu.

Install ffmpeg and gifsicle.

sudo apt-get install ffmpeg gifsicle

Step 2: Creating a output directory and using ffmpeg to create multiple gif files as frames.
In below given example, through ffmpeg command we are creating multiple file inside output directory

Here,
-r = frame rate
-i = input file

mkdir output
ffmpeg -i linux-tutorial.mp4 -r 10 output/out%04d.gif 

Step 3: Now aggregate all gif files into single gif file with the help of gifsicle command
Here,
-d = delay

#(This example is without loop)
gifsicle -d 10  output/*.gif > linux-example.gif

#(This example is with loop)
gifsicle -d 10 --loop  output/*.gif > linux-example2.gif

Optimize the file size of gif

After creating a gif file from above given steps. If you have noticed the file size of gif file is bigger.
You can optimize the gif file size by using below given command example

gifsicle -O2 linux-example.gif -o new_linux-example.gif

Now check the file size of newly created gif file by using command du -sh new_linux-example.gif

Given below sample is loop GIF image

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.