Convert video file into gif file through command line in linux
For converting the video file into gif file there are many methods. I tried most of the methods. I used the software package and try to convert with mplayer and image-magick.
Image-magick has one issue that is related to memory allocation. With image-magick I was able to convert jpg to gif files but the problem was occurring when I was 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.
After getting this error many times I finally decided to go with ffmpeg and gifsicle . Which I found very much reliable.
Follow the given below steps to convert video file
Step 1: I assume you already have video file , here I 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
-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
-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
Below given sample is loop GIF image