#!/usr/bin/env bash
# $Id$

if [ $# -ne 3 ]
then
    echo "$(basename ${0}) <vobfile> <crop value> <bitrate>"
    exit 1
fi

RIPPATH=${PWD}
VOBFILE=${1}
CROP=${2}
#  bitrate.py -o 0.5 -t <size> <length> | awk '/Target/ {printf "%0.f\n", $(NF-1)}'
BITRATE=${3}
PASS_1="psnr:turbo=2:pass=1:bframes=3:threads="
PASS_2="psnr:subq=6:partitions=all:8x8dct:me=umh:frameref=5:bframes=3:b_pyramid:weight_b:pass=2:threads="
THREADS="auto"
MENCODER_ARGS="-vf pullup,softskip,crop=${CROP},hqdn3d=3:2:3:3 -sws 9 -oac copy -ovc x264"

for PASS in "${PASS_1}" "${PASS_2}"
do
    if [ "${PASS}" == "${PASS_1}" ]
    then
        PASS_ARGS="-o ${RIPPATH}/${VOBFILE/vob/1st.avi}"
    else
        PASS_ARGS="-o ${RIPPATH}/${VOBFILE/vob/avi}"
    fi
    echo mencoder ${RIPPATH}/${VOBFILE} \
        ${MENCODER_ARGS} \
        -x264encopts bitrate=${BITRATE}:${PASS}${THREADS} \
        ${PASS_ARGS}
done

# vim:syntax=sh
# vim:sw=4:softtabstop=4:expandtab
