#!/bin/sh
#
#  bitrate is 2500 by default for SVCD      (lower is worse)
#  quality factor is 8 by default for SVCD  (higher is worse)
#  The peak bit-rate and average bit-rate should differ by 20-25%
#  Suggested -b2300 -q10 / -b2400 -q9 / -b2500 -q8
#
#  .mov files from mobo are top interlaced first
#  .dv files are bottom interlaced first
#
def_interlace=1
def_ratio=1
def_sound=1
def_signal=1
def_tools=0

help(){
	cat <<END
Usage: $0 [options] <filename.[avi|dv|mov|mpg|qt]>
Options:
    -i n      n=0 progressive, n=1 top, n=2 bottom  ($def_interlace)
    -q n      quality, n=1 highest ... n=4 lowest   ($def_quality)
    -r n      aspect ratio, n=1 4:3, n=2 letterbox  ($def_ratio)
    -s n      n=0 mp2enc, n=1 toolame               ($def_sound)
    -t n      video, n=0 mjpegtools, n=1 ffmpeg     ($def_tools)
    -v n      video Signal, n=1 ntsc n=2 pal        ($def_signal)
    -h        Print this help message
END
	exit 0
}

interlace=$def_interlace
ratio=$def_ratio
sound=$def_sound
signal=$def_signal
mjpegtools=$def_mjpegtools

while getopts i:q:r:s:t:v:h name "$@"
do
    case $name in
i)
    interlace=$OPTARG ;;
q)
    quality=$OPTARG ;;
r)
	ratio=$OPTARG ;;
s)
	sound=$OPTARG ;;
t)
    tools=$OPTARG ;;
v)
    signal=$OPTARG ;;
*)
    help ;;
    esac
done
let shiftind=$OPTIND-1
shift $shiftind
if test "$#" != "1"
then
	help
fi
case $1 in
*.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$//"` ;;
*.dv)
    input="$1"
    base=`echo $input | sed "s/\.dv$//"` ;;
*.mpg)
    input="$1"
    base=`echo $input | sed "s/\.mpg$//"`-svcd ;;
*)
	help ;;
esac

case $quality in
1)
    abitrate=224
    bitrate=2500
    quant_m=4
    quant_f=2 ;;
3)
    abitrate=192
    bitrate=2200
    quant_m=6
    quant_f=3 ;;
4)
    abitrate=96
    bitrate=2000
    quant_m=7
    quant_f=4 ;;
*)
    abitrate=192
    bitrate=2400
    quant_m=5
    quant_f=3 ;;
esac
case $signal in
2)
    nflag_f="-r pal"
    nflag_m="-np" ;;
*)
    nflag_f="-r ntsc"
    nflag_m="-nn" ;;
esac
case $interlace in
0)
    iflag_f="-interlace 0"
    iflag_m=NOT_INTERLACED ;;
1)
    iflag_f="-interlace 1 -ildct -ilme -top 1"
    iflag_m=INTERLACED_TOP_FIRST ;;
*)
    iflag_f="-interlace 1 -ildct -ilme -top 0"
    iflag_m=INTERLACED_BOTTOM_FIRST ;;
esac
case $ratio in
2)
	case $interlace in
0)
		iflag_f="-interlace 0" ;;
*)
		iflag_f="-deinterlace" ;;
esac
    aflag_f="-s480x360 -padtop 60 -padbottom 60 -aspect 4:3"
    aflag_m="-a3" ;;
*)
    aflag_f="-aspect 4:3"
    aflag_m="-a2"  ;;
esac

echo "Encoding $input to $base.mpg..."
echo "    video bitrate $bitrate"
echo "    audio bitrate $abitrate"
sleep 5

case $tools in
1)
    ffmpeg -i $input -target svcd $aflag_f $iflag_f $nflag_f \
		-scan_offset \
        -qmin $quant_f -b $bitrate -ab $abitrate -y $base.mpg ;;
*)
#    lav2wav $input > $base.wav
	ffmpeg -i $input -f wav -y $base.wav;
case $sound in
1)
		sox -V $base.wav -r44100 $base-44.wav
		toolame -b$abitrate -s44.1 $base-44.wav $base.m2a ;;
*)
		mp2enc < $base.wav -r44100 -b$abitrate -o $base.m2a ;;
esac

#    lav2yuv $input |
	ffmpeg -i $input -f yuv4mpegpipe -pix_fmt yuv420p -y /dev/stdout |
	yuvcorrect -T $iflag_m |
	yuvscaler $nflag_m -O SVCD |
	yuvdenoise -g3,4,4 -t8,12,12 -M4,6,6 |
	mpeg2enc -M0 $nflag_m $aflag_m -f5 -G18 -b$bitrate \
		-V230 -q$quant_m -o $base.m2v 
	mplex -f4 $base.m2v $base.m2a -o $base.mpg ;;
esac
