DEV Community

Marcin Piczkowski
Marcin Piczkowski

Posted on

Make a gif from a video quickly

Alt Text
-- Photo by James Wainscoat on Unsplash --

In this post I will show you how you can easily convert a video into gif with free command line tools.

All in 3 steps:

1 ) Install require tools

2 ) Record a video (in MOV format) which you'd like to convert later to a gif file.

3 ) Transform video into gif like that:

ffmpeg -i my.mov -s 1400x800 -pix_fmt rgb24 \
 -r 20 -f gif -  | gifsicle --optimize=3 --delay=3 > my.gif
Enter fullscreen mode Exit fullscreen mode

The above command takes file my.mov as input for ffmpeg program and produces my.gif as output. In addition it does some optimisations using gifsicle.

We could actually skip gifsicle and only run:

ffmpeg -i my.mov -s 1400x800 -pix_fmt rgb24 -r 20 -f gif my.gif
Enter fullscreen mode Exit fullscreen mode

The drawback is that the resulting gif would be large.

If you're not interesting about the details of what the command does you can skip reading here. Otherwise follow me till the end.


Below is a more detailed explanation of different options.

If you'd like to see a complete list of ffmpeg parameters and options with explanation check here or use command:

ffmpeg -h full
Enter fullscreen mode Exit fullscreen mode

Similarly for gifsicle run:

gifsicle -h
Enter fullscreen mode Exit fullscreen mode

The following are ffmpeg options which were used to create a gif:

-i flag prepends input file path (movie).

-s flag is used to set frame size, e.g. 1400x800 means width of 1400 and height of 800 pixels.

-pix_fmt sets pixel format, e.g. rgb24 which is a format with 24 bits per pixel. Each color channel (red, green, and blue) in video is allocated 8 bits per pixel. To put simply, pixel format is a kind of computer representation for color.

-r is a frame rate, in our case 20 frames per second. What happens under the hood is that if the original video has higher frame rate than 20 than ffmpeg will remove some frames and if it had lower frame rate it will duplicate some frames to obtain output video with desired number of frames per second.

-f flag is used to set filter for ffmpeg, e.g. gif filter will transform video into a gif.

And here are gifsicle options:

--optimize is used to shrink resulting gif. There are 3 levels supported. Level 3 will take more time to process but is the best optimization.

--delay sets the delay in time between gif frames in hundredths of a second, in our case it will be 0.03s. The larger the value the slower the gif will be but if the value is too high it will make impression of lags.

You can even set a handy shortcut in your bash to just type:

gifly input.mov output.gif
Enter fullscreen mode Exit fullscreen mode

You can do this by adding a function on Mac OS or Linux in your ~/.bash_profile or ~/.bashrc.

Add the following lines:

gifly() {
    ffmpeg -i "$1" -s 1400x800 -pix_fmt rgb24 -r 20 -f gif -  | gifsicle --optimize=3 --delay=3 > "$2"
}
Enter fullscreen mode Exit fullscreen mode

That's it, ffmpeg rocks! Enjoy creating gifs without any special software.

Top comments (0)