From ac44b40f2fcbb4c21058298bc7a39994cd162326 Mon Sep 17 00:00:00 2001
From: katherine <shmibs@shmibbles.me>
Date: Fri, 24 Mar 2017 03:21:02 -0700
Subject: add multiple options

---
 Readme.md |   2 +-
 make-gif  | 104 +++++++++++++++++++++++++++++++++++++++-----------------------
 2 files changed, 67 insertions(+), 39 deletions(-)

diff --git a/Readme.md b/Readme.md
index 73f470a..61f1744 100644
--- a/Readme.md
+++ b/Readme.md
@@ -12,7 +12,7 @@ files as painless as possible, with support for things like embedding
 subtitles, various dithering algorithms, cropping, and post-optimisation (via
 [gifsicle](https://www.lcdf.org/gifsicle/))
 
-just clone and run `make-gif --help` to get started!
+just clone and run `make-gif -h` to get started!
 
 dependencies
 ============
diff --git a/make-gif b/make-gif
index d553aff..3d77d1a 100755
--- a/make-gif
+++ b/make-gif
@@ -12,14 +12,17 @@ usage() {
 	[[ "$1" != "" ]] && print_error $1
 	echo "Usage: $callstr [OPTIONS...] <infile> <outfile>"
 	echo ""
-	echo "           description  option     default val"
-	echo "            start time  -s <time>  00:00:00"
-	echo "     length in seconds  -t <num>   full length"
-	echo "               gif fps  -f <num>   10"
-	echo "       gif pixel width  -w <num>   480"
-	echo "    use subtitle track  -b <int>   none"
-	[[ $hasgsic ]] && echo "optimise with gifsicle  -g"
-	echo "       print this help  -h"
+	echo "             description  option     default val"
+	echo "              start time  -s <time>  00:00:00"
+	echo "       length in seconds  -t <num>   full length"
+	echo "                 gif fps  -f <num>   10"
+	echo "         gif pixel width  -w <num>   480"
+	echo "      use subtitle track  -b <int>   none"
+	echo "       number of colours  -c <int>   256"
+	echo "     dithering algorithm  -d <str>   sierra2_4a"
+	echo "redraw only changed rect  -r"
+	[[ $hasgsic ]] && echo "  optimise with gifsicle  -o"
+	echo "         print this help  -h"
 	exit 1
 }
 
@@ -48,18 +51,25 @@ local width=480
 local subs=""
 local strack=0
 local gsic=""
+local dithalg="sierra2_4a"
+local colour_count=256
+local rect=""
 
 local timepat='^(([0-9][0-9]:){1,2}[0-9][0-9]|[0-9]+)(\.[0-9]+){0,1}$'
 local numpat='^[1-9][0-9]*(\.[0-9]+){0,1}$'
 local intpat='^[1-9][0-9]*$'
 local zintpat='^[0-9]+$'
+local dithpat='^(bayer[0-5]|heckbert|floyd_steinberg|sierra2|sierra2_4a)$'
 
-# tmp var used to hold '-t' if length is used
+# used to hold '-t' if length is used
 local t=""
 
+# used to hold subtitle options, if present
+local substr=""
+
 # really annoying, but no other good way to do optional args 
 if [[ $hasgsic ]]; then
-	while getopts :s:t:f:w:bhn:g opt; do
+	while getopts :s:t:f:w:b:c:d:rho opt; do
 		case "$opt" in
 			s) 
 				[[ ! $(echo $OPTARG | grep -oE "$timepat") ]] \
@@ -88,13 +98,27 @@ if [[ $hasgsic ]]; then
 				strack=$OPTARG
 				subs=true
 				;;
-			g) gsic=true ;;
+			c)
+				[[ ! $(echo $OPTARG | grep -oE "$intpat") ]] \
+					|| [[ $OPTARG -gt 256 || $OPTARG -lt 2 ]] \
+					&& usage "colour count must be an integer in the range 2-256"
+				colour_count=$OPTARG
+				;;
+			d)
+				[[ ! $(echo $OPTARG | grep -oE "$dithpat") ]] \
+					&& usage "dithering algorithm must be one of bayer<0-5>, heckbert, floyd_steinberg, sierra2, sierra2_4a"
+				dithalg=$OPTARG
+				[[ $(echo $OPTARG | grep -o bayer) ]] \
+					&& dithalg="bayer:bayer_scale=$(echo $OPTARG | grep -oE '[0-5]')"
+				;;
+			o) gsic=true ;;
+			r) rectmode=":diff_mode=rectangle" ;;
 			h) usage ;;
 			[?]) usage "unrecognised option" ;;
 		esac
 	done
 else
-	while getopts :s:t:f:w:bhn: opt; do
+	while getopts :s:t:f:w:b:c:d:rh opt; do
 		case "$opt" in
 			s) 
 				[[ ! $(echo $OPTARG | grep -oE "$timepat") ]] \
@@ -123,6 +147,20 @@ else
 				strack=$OPTARG
 				subs=true
 				;;
+			c)
+				[[ ! $(echo $OPTARG | grep -oE "$intpat") ]] \
+					|| [[ $OPTARG -gt 256 || $OPTARG -lt 2 ]] \
+					&& usage "colour count must be an integer in the range 2-256"
+				colour_count=$OPTARG
+				;;
+			d)
+				[[ ! $(echo $OPTARG | grep -oE "$dithpat") ]] \
+					&& usage "dithering algorithm must be one of bayer<0-5>, heckbert, floyd_steinberg, sierra2, sierra2_4a"
+				dithalg=$OPTARG
+				[[ $(echo $OPTARG | grep -o bayer) ]] \
+					&& dithalg="bayer:bayer_scale=$(echo $OPTARG | grep -oE '[0-5]')"
+				;;
+			r) rectmode=":diff_mode=rectangle" ;;
 			h) usage ;;
 			[?]) usage "unrecognised option" ;;
 		esac
@@ -164,33 +202,23 @@ ffprobe -loglevel -8 -print_format json -show_streams "${tmp_pref}-in" \
 	| IFS=':' read -A as
 [[ "$as" != "" ]] && let "height = ${width} * ${as[2]} / ${as[1]}"
 
+[[ $subs ]] && substr="subtitles=${tmp_pref}-in:si=$strack,"
+
 # convert
-if [[ $subs ]]; then
-	echo "pass 1..."
-	ffmpeg -loglevel 16 -y -ss $start $t $length -i "${tmp_pref}-in" -copyts \
-		-vf "subtitles=${tmp_pref}-in:si=$strack,setsar=1/1,fps=$fps,scale=${width}:${height}:flags=lanczos,palettegen" \
-		${tmp_pref}-palette.png
-	[[ $? -ne 0 ]] && fferr=true
-	[[ ! $fferr ]] && echo "pass 2..." && ffmpeg -loglevel 24 \
-		-ss $start $t $length \
-		-i "${tmp_pref}-in" -i ${tmp_pref}-palette.png \
-		-copyts -filter_complex \
-		"subtitles=${tmp_pref}-in:si=$strack,setsar=1/1,fps=$fps,scale=${width}:${height}:flags=lanczos[x];[x][1:v]paletteuse" \
-		"$2" || abort
-	[[ $? -ne 0 ]] && fferr=true
-else
-	echo "pass 1..."
-	ffmpeg -loglevel 16 -y -ss "$start" $t $length -i "${tmp_pref}-in" \
-		-vf "setsar=1/1,fps=$fps,scale=${width}:${height}:flags=lanczos,palettegen" \
-		${tmp_pref}-palette.png
-	[[ $? -ne 0 ]] && fferr=true
-	[[ ! $fferr ]] && echo "pass 2..." && ffmpeg -loglevel 24 \
-		-ss $start $t $length \
-		-i "${tmp_pref}-in" -i ${tmp_pref}-palette.png -filter_complex \
-		"setsar=1/1,fps=$fps,scale=${width}:${height}:flags=lanczos[x];[x][1:v]paletteuse" \
-		"$2"
-	[[ $? -ne 0 ]] && fferr=true
-fi
+echo "pass 1..."
+ffmpeg -loglevel 16 -y -ss "$start" $t $length -i "${tmp_pref}-in" -copyts -filter_complex \
+	"${substr}setsar=1/1,fps=$fps,scale=${width}:${height}:flags=lanczos,palettegen=max_colors=${colour_count}" \
+	${tmp_pref}-palette.png
+
+[[ $? -ne 0 ]] && fferr=true
+
+[[ ! $fferr ]] && echo "pass 2..." && ffmpeg -loglevel 24 \
+	-ss $start $t $length \
+	-i "${tmp_pref}-in" -i ${tmp_pref}-palette.png -copyts -filter_complex \
+	"${substr}setsar=1/1,fps=$fps,scale=${width}:${height}:flags=lanczos[x];[x][1:v]paletteuse=dither=${dithalg}${rectmode}" \
+	"$2"
+
+[[ $? -ne 0 ]] && fferr=true
 
 [[ $fferr ]] && abort
 rm_tmps
-- 
cgit v1.2.3