Hi!

Sometimes it’s useful to display videos instead of images to show certain things.

But a video requires a player and not every browsers are capable to play all kind of videos, whose can have many different formats and codecs.

On the other side images are easy to both be uploaded/embedded to a website and to be displayed by who’s surfing the web.

A GIF (Graphical Interchange Format) can contain frames, like a video, so it can be animated as a video, but it’s, in fact, an image format.

Luckily for us we have a free, open source and extremely powerful software named FFMPEG that can also convert a video to gif quite easily.

Now this great software can do many more stuff and can be extremely complicated, but i’m going to explain just what we need here and no more. It has to be used from the command line, and every command needs an input and an output which are formatted in bold and italic, the other options that i’m going to add are in red, and i’m going to explain them and put a link to the official ffmpeg’s documentation.
This page can be super heavy to load, also when you open a gif, give it some seconds to be fully loaded before judging it’s quality

Let’s install FFMPEG: first let’s check what version your distro has, ubuntu 16.04 has 2.8.6 in its repository which is fine, install it!

Ok, now we can start using ffmpeg! the base command is:

ffmpeg -i video.mp4 image.gif

this convert your video to a gif with the same size and other features (always remember to put -i before every input you want to use)

I did 4 videos with my phone at 720p, here are the results:

I’m going to discuss the image quality later, now let’s talk about resizing the images from the native 720p to 480p

ffmpeg -i video.mp4 -s hd480 image.gif

-s stands for size and can be either a width x height value or a recognized abbreviation

ps. if your video is vertical, you may need to switch WxH to HxW
in my case -s 480×852

take a look:

Another good option is to reduce the gif’s length, this is helpful if you have a long video, like an entire movie or a live show

ffmpeg -ss 10 -t 5 -i video.mp4 image.gif

-ss set a position where the stream is going to start

-t set the duration of the stream

both options can be either in Seconds or Hours:Minutes:Seconds

in the example above the gif will be 5 seconds long and will contain the frames of the video starting from 10 seconds after its start point

I made all my gif 2 seconds long:

If you want to decrease your gif size as much as possible without loosing quality or resizing it, you can set a static framerate value.
This can be helpful when your video source is a gameplay, which can contain a lot of frames per seconds (e.g. 100 or more), set the framerate at 60 or 30 can heavily decrease the gif size.

ffmpeg -i video.mp4 -r pal image.gif

-r as i wrote stands for rate and can be a value or a recognized abbreviation

the pal value set the framerate to 25:

Now let’s talk about the gifs’ quality. You may have noticed that they aren’t the same quality as the input video and that there is a sort of layer covering them.
The main reason is because a standard set of colors (palette) is used to convert the video; or goal is to generate a specific palette for our stream, then to use them for a better result:

ffmpeg -i video.mp4 -vf palettegen palette.png

then:

ffmpeg -i video.mp4 -i palette.png -lavfi paletteuse image.gif

(you can run in one command by adding && between them)

-vf and -lavfi let you add a Video Filter to your output

there are soooo many options more to add, just take a look at the filters documentation and try them all (ok maybe not..)

a good one is the saturation, which can create black&white, reverse colors or max saturation

ffmpeg -i video.mp4 -vf hue=s=0 image.gif

or

ffmpeg -i video.mp4 -i palette.png -lavfi paletteuse,hue=s=0 image.gif

hue=s=0

hue=s=10

hue=s=-1

that’s it :)

TA SALÜDE