Creating GIFs From Videos
While it is easy to embed videos in web-sites, sometimes you want the immediacy that you get an image. GIFs are the obvious answer to this "a video in an image file". Creating them is not always obvious though.
There are many pieces of software that can be used. On Linux, one is gifcurry which is not bad and interestingly is written in Haskell, a sophisticated type-safe functional language. Gifcurry does suffer from a few limitations such as for long videos however.
There are many pieces of software that can be used. On Linux, one is gifcurry which is not bad and interestingly is written in Haskell, a sophisticated type-safe functional language. Gifcurry does suffer from a few limitations such as for long videos however.
ffmpeg
After some experimentation, the most reliable mechanism I've found is old faithful command-line utility ffmpeg. Once you know the correct arguments to pass, it is reliable and fast.
You do it in two steps. First you extract a colour palette from the video, then you create the GIF using that palette:
ffmpeg -i <Video File>.<extension> -filter_complex "[0:v] palettegen" <palette file>.png
ffmpeg -i <Video File>.<extension> -i <palette file>.png -filter_complex "[0:v][1:v] paletteuse" <GIF File>.gif
For example, if your video file is MyVideo.mpg and your want to create MyVideo.gif, then you'd do:
ffmpeg -i MyVideo.mpg -filter_complex "[0:v] palettegen" palette.png
and then
ffmpeg -i MyVideo.mpg -i palette.png -filter_complex "[0:v][1:v] paletteuse" MyVideo.gif
Many variations can be executed using ffmpeg - the documentation is extensive. For example, here's how to speed up or slow down a video prior to creating a GIF - which can be useful.
Other Tricks
A couple of other useful methods when you're creating GIFs (or other work)
Determine Resolution
ffprobe -v error -select_streams v:0 -show_entries stream=width,height -of csv=s=x:p=0 input.mp4
The result is just the text 1280x720 or similar
Cropping Video
ffmpeg -i in.mp4 -filter:v "crop=<width>:<height>:<left>:<top>" out.mp4
where "width" and "height" are of the resulting video; and "left" and "top" are for the top-left point of where the crop rectangle is positioned in the original.
Comments
Post a Comment