Convert an mp4 video to an animated gif with Ffmpeg
If you want to convert an mp4 video file in to an animated gif, all you have to do is a quick search, and your search engine will show you a ton of websites that can do this for you. Many websites out there offer this service for free, which is great of course, but I always find there to be certain limitations that I don't care for much. Often the output file is of a lower resolution, or it can only be a couple of seconds long, or it has added a watermark that ruins the gif completely. What if I told you that you can use a really simple command for Ffmpeg to convert your video to gif, without any of the hassle? There are different ways to create a gif from our mp4, and I'll start by showing the quick and dirty way first. Be aware that the results of this first method are often not as good as you might be expecting.
ffmpeg -i input.mp4 -vf "fps=25,scale=iw:ih" output.gif
This pretty much speaks for itself right? Simply replace input.mp4 with the filename of your video file. Be aware that you will need to add "" to the filename when the filename contains any spaces. Optionally you can specify the filename for the gif file. Like I mentioned earlier, the results of this method may vary, and not up to your wishes.
This next method is a lot better, and you should use this one in nearly all situations. It does add an extra step, but that shouldn't be a reason not to use this method.
ffmpeg -i input.mp4 -vf "fps=25,scale=iw:ih:flags=lanczos,palettegen" palette.png
This extracts the color palette from the source mp4 file, stores it in a .png file, so it can be used as input to create a much better gif in the end. Once it has created the png file, you can proceed to this next step shown below.
ffmpeg -i input.mp4 -i palette.png -filter_complex "fps=25,scale=iw:ih:flags=lanczos[x];[x][1:v]paletteuse" output.gif
Note that we added the palette png we just created to the input variables. This method may take a second or two longer, but the results are certainly worth it. Give it a try yourself, you'll be amazed by how much this improves the quality of the gif.
These two images really show the difference in quality. It can best be noticed when you take a close look at the background.


Recent Comments