[Guide] How to create a short preview gif using ffmpeg and gifsicle

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

preview-opt


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/
image

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:
image

image


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…”
image

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.

preview


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.

preview-opt

The quality is ever so slightly worse, but the file size is less than half.

8 Likes

Thanks for sharing! I should incorporate this into my workflow :thinking:

1 Like

Thanks for this tutorial. I had the time to take a look, works like a charm (on Windows 11)
I have personally made a bat to do the conversion and GIF optimlzation for a set of point on the video

Below the code from my “makegif.bat”, using as first parameter the video path, followed by 4 integers of different points of video (5 second from them)
example :

makegif.bat

set /A time_end = %2 + 5
set /A time_end2 = %3 + 5
set /A time_end3 = %4 + 5
set /A time_end4 = %5 + 5
ffmpeg -ss %2 -to %time_end% -i %1 -filter_complex “fps=10,scale=360:-1[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse” preview1.gif
gifsicle --optimize=10 --lossy=100 -o preview-opt1.gif preview1.gif
del preview1.gif
ffmpeg -ss %3 -to %time_end2% -i %1 -filter_complex “fps=10,scale=360:-1[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse” preview2.gif
gifsicle --optimize=10 --lossy=100 -o preview-opt2.gif preview2.gif
del preview2.gif
ffmpeg -ss %4 -to %time_end3% -i %1 -filter_complex “fps=10,scale=360:-1[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse” preview3.gif
gifsicle --optimize=10 --lossy=100 -o preview-opt3.gif preview3.gif
del preview3.gif
ffmpeg -ss %5 -to %time_end4% -i %1 -filter_complex “fps=10,scale=360:-1[s]; [s]split[a][b]; [a]palettegen[palette]; [b][palette]paletteuse” preview4.gif
gifsicle --optimize=10 --lossy=100 -o preview-opt4.gif preview4.gif
del preview4.gif

(Quick bat, don’t know if possible to make loop, but it works good like this :slight_smile: )

Hey um, Do you have the sauce for that animation?

Maybe better, with the scri^pt :wink:

https://discuss.eroscripts.com/t/hatsune-miku-rabbit-hole/155766

1 Like