Created
December 24, 2016 03:12
-
-
Save cwoodall/8db0acd49f059c58424f3d736b99b999 to your computer and use it in GitHub Desktop.
A little script for generating compressed gifs using ffmpeg from a video file (webm, or mp4)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
ORIG_DIR=`pwd` | |
FRAME_DIR="/tmp/gif-frames" | |
LOSSY=80 | |
FPS=5 | |
LOOP=0 | |
SCALE=.5 | |
COLORS=128 | |
# A POSIX variable | |
OPTIND=1 # Reset in case getopts has been used previously in the shell. | |
# Initialize our own variables: | |
output_file="out.gif" | |
usage="$(basename "$0") [-h] [-o out.gif] [-c C] [-s S] [-l L] [-d D] [-f F] input_file.gif | |
A little script for generating compressed gifs using ffmpeg, imagemagick and | |
gisicle (with giflossy). | |
where: | |
-h show this help text | |
-s Scale the image by this percentage (default: $SCALE) | |
-o Output filename (default: $output_file) | |
-c Colors (default: $COLORS) | |
-l Loop (default: $LOOP) | |
-d Temporary output directory (default: $FRAME_DIR) | |
-f Frame rate in FPS (default: $FPS)" | |
while getopts h\?o:s:c:l:d:f: opt; do | |
case "$opt" in | |
h|\?) | |
printf "$usage" | |
exit 0 | |
;; | |
o) output_file=$OPTARG | |
;; | |
s) SCALE=$OPTARG | |
;; | |
l) LOOP=$OPTARG | |
;; | |
d) FRAME_DIR=$OPTARG | |
;; | |
c) COLORS=$OPTARG | |
;; | |
f) FPS=$OPTARG | |
;; | |
esac | |
done | |
shift $((OPTIND-1)) | |
[ "$1" = "--" ] && shift | |
echo $@ | |
mkdir $FRAME_DIR | |
ffmpeg -i "$@" -r $FPS "$FRAME_DIR/frame-%03d.jpg" | |
cd $FRAME_DIR | |
convert -delay $(( 100 / FPS )) -loop $LOOP *.jpg tmp.gif | |
gifsicle -O3 --lossy=$LOSSY --colors=$COLORS --scale=$SCALE -o $ORIG_DIR/$output_file tmp.gif | |
rm -r $FRAME_DIR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment