Sunday 8 March 2009

Convert iso to vob script

Since i own a PS3 i like to stream all my movies to it. Sadly the video support in it as of date is pretty poor, but it can play .vob. Sadly most movies "borrowed" from the internet is encapsulated into a .iso or .img files, that is iso9660 for us mortals... ;)

Anyway, upon mounting an iso you will find, if it's a dvd rip of course, two folders VIDEO_TS and AUDIO_TS. The VIDEO_TS contains the whole movie segemented into a s*itload of .vob files.
Each of these files contains x-minutes of the movie. So what you can do if you don't like changing movie file every other second. Merge the files together as one large vob. This can easily be done with a cat *.vob new_file.vob command.

But as i'm lazy by nature i wrote a script doing all this for me. I found a nifty cat progress bar implementation called Bar to see the merge status as cat has no progress report of it's own.
So here goes, my extract-vob script. Ps. Sorry about the indentation, i blame the blogs wysiwyg.


#!/bin/bash
NO_ARGS=""

#path and name of file
SRC_NAME="$1"

#DEST path
DEST="$2"

#Name of file with extension
FILE_NAME="${SRC_NAME##*/}"

#Base name of file without extension
BASE_NAME="${FILE_NAME%%.*}"

#EXTENSION of file
EXT="${FILE_NAME#*.}"

#Temporary mount to /mnt
TMP_DIR="/mnt"

#####################################
#To utilize the mount command user #
#must be/have root-previlegies #

#####################################
function check_if_root()
{
ROOT_UID=0 # Only users with $UID 0 have root privileges.
E_NOTROOT=67 # Non-root exit error.

if [ "$UID" -ne "$ROOT_UID" ];then
echo "Must be root to run this script."
exit $E_NOTROOT
fi
}

#########################
#Check for common errors#
#########################
function errors ()
{
if [[ "$SRC_NAME" == "$NO_ARGS" || "$DEST" == "$NO_ARGS" ]];then
echo "Too few arguments"
exit 0
elif [[ ! -e $SRC_NAME || -d $SRC_NAME ]];then
echo "Source does not exist"
echo "Source can not be a directory"
exit 0
elif [[ ! -d $DEST || ! -e $DEST ]];then
echo "DEST is not a directory"
echo "DEST does not exist"
exit 0
fi
}

##########################################
#Check for simple errors and correct them#
##########################################
function dest ()
{
if [[ $DEST == "." ]];then
DEST="$PWD"
elif [[ $DEST == "./" ]];then
DEST="$PWD"
elif [[ $DEST == "/" ]];then
DEST="$PWD"
fi

if [[ ! "$DEST" =~ /$ ]];then
DEST=$DEST"/"
fi
}

################
#Main function #
################
function main()
{
echo "Source : $SRC_NAME"
echo "DEST : $DEST$BASE_NAME.vob"

echo "Mount file in tmp dir"
mount -o loop -t iso9660 $SRC_NAME $TMP_DIR

echo "Extracting file to $DEST$BASE_NAME.vob"
bar -o "$DEST/$BASE_NAME.vob" -c cat $TMP_DIR/video_ts/vts_*.vob
chmod 777 "$DEST/$BASE_NAME.vob"

echo "Umount $TMP_DIR"
umount $TMP_DIR

echo "Remove $TMP_DIR"
rmdir $TMP_DIR

echo "Done! You can find extracted file at:"
echo "$DEST$BASE_NAME.vob"
}

#########################
# Function Calls #
#########################

check_if_root
errors $#
dest $DEST
main $#