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

help(){
    cat <<END
Usage: $0 [options] outfile.any
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

outfile=$1
base=`echo $outfile | sed "s/\.[^.]*$//"`

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

echo "Encoding yuv4mpeg on /dev/stdin to $base.avi"
sleep 5

mencoder /dev/stdin -audiofile $base.wav \
	-ovc xvid -aspect ${w}:${h} \
    -xvidencopts profile=dxnhtntsc:nointerlacing:${quant_f} \
    -oac mp3lame -lameopts preset=medium \
    -vf ${deint_f}scale=${w}:${h} \
    -af resample=44100 -o $base.avi
