Convert mp4 to gif

by Halí - posted: November 15, 2019
MP4GIFImageMagickFFmpeg

We, dev people, have the need to post evidence of different behaviors of our desktop in many places (issues, documentation, blogs, etc.).

In this post, We’ll learn how to convert an mp4 file to gif in two simple steps.

Requirements

Instructions

First, we need to extract the frames from our video. FFmpeg can do it with the next command:

ffmpeg -i video.mp4 -vf fps=5 frames/%03d.png

The fps option defines the playback speed of the gif, I suggest between 5 and 6 to get closer to speed of the original video.

Then we “glue” the frames to make the gif.

convert frames/* output.gif

With this, we have ready our gif, but maybe it’s too big, so we can do the next step. Gif optimization.

convert output.gif -fuzz 1% -layers Optimize optimized.gif

Extra

  1. For the naming part: %03d.png we need to multiply fps times seconds in the video, so, if our video lasts up to 166 seconds (2 minutes and 46 seconds) and we are using 6 fps, we can use %03d.png, but if the video is longer, it could be necessary change the names to %04d.png, in order to have the frames sorted.

  2. The fuzz percentage partly determines the optimized gif quality, that’s why we use only 1%, you will notice that increasing that percentage will reduce the final quality.