#!/bin/sh
#
#  Multipass Xvid encoding
#
#  -i2 for dv
#
#  high bitrate -a224 -b2000 if it underruns
#  medium bitrate -a224 -b1800 if it underruns
#  low bitrate -a192 -b1600 if it underruns
#  very low bitrate -a96 -b1200 if it underruns
#
def_abitrate=224
def_bitrate=1800
def_docrop=false
def_interlace=2
def_ratio=1
def_passnum=0

help(){
    cat <<END
Usage: $0 [options] <filename.[mov|qt|dv]>
Options:
    -a n      Set the audio bitrate                 ($def_abitrate)
    -b n      Set the bitrate                       ($def_bitrate)
    -c        Crop the bottom by 3.333333%          ($def_docrop)
    -i n      n=0 progressive, n=1 top, n=2 bottom  ($def_interlace)
    -r n      aspect ratio, n=1 4:3, n=2 16:9       ($def_ratio)
    -R n      n=0 single pass, n=1,2 multipass      ($def_passnum)
    -h        Print this help message
END
    exit 0
}

abitrate=$def_abitrate
bitrate=$def_bitrate
docrop=$def_docrop
interlace=$def_interlace
ratio=$def_ratio
passnum=$def_passnum

while getopts a:b:chi:r:R: name "$@"
do
    case $name in
a)  
    abitrate=$OPTARG ;;
b)  
    bitrate=$OPTARG ;;
c)
    docrop=true ;;
i)
    interlace=$OPTARG ;;
r)
    ratio=$OPTARG ;;
R)
    passnum=$OPTARG ;;
*)
    help ;;
    esac
done
let shiftind=$OPTIND-1
shift $shiftind

if test "$#" != "1"
then
	help
	exit
fi
case $1 in
*.qt)
	type=mov
    input="$1"
    base=`echo $input | sed "s/\.qt$//"` ;;
*.mov)
	type=mov
    input="$1"
    base=`echo $input | sed "s/\.mov$//"` ;;
*.dv)
	type=dv
    input="$1"
    base=`echo $input | sed "s/\.dv$//"` ;;
*)
    help ;;
esac

case $ratio in
2)
    aflag="--export_asr 3 --export_par 4,3"
	WxH=640x480 ;;
*)
    aflag="--export_asr 2 --export_par 1"
	WxH=640x480 ;;
esac
case $docrop in
true)
    width=`echo $WxH | sed "s/x.*$//"`
    height=`echo $WxH | sed "s/^.*x//"`
    let height=$height-$height*10/300
    crop=${width}x${height} ;;
*)
    crop=$WxH ;;
esac
echo Encoding $input to $base.avi....

if test -f $base.cfg
then 
	config_source=$base.cfg
else
	config_source="/usr/local/lib/transcode/xvid4.cfg"
fi
case $interlace in
0)
    fields=p
    dnmode=0
    sed "s/^interlaced.*$/interlaced = 0/;
        s/^topfieldfirst.*$/topfieldfirst = 0/" \
        < $config_source \
        > xvid4.cfg ;;
1)
    fields=t
    dnmode=1
    sed "s/^interlaced.*$/interlaced = 1/;
        s/^topfieldfirst.*$/topfieldfirst = 1/" \
        < $config_source \
        > xvid4.cfg ;;
*)
    fields=b
    dnmode=1
    sed "s/^interlaced.*$/interlaced = 1/;
        s/^topfieldfirst.*$/topfieldfirst = 0/" \
        < $config_source \
        > xvid4.cfg ;;
esac

echo transcode -i $input -x $type -Z $WxH \
	$aflag --encode_fields $fields \
	-Jyuvdenoise=mode=$dnmode:sharpen=0:border=0x0-$crop \
	-R $passnum -y xvid4 -b $abitrate -w $bitrate -o $base.avi

sleep 5
transcode -i $input -x $type -Z $WxH \
	$aflag --encode_fields $fields \
	-Jyuvdenoise=mode=$dnmode:sharpen=0:border=0x0-$crop \
	-R $passnum -y xvid4 -b $abitrate -w $bitrate -o $base.avi

mv xvid4.cfg $base.cfg
