#!/bin/sh
#
def_quality=3
def_ratio=2

help(){
    cat <<END
Usage: $0 [options] <filename.[avi|dv|mov|mpg|qt|ts|mkv]>
Options:
    -q n      quality, n=1 highest ... n=4 lowest   ($def_quality)
    -r n      aspect ratio, n=1 4:3, n=2 16:9       ($def_ratio)
    -h        Print this help message
END
    exit 0
}

quality=$def_quality
ratio=$def_ratio

while getopts d:i:q:r:s:t:v:h name "$@"
do
    case $name in
q)
    quality=$OPTARG ;;
r)
    ratio=$OPTARG ;;
*)
    help ;;
    esac
done

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

case $quality in
1)
    resa=720x480
    sara=32:27
    resb=720x480
    sarb=8:9
    abitrate=96k
    asample=48000
    bitrate=1024 ;;
2)
    resa=720x400
    sara=1:1
    resb=600x450
    sarb=1:1
    abitrate=64k
    asample=44100
    bitrate=768 ;;
4)
    resa=512x288
    sara=1:1
    resb=480x360
    sarb=1:1
    abitrate=32k
    asample=11025
    bitrate=480 ;;
*)
    resa=640x360
	sara=1:1
	resb=512x384
    sarb=1:1
    abitrate=48k
	asample=22050
    bitrate=512 ;;
esac
case $ratio in
1)
	res=$resb
	sar=$sarb ;;
*)
	res=$resa
	sar=$sara ;;
esac

echo Encoding audio to $base.acc...
echo ffmpeg -i $input -ab $abitrate -ar $asample -acodec libfaac -y $base.aac
ffmpeg -i $input -ab $abitrate -ar $asample -acodec libfaac -y $base.aac

echo Encoding video to ${base}_$res.264...
echo x264 --fps 30000/1001 --sar $sar --pass 1 --bitrate $bitrate \
    --stats p.stats --level 4.1 --keyint 14 --min-keyint 2 \
    --ref 3 --mixed-refs --bframes 2 --b-adapt 2 --weightb --direct auto \
    --deblock -1:-1 --subme 7 \
    --trellis 2 --partitions p8x8,b8x8,i4x4,i8x8 --8x8dct \
    --ipratio 1.1 --pbratio 1.1 \
    --vbv-bufsize 14475 --vbv-maxrate 24000 --qcomp 0.5 \
    --me umh --threads auto --progress --no-psnr \
    --no-ssim --output ${base}_$res.264 /dev/stdin $res \
    --mvrange 511 --aud --nal-hrd
ffmpeg -i $input -deinterlace -s $res \
	-f yuv4mpegpipe -pix_fmt yuv420p -r 30000/1001 -y /dev/stdout |
	y4mtoyuv |
x264 --fps 30000/1001 --sar $sar --pass 1 --bitrate $bitrate \
    --stats p.stats --level 4.1 --keyint 14 --min-keyint 2 \
    --ref 3 --mixed-refs --bframes 2 --b-adapt 2 --weightb --direct auto \
    --deblock -1:-1 --subme 7 \
    --trellis 2 --partitions p8x8,b8x8,i4x4,i8x8 --8x8dct \
    --ipratio 1.1 --pbratio 1.1 \
    --vbv-bufsize 14475 --vbv-maxrate 24000 --qcomp 0.5 \
    --me umh --threads auto --progress --no-psnr \
    --no-ssim --output ${base}_$res.264 /dev/stdin $res \
    --mvrange 511 --aud --nal-hrd

echo Multiplexing to ${base}_$res.mp4...
echo MP4Box -add ${base}_$res.264 -add $base.aac \
	-fps 29.97002997002997002997 -isma ${base}_$res.mp4
rm -f ${base}_$res.mp4
MP4Box -add ${base}_$res.264 -add $base.aac \
	-fps 29.97002997002997002997 -isma ${base}_$res.mp4
