#! /bin/sh

#################################################
#                                               #
# Building a dvd out of a (set of) videofiles   #
#                                               #
#################################################

# Van http://movabletripe.com/archive/merging-avis-into-a-single-dvd-on-linux/ :

# Tools you will need for the task:
#  mencoder
#  ffmpeg
#  dvdauthor
#  mkisofs


Mpeg()
{
# Make it into an mpeg2 for dvd
echo ""
echo "Make it into an mpeg2 for dvd"
echo ""
#ffmpeg -i ${File_Orig_1} -y -target pal-dvd  -threads 2 -sameq  ${File_Orig_1}_final.mpg
ffmpeg -i ${File_Orig_1} -y -target pal-dvd  -threads 2 -mbd rd -trellis 2 -cmp 2 -subcmp 2 -pass 1/2 ${File_Orig_1}_final.mpg
}

Dir()
{
# remove old cruf else it will get included
rm -rf ${File_Orig_1}_dvd
# make it into a directory with proper VIDEO.TS and IFO files for your DVD player to read
echo ""
echo "Making a proper dvd directory"
echo ""
dvdauthor --title -o ${File_Orig_1}_dvd -f ${File_Orig_1}_final.mpg
dvdauthor -o ${File_Orig_1}_dvd -T
}

Iso()
{
# and one final step to make that DVD structure into a burnable image
echo ""
echo "One final step to make that DVD structure into a burnable iso image"
echo ""
mkisofs -dvd-video -o ${File_Orig_1}.iso ${File_Orig_1}_dvd/

# any way to test/verify the iso here?
echo "DVD-iso is finished. Use 'mplayer ${File_Orig_1}.iso' to test it"
echo ""
echo "For burning, use 'wodim -speed=4 -dev=/dev/sr0 ${File_Orig_1}.iso'"
echo "Or use 'growisofs -Z /dev/sr0=${File_Orig_1}.iso'"
}


########
# Main #
########

# Let's check for file 1
if [ ! -z "${1}" ]
	then
		File_Orig_1="${1}"
		echo "File we'll work on is: ${File_Orig_1}"
	else
		echo "Add a file argument to work on"
		echo "Make sure there are no spaces or other strange characters in the file name"
		exit
fi

if [ ! -z "${2}" ]
	then
		echo "Maximum number of files we work on is 1"
		echo "Make sure there are no spaces in your filenames"
fi

# Give the option and time to quit
echo ""
if [ ! -z ${File_Orig_1} ]
	then
		echo "Going to work on ${File_Orig_1}"
fi
echo "Make sure there are no spaces in your filenames"
echo ""
echo "Sleeping now for 10 seconds, so if you don't want this to happen,"
echo "then press Ctrl-C"
echo ""
sleep 10

# Call the functions
Mpeg
Dir
Iso


echo ""
echo "Exiting now gracefully"
exit


