#!/bin/bash
#
# Made by Juan Velasco questions welcomed at velasco@skynet.be
#
# good heuristic: each minute corresponds to 7 megas, since
# a 100 min movie takes 700 megas and has good quality
if [ $# != 5 ] ; then
echo "Use: dvdRip3pass "
echo " ex1: divRip3pass vts_01_1.vob godfather.avi 700 90 1.78"
echo " ex2: divRip3pass \"-dvd 1\" taxidriver.avi 650 90 1.78"
echo " ex3: divRip3pass \"-dvd 1 -chapter 1-22 \" goodBadUglyPart1.avi 700 78 1.78"
echo " ex4: divRip3pass \"-dvd 1 -aid 131\" madMax.avi 700 89 1.78"
echo " ex5: divRip3pass \"-dvd 1 -aid 128\" snatch.avi 700 98 1.78"
exit 1
fi
# ARGUMENTOS -----------
INPUT=$1
OUTPUT=$2
MAXSIZE=$(($3*1000))
MINUTOS=$4
ASPECT=$5
#-----------------------
SEGUNDOS=$(($MINUTOS*60 +60))
# ESTIMACION PARA 2 PASES -----------------------------------------
#Audio rate used to estimate is 128 kbits/s which is 16 kbytes/s
#but in reality it uses variable audio bit rate see vbr=3
#(the estimator gives a higher number)!!
#Amount of size for the audio (no video)
AUDIOSIZE=$((16*$SEGUNDOS))
#Amount of free space in bytes for the movie (with no sound)
FREE=$(($MAXSIZE - $AUDIOSIZE))
#Calculates the kbit rate per secong multiplying the kbytes by 8
#(1 kbyte = 8 kbits) and dividing by the number of seconds to encode
RATE=$((($FREE*8) / $SEGUNDOS))
# -------------------------------------------------------------------
# OUTPUT ESTIMACION --------------------------------------------------
echo ""
echo "Information to encode : $INPUT"
echo "Encoded output file : $OUTPUT"
echo "Max space to use : $MAXSIZE kbytes"
echo "Estimated size of 128 kbits/s audio : $AUDIOSIZE kbytes"
echo "Estimated average bit rate (2-pass) ------> $RATE kbits"
FINALSIZE=$(( ($RATE * $SEGUNDOS)/8 + $AUDIOSIZE))
echo "Estimated final size (2-pass) ------> $FINALSIZE kbytes"
# --------------------------------------------------------------------
# PERO HAREMOS 3-PASES!! ----------------------------------------------
echo "FIRST PASS"
#echo "set -lameopts vbr=3:vol=8 to increase volume if the default is low"
mencoder $INPUT -oac mp3lame -lameopts vbr=3 -ovc frameno -o frameno.avi
# ESTIMACIONES MAS EXACTAS
AUDIOSIZE=`ls -l frameno.avi | awk '{ print $5 }'` # en bytes
AUDIOSIZ=$(($AUDIOSIZE/1000)) # en Kbytes
#Amount of free space in bytes for the movie (with no sound)
FREE=$(($MAXSIZE - $AUDIOSIZ)) # todo en Kbytes
#Calculates the bit rate per secong multiplying the kbytes by 8
#(1 kbyte = 8 kbits) and dividing by the number of seconds to encode
RATE=$((($FREE*8) / $SEGUNDOS))
echo "Size of frameno.avi audio file : $AUDIOSIZ Kbytes"
echo "Bit rate (3-pass) ------> $RATE kbits"
FINALSIZE=$(( ($RATE * $SEGUNDOS)/8 + $AUDIOSIZ))
echo "Estimated final size (3-pass) ------> $FINALSIZE kbytes"
echo "SECOND PASS"
mencoder $INPUT -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vqmin=2:vqmax=31:vbitrate=$RATE:vhq:vpass=1:aspect=$ASPECT -o /dev/null
echo "THIRD PASS"
mencoder $INPUT -oac copy -ovc lavc -lavcopts vcodec=mpeg4:vqmin=2:vqmax=31:vbitrate=$RATE:vhq:vpass=2:aspect=$ASPECT -o $OUTPUT
rm -f frameno.avi
rm -f divx2pass.log