This is a small guide to using ffmpeg and gifsicle, two cross platform command-line tools to create a preview gif with a tiny file size using only 2 commands.
TL;DR version
- Install both ffmpeg and gifsicle
- Open a terminal in the location of the video you want to create a preview gif for
- Run these two commands
ffmpeg -ss 25 -to 30 -i "video.mp4" -filter_complex "fps=10,scale=360:-1[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" preview.gif
gifsicle --optimize=10 --lossy=100 -o preview-opt.gif preview.gif
- done, you now have a preview gif with a relatively small file size
Long version:
Install
Linux
Use your distros package manager to install both.Fedora:
sudo dnf in ffmpeg gifsicle
Arch:
sudo pacman -S ffmpeg gifsicle
Debian/Ubuntu:
sudo apt install ffmpeg gifsicle
Windows
For windows you have to install the binaries and then manually set environment variables so that windows recognizes the commands. I am installing this on Windows 11, but the process should be the same for 10 and very similar for older windows versions.First, download ffmpeg here. I use the essentials version.
https://www.gyan.dev/ffmpeg/builds/
and gifsicle here. The 64-bit version.
https://eternallybored.org/misc/gifsicle/
Then you need to make 2 new folders where you want them to live, I put them in C:\Program Files
so I now have:
C:\Program Files\ffmpeg
and
C:\Program Files\gifsicle
Now just extract the two zipped files you downloaded into those folders you just made.
It should look something like this:
In order for Windows to recognize those programs in the terminal, you must set their paths as environment variables.
First search for “environment” in the start menu and open the environment variables menu:
Select “Environment Variables…”
Select the “Path” Variable, then “Edit…”
Select “New” and add these two paths (or wherever you installed ffmpeg and gifsicle)
Note: You must include the \bin
folder inside of the ffmpeg folder
That’s it, they are both installed.
Using ffmpeg and gifsicle
The process to use these tools is the same on linux and windows, so I will show how I use them on linux.
I will use this Hatsune Miku script as an example
First, go to the folder with the video you want to create a gif from, then open a terminal in that location.
Then in the terminal, type this command, replacing "video.mp4"
with the name of the video file:
ffmpeg -ss 25 -to 30 -i "video.mp4" -filter_complex "fps=10,scale=360:-1[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse" preview.gif
This will take the video file you specified, in this case "Miku_Rabbit.mp4"
and create a gif "preview.gif"
from the timestamps you specified.
The -ss 25 -to 30
arguments instruct FFmpeg to trim the video beginning at the 25-second mark and ending at the 30-second mark. If your input video is very long, this will help you cut only the part of the video you want to convert to GIF. I recommend making GIFs no longer than 5 seconds to maintain a manageable file size. I chose 25 to 30 seconds for this video since it best shows the action.
The fps=10
sets the GIF frame rate to 10 fps (frames per second), more than enough for a preview.
Finally, scale=360:-1
resizes the video to 360 pixels wide, making the GIF file much smaller.
You can edit these values, to further reduce the file size, or to create a higher quality gif at a larger size. Whatever your preference.
Once you’ve run that command, you should have a new file "preview.gif"
which is a 5 second loop from the video, between the 25 and 30 second mark.
But this gif is still 5MB, we can make it smaller using gifsicle.
After creating “preview.gif” simply run this command:
gifsicle --optimize=10 --lossy=100 -o preview-opt.gif preview.gif
This will create a new file "preview-opt.gif"
from an existing gif "preview.gif"
The --optimize=10
argument specifies the level of optimization, which can be any integer. A higher number will produce better results, but will take more time to process.
The --lossy=100
argument specifies how many artifacts should be allowed; higher values lead to smaller files, but will produce more artifacts.
The quality is ever so slightly worse, but the file size is less than half.