#!/bin/bash

# setting standards
AUDIO_SCALE=3.0
AUDIO_STREAM=0
AUDIO_BITRATE=160
BITRATE=1400
CROP=16
INPUT_CODEC=vob
OUTPUT_CODEC=divx4
DEINTERLACE=0
RESIZE32=5,0 # 5 for 1.78:1, 8 for 2.35:1

# Audio-Spur soll per Parameter gewhlt werden knnen
if test "$3"; then AUDIO_STREAM="$3"; fi
# Resize soll per Parameter gewhlt werden knnen
if test "$4" = "1" ; then RESIZE32=4; CROP=64,16; fi # 2.35:1
if test "$4" = "2" ; then RESIZE32=1; CROP=0,0; fi   # 1.33:1
# Bitrate soll per Parameter gewhlt werden knnen
if test "$5"; then BITRATE="$5"; fi

if (test "$1") && (test "$2"); 
  then 
    echo ================================================================================
    echo "=> Encoding $1 to $2, using audio channel 0x08$AUDIO_STREAM."
    echo "=> bitrate $BITRATE + crop : $CROP + resize : $RESIZE32"
    echo ================================================================================
    
    transcode -i "$1" -x $INPUT_CODEC -o "$2" -y $OUTPUT_CODEC -s $AUDIO_SCALE \
      -w $BITRATE -j $CROP -B $RESIZE32 -I $DEINTERLACE -a $AUDIO_STREAM -b $AUDIO_BITRATE -q 1;
  else
    echo "usage: vob2avi [input] [output] [audiostream]"
    echo "               [0 - 1.78:1 | 1 - 2.35:1=16:9 | 2 - 1.33:1=4:3] [bitrate]"
    echo "If a file named 'shutdown' exists, then the computer will shut down"
    echo "after it has finished encoding; this requires the script to be run as root."
fi
if test -e "shutdown"; then 
  echo -en "\x07" # make some noise
  echo "Shutting down the computer in 1 minute."
  sleep 60
  halt; 
fi
