Skip to content

Instantly share code, notes, and snippets.

@Capster
Last active August 23, 2017 18:53
Show Gist options
  • Save Capster/32499fb5b24a11602f3037c0acfe6eb8 to your computer and use it in GitHub Desktop.
Save Capster/32499fb5b24a11602f3037c0acfe6eb8 to your computer and use it in GitHub Desktop.
#!/bin/bash
filename=${0##*/}
usage() {
echo "Usage: $filename [OPTION]..."
echo "Try '$filename --help' for more information."
}
help() {
echo "Usage: $filename [OPTION]..."
echo
echo " -h, --help Display this help"
echo " -i, --in Set file name for input video file (.mkv, .mp4, .webm, etc)"
echo " -o, --out Set file name for output .gif (Should be in .gif format)"
echo " -q, --quality GIF quality (360 by default)"
echo " -f, --fps GIF framerate"
echo " -s, --start Start time"
echo " -d, --duration Duration"
}
if [ $# == 0 ] ; then
usage
exit 1;
fi
while [ "$1" != "" ]; do
PARAM=$1
VALUE=$2
case $PARAM in
-h | --help)
help
exit
;;
-i | --input)
INPUT=$VALUE
shift
;;
-q | --quality)
QUALITY=$VALUE
shift
;;
-o | --output)
OUTPUT=$VALUE
shift
;;
-f | --fps)
FPS=$VALUE
shift
;;
-s | --start)
START=$VALUE
shift
;;
-d | --duration)
DURATION=$VALUE
shift
;;
*)
usage
exit 1
;;
esac
shift
done
if [ -z $OUTPUT ]; then
OUTPUT="${INPUT%.*}.gif"
fi
if [ -z $QUALITY ]; then
QUALITY="360"
fi
if [[ $FPS ]]; then
fps="fps=$FPS,"
fi
if [[ $START ]]; then
start="-ss $START"
fi
if [[ $DURATION ]]; then
duration="-t $DURATION"
fi
palette="${OUTPUT%.*}.png"
ffmpeg -y $start $duration -i $INPUT -vf $fps scale=$QUALITY:-1:flags=lanczos,palettegen $palette
ffmpeg $start $duration -i $INPUT -i $palette -filter_complex "scale=$QUALITY:-1:flags=lanczos[x];[x][1:v]paletteuse" $OUTPUT
rm "$palette" 2> /dev/null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment