Skip to content

Instantly share code, notes, and snippets.

@acdvorak
Created November 13, 2018 17:08
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 acdvorak/74d4c306d23437bcac2d7fa1217cd4da to your computer and use it in GitHub Desktop.
Save acdvorak/74d4c306d23437bcac2d7fa1217cd4da to your computer and use it in GitHub Desktop.
Convert video files to animated GIF with FFmpeg
# Usage: mov-to-gif path/to/input.mov [path/to/output.gif]
#
# This script generates high-quality GIFs at reasonable file sizes from practically *any* type of video file.
# By default, it reduces the framerate to 10 fps and scales the resolution to 800px wide (maintaining aspect ratio), but
# those parameters can be changed below.
#
# If no output path is specified (second argument), the script will use the input file's path with the extension changed
# to ".gif".
#
# EXISTING GIF FILES WILL BE OVERWRITTEN AUTOMATICALLY!
#
# FFmpeg can be installed via go/homebrew on Mac.
#
# Add this function to your ~/.bash_profile script and run `source ~/.bash_profile` in the terminal to use it.
function mov-to-gif() {
local INPUT_PATH="$1"
local OUTPUT_PATH="$2"
if [ -z "$OUTPUT_PATH" ]; then
local OUTPUT_PATH=$(echo "$INPUT_PATH" | sed -E 's/[.][[:alnum:]]+$/.gif/')
fi
local FPS='10'
local WIDTH='800'
# From http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
local PALETTE_PATH="/tmp/palette.png"
local FILTERS="fps=$FPS,scale='min($WIDTH,iw)':-1:flags=lanczos"
ffmpeg -y -v warning -i "$INPUT_PATH" -vf "$FILTERS,palettegen" "$PALETTE_PATH"
ffmpeg -y -v warning -i "$INPUT_PATH" -i "$PALETTE_PATH" -lavfi "$FILTERS [x]; [x][1:v] paletteuse" "$OUTPUT_PATH"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment