#!/bin/bash
# /usr/bin/local/create-dvd RM20070406
# Modified by RM 30/12/07
#################################################
# For processing mpeg-ps files that have been saved from 
# DVB-T programs or ripped and concatenanted from
# DVD's.
#
# Copy into /usr/local/bin or wherever else appropriate
# and set sthe script executable if it isn't.
#
# Run this script in the directory in which you intend to
# process the dvd. The source file can be anywhere in your
# file sysytem. Be sure you have enough room in your work
# directory. To be safe you'd want double the space to just 
# convert to a dvd.iso and burn it, and triple the space to 
# shrink the video before creating a dvd.iso and burning it.
#
# This script does not decide if you need to shrink a video
# to fit on a 4.7 gb dvd. Nor does it check to see if you 
# have enough room to in the directory you are working in.
# You'll have to sort that out yourself.
#
# This script is working currently working on slack-12.0.
# 
#################################################

dvd_device=/dev/sr0

# Make a fast and dirty dvd.xml with 2 hours of 5 minute chapter breaks.
# If your movie is longer than 2 hours your last chapter is as long as the difference.
# If your movie is less than 2 hours long there's no harm done by declaring more chapters than required.
create_dvdxml()
    {
    echo "<dvdauthor>" > dvd.xml  # Using a single first ">" will overwrite existing dvd.xml in working directory
    echo "    <vmgm />" >> dvd.xml
    echo "    <titleset>" >> dvd.xml
    echo "        <titles>" >> dvd.xml
    echo "            <pgc>" >> dvd.xml
    echo -e "                <vob file=\"movie.vob\" chapters=\"0,5:00,10:00,15:00,20:00,25:00,30:00,35:00,40:00,45:00,50:00,55:00,60:00,65:00,70:00,75:00,80:00,85:00,90:00,95:00,100:00,105:00,110:00,115:00,120:00\" />" >> dvd.xml
    echo "            </pgc>" >> dvd.xml
    echo "        </titles>" >> dvd.xml
    echo "     </titleset>" >> dvd.xml
    echo " </dvdauthor>" >> dvd.xml
    }

# This will process the video into a (IMHO) good format for PAL dvd's.
make_vob_first()
    {   
    mencoder -oac lavc -ovc lavc -of mpeg -mpegopts format=dvd \
    -vf scale=720:576,harddup -srate 48000 -lavcopts \
    vcodec=mpeg2video:vrc_buf_size=1835:vrc_maxrate=9800:vbitrate=5000:keyint=15:acodec=ac3:abitrate=192:aspect=16/9 \
    -ofps 25 $INPUT_FILE -o  movie.mpg
    }
    
do_dvd()
    {
    echo "ln -s movie.mpg movie.vob"
    ln -s movie.mpg movie.vob
    echo "dvdauthor -o dvd -x dvd.xml"
    dvdauthor -o dvd -x dvd.xml
    echo "mkisofs -dvd-video -o dvd.iso dvd"
    mkisofs -dvd-video -o dvd.iso dvd
    growisofs -Z $dvd_device=dvd.iso
    eject
    }
    
shrink_vob()
    {
    # Extract sound
    nice tcextract -i movie.mpg -a 0 -x ac3 -t vob > movie.ac3
    # Extract video
    nice tcextract -i movie.mpg -t vob -x mpeg2 > movie.m2v  
    # Shrink video to half original size
    nice tcrequant -i movie.m2v  -o shrunk.m2v -f $REQUANT_FACTOR
    # Rename movie.mpg to movie.mpg-orig
    mv movie.mpg movie-orig.mpg
    # Merge the shrunken video with the ac3 audio
    nice mplex -f 8 -o movie.mpg shrunk.m2v movie.ac3
    # Now clear away the debris and reclaim some realestate
     rm movie.ac3
     rm movie.m2v
     rm shrunk.m2v
     
     do_dvd
     }
     
# End of functions, Begin Main
INPUT_FILE=$1
dvd_device=/dev/cdrom

test -n "$INPUT_FILE"
  if [ $? -eq 1 ]; then
    echo -e "\nUsage: create-dvd [input file]"
    #echo -e "For more information use create-dvd -help\n" # Not implemented yet
    exit
  fi
clear
echo -e "\n\nThis script will create a dvd ISO or a dvd ISO and burn it to a disk.\n"
sleep 2
echo -e "\nThis script assumes that you will be burning with /dev/cdrom.\nChange the variable \"dvd_device\" if it is some other device.\n"
sleep 5
echo -e "\nIf you do not have a writeable DVD disk in the appropriate drive, \nthis script will still create the dvd.iso."
sleep 10
echo -e "\n\nEnter the number of your selection:\n"
echo -e "1. Create a dvd from $INPUT_FILE without processing" 
echo -e "   or shrinking the video."
echo -e "2. Process $INPUT_FILE into a PAL 16:9 video file with"
echo -e "   ac3 audio using mencoder and then create the dvd."   
echo -e "3. Process as in selection 2 above and then shrink the"
echo -e "   video before creating the dvd.\n"

read response_1

PROC_VOB=$response_1

case "$PROC_VOB" in
    '1')
    tcprobe -d 0 -i $INPUT_FILE | grep -q "aspect ratio: 16:9" 
    if [ $? -eq 1 ];then
        echo -e "\n$INPUT_FILE does not appear have a dvd PAL format."
        echo -e "Check it's format or use option 2."
        sleep 15s
        exit
    fi
    
    ln -s $INPUT_FILE movie.mpg
    create_dvdxml
    do_dvd
    ;;
    '2')
    make_vob_first
    create_dvdxml
    do_dvd   
    ;;
    '3')
    echo -e "\n\nWhat is the requant (shrink) factor?"
    echo -e "For example \"1.5\" will give you a video 75% original size,"
    echo -e "and \"2\" will give you a video at 50% the original size.\n"
    read response_2
    REQUANT_FACTOR=$response_2
    make_vob_first
    shrink_vob
    create_dvdxml
    do_dvd   
    ;;
    
esac

# End of script


