#!/bin/bash
# This script, called "myipoder", was written by Hector J. Levesque.
# Last change August 2009.
# It generates a command line that calls "mencoder" to produce an ipod movie.
# Test default settings with something like:  "myipoder -ch 4-4 -e 2MB | sh"
# For full usage, do "myipoder -h"

# path to suitable mencoder program.  Must have x264 and faac compiled in.
encoder="mencoder"
# path to suitable output file to use as default
output_file="~/Desktop/output.mp4"

# default options
dvd="dvd://1"
out="-o $output_file"
fmt="-of lavf -lavfopts format=mp4"
lang="-alang en"
sub=""
chap=""
end=""
contr="eq2=1.0:1.3:0.3:1.4"
scale="scale=576:320,dsize=576:320,unsharp=l3x3:0.5"
prog="-ofps 24000/1001"
qual=1     # default: quality=1
force=""   # default: confirm big jobs

# Bitrate settings for audio and video
vidlo="768"
vidmid="900"
vidhi="1381"
audlo="128"
audmid="192"
audhi="256"
# Misc other alternate settings
tvscale="scale=432:320,dsize=432:320,unsharp=l3x3:0.5"

while [ $# -gt 0 ]
do
  case "$1" in
    -d) dvd="dvd://$2"; shift;;
    -o) out="-o $2"; shift;; 
    -ch) chap="-chapter $2"; shift;;
    -e) end="-endpos $2"; shift;;
    -q) qual=$2; shift;;
    -c) contr="";;
    -t) scale="$tvscale";;
    -l) lang="-alang $2"; shift;;
    -s) sub="-slang en";;
    -p) prog="";;
    -f) force="y";;
     *) cat <<END >&2
Usage: myipoder [-f] [-d n] [-o file] [-ch n-m] [-e nMB] [-q n] [-p] [-c] [-t]
Output a script that encodes a DVD with the following optional settings:
     -f       do not ask for confirmation on big jobs (default: do)
     -d n     work on track n of DVD (default: 1)
     -l xx    audio language is xx (default: en)
     -s       include English subtitles (default: none)
     -o file  output MP4 file (default: $output_file)
     -ch n-m  only do chapters n to m of DVD track (default: all the track)
     -e nMB   stop when the MP4 file is nMB in size (default: do not stop)
     -q n     where n=0,1,2,3 is the quality of the output (default: 1)
     -p       output rate for non-progressive video (default: progressive)
     -c       use given contrast for video (default: reduced contrast)
     -t       change the format to standard TV (default: widescreen)
Since this outputs a command line, the typical use is: myipoder <options> | sh
END
        exit 0;;
  esac
  shift
done

vidopts="level=30:cabac=0:ref=1:analyse=all:me=umh:subme=6:no-fast-pskip=1:trellis=1:global_header"
audopts="mpeg=4:object=2:raw -channels 2 -srate 48000"

case "$qual" in
  0) video="-ovc x264 -x264encopts bitrate=$vidlo:$vidopts"  # 768;128
     audio="-oac faac -faacopts br=$audlo:$audopts";;

  1) video="-ovc x264 -x264encopts bitrate=$vidmid:$vidopts" # 900;192
     audio="-oac faac -faacopts br=$audmid:$audopts";;       # movies

  2) video="-ovc x264 -x264encopts bitrate=$vidmid:$vidopts" # 900;256
     audio="-oac faac -faacopts br=$audhi:$audopts";;        # music

  *) video="-ovc x264 -x264encopts bitrate=$vidhi:$vidopts"  # 1381;256
     audio="-oac faac -faacopts br=$audhi:$audopts";;
esac

if [ -z "$contr" ]
  then 
    vf="-vf $scale,harddup"
  else
    vf="-vf $scale,$contr,harddup"
fi
 
if [ -z "$force$end$chap" ]
  then 
    echo -n "Are sure you want to encode the entire dvd? [yes] " >&2
    read confirm
    [[ $confirm == n* ]] || [[ $confirm == N* ]] && exit 0
fi

echo "$encoder $dvd $lang $sub $out $fmt $prog $audio $video $vf $chap $end"
