#!/bin/sh
#
def_ratio=1
def_deint=2
def_quality=3
def_db=8

help(){
    cat <<END
Usage: $0 [options] <filename.[mov|qt|dv|mpg|avi|flv|vob]>
Options:
    -d n      deinterlating: 
              n=0 none, n=1 adaptive, n=2 fast      ($def_deint)
    -r n      n=1 4:3, n=2 letterbox, n=3 panscan   ($def_ratio)
              n=4 9:5 to 4:3 panscan
    -v n      boost volume by n db                  ($def_db)
    -q n      quality, n=1 highest ... n=3 lowest   ($def_quality)
    -h        Print this help message
END
    exit 0
}

ratio=$def_ratio
deint=$def_deint
quality=$def_quality
db=$def_db

while getopts d:q:r:v:h name "$@"
do
    case $name in
d)
    deint=$OPTARG ;;
r)
    ratio=$OPTARG ;;
q)
	quality=$OPTARG ;;
v)
	db=$OPTARG ;;
*)
    help ;;
    esac
done
let shiftind=$OPTIND-1
shift $shiftind

if test "$#" != "1"
then
	help
fi
case $1 in
*.vob)
    input="$1"
    base=`echo $input | sed "s/\.vob$//"` ;;
*.flv)
    input="$1"
    base=`echo $input | sed "s/\.flv$//"` ;;
*.qt)
    input="$1"
    base=`echo $input | sed "s/\.qt$//"` ;;
*.mov)
    input="$1"
    base=`echo $input | sed "s/\.mov$//"` ;;
*.avi)
    input="$1"
    base=`echo $input | sed "s/\.avi$//"`-zodiac ;;
*.dv)
    input="$1"
    base=`echo $input | sed "s/\.dv$//"` ;;
*.mpg)
    input="$1"
    base=`echo $input | sed "s/\.mpg$//"` ;;
*)
    help ;;
esac

case $quality in
1)
	abitrate=128
	vbitrate=450 ;;
3)
	abitrate=64
    vbitrate=250 ;;
*)
	abitrate=96
	vbitrate=350 ;;
esac

echo Encoding $input to $base.avi....

case $ratio in
2)
    ratio_f="scale=320:180,expand=320:240"
	;;
3)
    ratio_f="scale=426:240,crop=320:240"
    ;;
4)
	ratio_f="scale=576:324,crop=320:240"
	;;
*)
    ratio_f="scale=320:240"
	;;
esac

case $deint in
0)
    deint_f="" ;;
1)
	deint_f="mcdeint=0:1:1," ;;
*)
	deint_f="kerndeint," ;;
esac

#	-xvidencopts profile=dxnportntsc:bitrate=$vbitrate:max_bframes=0 \

echo mencoder $input -quiet \
	-ovc xvid -oac twolame -ofps 15 \
	-xvidencopts profile=sp3:bitrate=$vbitrate \
	-twolameopts br=$abitrate \
	-vf harddup,${deint_f}$ratio_f,dsize=320:240 \
	-af resample=44100,volume=$db,volnorm=2 -o $base.avi

sleep 5

mencoder $input -quiet \
	-ovc xvid -oac twolame -ofps 15 \
	-xvidencopts profile=sp3:bitrate=$vbitrate \
	-twolameopts br=$abitrate \
	-vf harddup,${deint_f}$ratio_f,dsize=320:240 \
	-af resample=44100,volume=$db,volnorm=2 -o $base.avi
