Skip to content

Instantly share code, notes, and snippets.

@JobLeonard
Created November 28, 2022 11:37
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JobLeonard/5f99b712ba77b7aa82696c1368fd265f to your computer and use it in GitHub Desktop.
Save JobLeonard/5f99b712ba77b7aa82696c1368fd265f to your computer and use it in GitHub Desktop.
#!/bin/bash
bold=$(tput bold)
normal=$(tput sgr0)
# No need to create a temp directory if no valid input is given
if [ -z "$1" ]; then
echo "${bold}Input filename required!${normal}"
exit 1
elif [ -z "$2" ]; then
echo "${bold}Output filename required!${normal}"
exit 1
fi
# Temp directory script courtesy of Ortwin Angermeier (thanks!)
# copied verbatim from StackOverflow answer
# https://stackoverflow.com/a/34676160/3211791
#================================================
echo ""
echo "${bold}Creating temp working directory to store palette${normal}"
echo ""
# the directory the script is called from
CWD=$(pwd)
# the directory of the script
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
# the temp directory used, within $DIR
# omit the -p parameter to create a temporal directory in the default location
WORK_DIR=`mktemp -d -p "$DIR"`
# check if tmp dir was created
if [[ ! "$WORK_DIR" || ! -d "$WORK_DIR" ]]; then
echo ""
echo "${bold}Could not create temp dir${normal}"
echo ""
exit 1
fi
echo ""
echo "${bold}Temp working directory created in $WORK_DIR${normal}"
echo ""
# deletes the temp directory
function cleanup {
rm -rf "$WORK_DIR"
echo ""
echo "${bold}Deleted temp working directory $WORK_DIR${normal}"
}
# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
#================================================
# implementation of script starts here
palette="$WORK_DIR/palette.png"
filters="fps=15"
echo ""
echo "${bold}Determining GIF palette${normal}"
echo ""
ffmpeg -v verbose -i "$1" -vf "$filters,palettegen=stats_mode=diff" -y $palette
echo ""
echo "${bold}Creating $2 ${normal}"
echo ""
ffmpeg -v verbose -i "$1" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse=dither=bayer:bayer_scale=3:diff_mode=rectangle" -y "$2"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment