#!/bin/sh
#
def_ratio=2
def_quant=4
def_deint=2

help(){
    cat <<END
Usage: $0 [options] <filename.[mov|qt|dv|mpg|avi|flv|mkv]>
Options:
    -d n      deinterlating: 
              n=0 none, n=1 adaptive, n=2 fast      ($def_deint)
    -r n      aspect ratio, n=1 4:3, n=2 16:9       ($def_ratio)
    -q n      fixed quantizer                       ($def_quant)
    -h        Print this help message
END
    exit 0
}

ratio=$def_ratio
quant=$def_quant
deint=$def_deint

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

if test "$#" != "1"
then
	help
fi
case $1 in
*.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$//"`-ultra ;;
*.mkv)
    input="$1"
    base=`echo $input | sed "s/\.mkv$//"` ;;
*.dv)
    input="$1"
    base=`echo $input | sed "s/\.dv$//"` ;;
*.mpg)
    input="$1"
    base=`echo $input | sed "s/\.mpg$//"` ;;
*)
    help ;;
esac

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

case $ratio in
2)
	w=720
	h=400 ;;
*)
	w=600
	h=450 ;;
esac

quant_f="fixed_quant=${quant}"

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

# can add this -delay 0.25 fix sync

echo mencoder $input -aspect ${w}:${h} \
	-ovc xvid -oac mp3lame \
	-xvidencopts profile=dxnhtntsc:nointerlacing:${quant_f} \
	-lameopts vbr=0:br=192 -idx \
	-vf harddup,${deint_f}scale=${w}:${h} \
	-af resample=44100 -o $base.avi

sleep 5

mencoder $input -aspect ${w}:${h} \
	-ovc xvid -oac mp3lame \
	-xvidencopts profile=dxnhtntsc:nointerlacing:${quant_f} \
	-lameopts vbr=0:br=192 -idx \
	-vf harddup,${deint_f}scale=${w}:${h} \
	-af resample=44100 -o $base.avi
