Skip to content

Instantly share code, notes, and snippets.

@rickosborne
Created November 26, 2022 18:23
Show Gist options
  • Save rickosborne/85743480e6407ef62a6d440d40d759ee to your computer and use it in GitHub Desktop.
Save rickosborne/85743480e6407ef62a6d440d40d759ee to your computer and use it in GitHub Desktop.
Convert a video to a GIF
#!/bin/sh
# Because I can never rememeber the magic incantations.
# Note that FFMPEG won't do as good a job as GIF Brewery or other apps, but it's good enough in a pinch.
IN_FILE="$1"
OUT_FILE="${IN_FILE%.*}.gif"
PALETTE_FILE="${IN_FILE%.*}-palette.png"
if [[ "" == "${SEEK_SECS}" ]] ; then
SEEK_SECS="0"
fi
if [[ "" == "$DURATION" ]] ; then
DURATION=$(ffprobe -i "${IN_FILE}" -v quiet -show_entries format=duration -hide_banner -of default=noprint_wrappers=1:nokey=1)
fi
if [[ "" == "${FPS}" ]] ; then
FPS="15"
fi
if [[ "" == "${WIDTH}" ]] ; then
WIDTH=$(ffprobe -i "${IN_FILE}" -v error -of default=noprint_wrappers=1:nokey=1 -select_streams v:0 -show_entries stream=width)
fi
if [[ -f "${PALETTE_FILE}" ]] ; then
rm "${PALETTE_FILE}"
fi
ffmpeg -hide_banner -ss "${SEEK_SECS}" -t "${DURATION}" -i "${IN_FILE}" -vf "fps=${FPS},scale=${WIDTH}:-1:flags=lanczos,palettegen" "${PALETTE_FILE}"
ffmpeg -hide_banner -ss "${SEEK_SECS}" -t "${DURATION}" -i "${IN_FILE}" -i "${PALETTE_FILE}" -filter_complex "fps=${FPS},scale=${WIDTH}:-1:flags=lanczos[x];[x][1:v]paletteuse" "${OUT_FILE}"
rm "${PALETTE_FILE}"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment