Make a gif from a video using ffmpeg and ImageMagick on Linux

05 Jan 2018 ᛫ 2 min read

SHARE:

Method 1

This method extracts intermediate frames from the video that will be used to create the gif. The output folder where the frames will be saved should already exist.

Step 1: Extract frames

ffmpeg -i <input.mp4> -r 10 <path-to-output-folder>/frame-%03d.png

Step 2: Convert the frames to a gif

convert -delay -10 loop 0 -layers Optimize <path-to-output-folder>/*.png <output.gif>

Method 2

This method option forgoes the intermediate frames by piping the outputs of the ffmpeg extraction directly to the convert command.

ffmpeg -i <input.mp4>  -r 10 -f image2pipe -vcodec ppm - | convert -delay 10 -loop 0 -layers Optimize - <output.gif>

Useful ffmpeg and convert options

You can check the help or man pages of ffmpeg and convert for more details about the available options but here are a few that I’ve found to be the most useful.

ffmpeg

convert

Skip the first 10 seconds of the video and make a 5 second gif

Add -ss 10 and -t 5 to the ffmpeg command.

Using method 2:

ffmpeg -ss 0 -t 25 -i <input.mp4> -r 10 -f image2pipe -vcodec ppm - | convert -delay 10 -loop 0 -layers Optimize - <output.gif>

Force gif to a different resolution than the video source

Add -vf scale=n:-1:flags=lanczos to the ffmpeg command where n is the width you want for the output gif.

Using method 2 and assuming I want a gif with a width of 1080 pixels:

ffmpeg -i <input.mp4> -vf scale=1080:-1:flags=lanczos -r 10 -f image2pipe -vcodec ppm - | convert -delay 10 -loop 0 -layers Optimize - <output.gif>

SHARE:
comments powered by Disqus

You may also like:

Convert a PDF to an Image (PNG, JPG, etc) in Linux using ImageMagick

05 Oct 2018 ᛫ 1 min read

My new daily driver

15 Feb 2020 ᛫ 6 min read


BNHR

[email protected]

Creative Commons License
This work and its contents by Ben Hur S. Pintor is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.
Other works (software, source code, etc.) referenced in this website are under their own respective licenses.
This site is powered by Jekyll and hosted on Github (view source)