#!/bin/sh
#
def_interlace=1

help(){
    cat <<END
Usage: $0 [options] <filename.[mov|qt|dv|mpg|avi|flv]>
Options:
    -i n      n=0 prog, n=1 top, n=2 bot, n=3 deint ($def_interlace)
    -h        Print this help message
END
    exit 0
}

interlace=$def_interlace

while getopts i:h name "$@"
do
    case $name in
i)
	interlace=$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$//"`-zodiac ;;
*.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 $interlace in
0)
    xint_f="nointerlaced"
    deint_f="harddup"
    int_f="" ;;
1)
    xint_f="interlaced"
    deint_f="harddup,yadif=1:0,mcdeint=2:0:10"
	int_f="-field-dominance 0" ;;
*)
    xint_f="interlaced"
    deint_f="harddup,yadif=1:1,mcdeint=2:1:10"
    int_f="-field-dominance 1" ;;
esac

echo mencoder $input $int_f \
	-ovc x264 -oac lavc \
	-lavcopts acodec=ac3:abitrate=192 \
	-x264encopts bitrate=12000:$xint_f:threads=2 \
    -vf $deint_f,scale=1280:720 \
	-ofps 60000/1001 -fps 60000/1001 \
	-o $base.avi

echo ffmpeg -i $base.avi -acodec copy -vcodec copy -f vob -y $base.vob

sleep 5

mencoder $input $int_f \
	-ovc x264 -oac lavc \
	-lavcopts acodec=ac3:abitrate=192 \
	-x264encopts bitrate=12000:$xint_f:threads=2 \
    -vf $deint_f,scale=1280:720 \
	-ofps 60000/1001 -fps 60000/1001 \
	-o $base.avi

ffmpeg -i $base.avi -acodec copy -vcodec copy -f vob -y $base.vob
