How to Create Animated GIF from Video Using Ffmpeg
Posted By : Yasir Zuberi | 30-Dec-2014
You must have heard of animated GIF files. GIF files provides us a unique replacement to short video clips. A GIF file is less in file-size and can deliver more information as compared to short video clips. We can find usage of GIF files in social networking sites, gaming sites, advertisement portals and many more.
In this blog, I am going to explain you two simple and straightforward ways to easily create an animated gif file from a video file using ffmpeg.
1:- Direct conversion of Video to GIF using ffmpeg
ffmpeg -i inputVideo.mp4 -ss 00:00:05 -t 00:00:02 -vf scale=320:-1 -r 10 directVideotoGif.gif
Explanation of command:-
-i inputVideo.mp4 specifies the video file which ffmpeg tool will use.
-ss 00:00:05 specifies ffmpeg tool the start time of conversion.
-t 00:00:02 specifies that video will convert from start time to next 2 seconds.
-vf scale=320:-1 to convert the file at the width of 320 pixels and ffmpeg tool will automatically give height maintaining the aspect ratio.
-r 10 specifies the frame rate to 10 fps.
directVideotoGif.gif is the fileName of converted gif file.
Note:- Above ffmpeg's command will give an average quality of image but if you want to improve image quality you can opt for below option.
2:- Conversion of Video to GIF using ffmpeg and ImageMagick.
Here first we will get the frames from our video file and then covert the frames into animate GIF image using ImageMagick.
mkdir frames //create a directory to store frames from a video file.
ffmpeg -i inputVideo.mp4 -ss 00:00:05 -t 00:00:02 -vf scale=320:-1 -r 10 frames/ffout%03d.png
//This command is similar to the previous command, here we are getting frames as png files.
convert -delay 5 frames/ffout*.png converted.gif
//This command is from ImageMagick, where it converts all the png format files in frames directory into a gif file. You may change -delay value to increase/decrease the animation speed of your GIF file.
Note:- I am assuming that you have ffmpeg and ImageMagick installed on your system.
Cookies are important to the proper functioning of a site. To improve your experience, we use cookies to remember log-in details and provide secure log-in, collect statistics to optimize site functionality, and deliver content tailored to your interests. Click Agree and Proceed to accept cookies and go directly to the site or click on View Cookie Settings to see detailed descriptions of the types of cookies and choose whether to accept certain cookies while on the site.
About Author
Yasir Zuberi
Yasir is Lead Developer. He is a bright Java and Grails developer and have worked on development of various SaaS applications using Grails framework.