Some time ago I created a helper to convert videos to gif using ffmpeg and gifsicle. This script provides a set of options through a simple interface and, in case you don't have those libraries installed, it does everything inside a docker container.
If you are not interested in hearing about this tool, here is a simple command that does the job:
ffmpeg -i [input video] -f gif - | gifsicle --optimize=3 > [output gif]
However, if like me, you don't like to install random libraries and their dependencies in your main system, or maybe you just find this command too hard to remember, keep reading and I will tell you a bit more about it.
Options
Those are all the options available in video2gif:
video2gif [OPTIONS] [input file]
-s, --size size. e.g 600x400. Default: same as video size
-o, --output output file name. Default: [input].gif.
-i, --input input file.
-ts, --start-time time position from video to start gif. Seconds or HH:mm:ss. Default: start of the video
-te, --end-time time position from video to stop gif. Seconds or HH:mm:ss. Default: end of the video
-d, --gif-frame-duration delay/duration of each Gif frame in hundredths of a second. Default: 3.
-fr, --video-frame-rate video frame rate. Less frames generate smaller gifs. Used by ffmpeg. Default: 10.
-h, --help print help message.
-v, --version print version.
Everything you do with video2gif you can do with ffmpeg and gifsicle. Because of that, I will show you how to use both commands, so you don't need to download my tool if you don't want to.
Defining an output size:
video2gif
video2gif -s 600x400 input.mpeg
ffmpeg + gifsicle
ffmpeg -i input.mpeg -s 600x400 -f gif - | gifsicle --optimize=3 > output.gif
Starting at video position:
video2gif
video2gif -ts 1:05 input.mpeg
ffmpeg + gifsicle
ffmpeg -ss 1:05 -i input.mpeg -f gif - | gifsicle --optimize=3 > output.gif
Note: -ss
needs to come before -i
, otherwise it won't work.
Stopping at video position:
video2gif
video2gif -te 3:00 input.mpeg
ffmpeg + gifsicle
ffmpeg -to 3:00 -i input.mpeg -f gif - | gifsicle --optimize=3 > output.gif
Note: as in the previous example, argument position matters.
Set duration for each gif frame
Duration is defined by hundredths of a second, so 3 is equivalent to 0.03 second.
video2gif
video2gif -d 3 input.mpeg
ffmpeg + gifsicle
ffmpeg -i input.mpeg -f gif - | gifsicle --optimize=3 --delay 3 > output.gif
Define the number of frames per second
Fewer frames generate smaller gifs, with the expense of making it choppier.
video2gif
video2gif -fr 10 input.mpeg
ffmpeg + gifsicle
ffmpeg -i input.mpeg -r 10 -f gif - | gifsicle --optimize=3 > output.gif
All options example
video2gif
video2gif -ts 10 -te 1:10 -s 400x200 -d 3 -fr 10 input.mpeg
ffmpeg + gifsicle
ffmpeg -ss 10 -to 1:10 -i input.mpeg -s 400x200 -r 10 -f gif - | gifsicle --optimize=3 -d 3 > output.gif
Conclusion
As you can see, it is not that hard to use ffmpeg+gifsicle, however, if you do this conversion frequently you may consider using my script.
I hope it was useful. To know more about how it works, you can check the repository.