#!/bin/dash
# Author: jamesbond - License: MIT License
# Originally authored for Puppy Newsletter April 2017 edition
# Made using info from http://blog.pkh.me/p/21-high-quality-gif-with-ffmpeg.html
# i18n by step

### Configuration
IN=${1}
OUT=${2:-out.gif}
SCALE=${3:-300}
QUALITY=${4:-low}

### i18n
export TEXTDOMAIN=fatdog
export OUTPUT_CHARSET=UTF-8
. gettext.sh
SN=${0##*/}

if [ -z "$IN" ]; then
	eval_gettext "\
Usage: \$SN input [out] [horz-res] [quality]
- out is the output filename, default is '\$OUT'
- horz-res is the horizontal resolution, default is '\$SCALE'
- quality is image quality of conversion, you can choose between
  'low', 'lower', 'high', or 'high2'. Higher quality = bigger file.
  Default is '\$QUALITY'.
"
	exit 1
fi

palette="/tmp/palette.png"
filters="fps=10,scale=$SCALE:-1:flags=lanczos"
filters_lower="fps=10,scale=$SCALE:-1"

case $QUALITY in
	low)
		ffmpeg -i "$IN" -vf $filters -y "$OUT"
		;;
		
	lower)
		ffmpeg -i "$IN" -vf $filters_lower -y "$OUT"
		;;	

	high)
		### 
		ffmpeg -i "$IN" -vf "$filters,palettegen" -y $palette
		ffmpeg -i "$IN" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$OUT"
		;;

	high2)
		### 
		ffmpeg -i "$IN" -vf "$filters,palettegen=stats_mode=diff" -y $palette
		ffmpeg -i "$IN" -i $palette -lavfi "$filters [x]; [x][1:v] paletteuse" -y "$OUT"
		;;		
esac
