Skip to content

Instantly share code, notes, and snippets.

@trisweb
Last active September 11, 2019 23:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save trisweb/9c9d043081e4f68d8acaacd17075650a to your computer and use it in GitHub Desktop.
Save trisweb/9c9d043081e4f68d8acaacd17075650a to your computer and use it in GitHub Desktop.
Gifgen: ffmpeg gif generator script
#!/bin/bash
###############################################################################
# gifgen: a simple script to generate gifs
###############################################################################
# Usage:
# > gifgen [input.mov] [output.gif]
#
# [output.gif] is optional; if not given, filename will be auto-generated.
#
# Requirements: ffmpeg (brew install ffmpeg / apt-get install ffmpeg)
#
# It's not parameterized: stick this file in a bin folder on your PATH and edit
# it each time to adjust, i.e. nano `which gifgen`
###############################################################################
# Size = Max dimension (width or height)
SIZE=640
FPS=18
##### See http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
# DITHER=sierra2
# DITHER=floyd_steinberg
DITHER=sierra2_4a
# DITHER=floyd_steinberg
# DITHER=heckbert
# DITHER=bayer:bayer_scale=1 # 2, 3
# DITHER=none
##### Default filters (leave this line alone; adjust size and FPS above)
filters="fps=$FPS,scale=$SIZE:-1"
##### Uncomment to append, edit to your liking...
#
# For minor color pop:
# filters="$filters,eq=contrast=1.05:brightness=0.05:saturation=1.25"
#
# For heavier color/contrast/brightness boost:
# filters="$filters,eq=contrast=1.25:brightness=0.2:saturation=1.25"
#
# To add minor sharpening:
# filters="$filters,unsharp=luma_msize_x=3:luma_msize_y=3:luma_amount=1.3"
#
# To speed up or slow down:
# This is inverted; lower values = faster. Small speedup is nice for screen caps.
SPEED=0.85
filters="$filters,setpts=$SPEED*PTS"
#
#####
###############################################################################
# Don't edit below this line #
###############################################################################
palette="/tmp/palette.png"
if [[ -z "$2" ]]; then
OUTPUT="$(echo $1 | sed 's/ /_/g;s/\.[a-zA-Z0-9]\{2,5\}$/\.gif/')"
else
OUTPUT="$2"
fi
ffmpeg -i "$1" -vf "$filters,palettegen=stats_mode=diff" -y $palette
ffmpeg -i "$1" -i $palette -lavfi \
"$filters [x]; [x][1:v] paletteuse=dither=$DITHER:diff_mode=rectangle" \
-y $OUTPUT
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment