jcottobboni
9/26/2019 - 12:57 AM

A little script for generating compressed gifs using ffmpeg from a video file (webm, or mp4)

A little script for generating compressed gifs using ffmpeg from a video file (webm, or mp4)

#!/bin/bash

ORIG_DIR=`pwd`
FRAME_DIR="/tmp/gif-frames"
LOSSY=80
FPS=5
LOOP=0
SCALE=.5
COLORS=128

# A POSIX variable
OPTIND=1         # Reset in case getopts has been used previously in the shell.

# Initialize our own variables:
output_file="out.gif"

usage="$(basename "$0") [-h] [-o out.gif] [-c C] [-s S] [-l L] [-d D] [-f F] input_file.gif

A little script for generating compressed gifs using ffmpeg, imagemagick and
gisicle (with giflossy).

where:
    -h  show this help text
    -s  Scale the image by this percentage (default: $SCALE)
    -o  Output filename (default: $output_file)
    -c  Colors (default: $COLORS)
    -l  Loop (default: $LOOP)
    -d  Temporary output directory (default: $FRAME_DIR)
    -f  Frame rate in FPS (default: $FPS)"

while getopts h\?o:s:c:l:d:f: opt; do
    case "$opt" in
    h|\?)
        printf "$usage"
        exit 0
        ;;
    o)  output_file=$OPTARG
        ;;
    s)  SCALE=$OPTARG
        ;;
    l)  LOOP=$OPTARG
        ;;
    d)  FRAME_DIR=$OPTARG
        ;;
    c)  COLORS=$OPTARG
        ;;
    f)  FPS=$OPTARG
        ;;
    esac
done

shift $((OPTIND-1))

[ "$1" = "--" ] && shift

echo $@
mkdir $FRAME_DIR

ffmpeg -i "$@"  -r $FPS "$FRAME_DIR/frame-%03d.jpg"

cd $FRAME_DIR
convert -delay $(( 100 / FPS )) -loop $LOOP *.jpg tmp.gif

gifsicle -O3 --lossy=$LOSSY --colors=$COLORS --scale=$SCALE  -o $ORIG_DIR/$output_file tmp.gif

rm -r $FRAME_DIR