Programster's Blog

Tutorials focusing on Linux, programming, and open-source

FFmpeg - Create GIF Animated Images

Below is my really simple script for creating GIFs quickly and easily from longer video files. If you want to put in a lot of effort to get them nicer looking there is this post at



# Specify the relative or rull path to the video you wish to convert to an animated webp.

# Specify the name for the generated animated image. This may also be a full filepath like
# /path/to/myfile.gif

# Specify the name for a temporary file to put within /tmp. This must have the same file
# extension as the original video.

# Specify the start time of the video that the clip should be taken e.g. 1 minute and 10 seconds in

# Specify the length of time for the clip. E.g. 10 seconds

# Set width of the output gif. The height will be derived from the w:h ratio.

# Set the number of times to loop.
# 0 = infinite
# 1+ = that many times.
# -1 = no loop

# Extract the short video we wish to turn into a gif
ffmpeg \
  -ss $START_TIME \
  -vcodec copy \
  -acodec copy \

# Set framerate of animated image.

ffmpeg \
  -i /tmp/$TEMP_FILENAME \
  -loop $LOOP \
  -vf "fps=$FRAMERATE, scale=$WIDTH:-1:flags=lanczos,split[s0][s1];[s0]palettegen[p];[s1][p]paletteuse" \

The flags part in the last ffmpeg command make a big difference to improving the quality of the gif through the creation and use of a custom palette to the clip. In the extraction ffmpeg command, if one puts the -ss before the -i parameter, it is much quicker, but the time will not be as accurate as Ffmpeg skips to the closest I frame in the video which may be seconds away from where you wish to start/end.


Last updated: 23rd July 2022
First published: 28th February 2021