Movie Converter - bash/Zenity Version

This is the original bash script that used Zenity for the GUI.


#!/bin/bash
###############################################################
# Purpose: Provide a simple GUI interface to mencoder for 
# transcoding video files.
###############################################################

###############################################################
# Set a few initial variables:
#	SHOME - Where the script resides
#	ICON - The icon that is displayed in the upper left of
#	       the Zenity window
#	STARTIN - Where the File-Open dialog will open up
#	MENCODER - Location of the mencoder executable
#	ZENITY - Common Zenity command line
###############################################################
SHOME="/home/paul/bin"
ICON="multimedia-player.png"
STARTIN=/temp
MENCODER="/opt/mplayer/bin/mencoder"
ZENITY="zenity --window-icon=$SHOME/$ICON"

# Check to see if mencoder is install where it's supposed to be
# Throw up an error if not.
if [ -e $MENCODER ]
then
	echo "Nothing to do"
	# This is a cludge because 1) bash has no unless statement like Perl
	# and 2) bash doesn't read the whole file before execution so it doesn't know about the 
	# fileSel function below yet.
else
	$ZENITY --error --text="Could not find mencoder at the defined location.  Install mencoder or change the MENCODER variable in mencode.zen to point to the proper location."
	exit
fi

###############################################################
# The function that fires off the file selection dialog.
# Clicking OK jumps to the fileName function.  Clicking Cancel 
# exits the script.
###############################################################
fileSel(){
	cd $STARTIN
	FILE=`$ZENITY --file-selection --title "MOMC - My Own Movie Converter"`
	if [ "$?" == "0" ]
	then
		sizeSel
	else
		exit
	fi
}

###############################################################
# The function that asks what size you want the file to be.
###############################################################
sizeSel() {
        CHOOSE=`$ZENITY --list --window-icon=$SHOME/$ICON --radiolist --column "" --column "Type" --column "Size" FALSE Normal Default FALSE Palm-TX 480x320 FALSE Palm-Tungsten-E 150x114`

        if [ "$?" == "0" ]
        then
		if [ $CHOOSE == "Normal" ]
		then
			echo "Nothing to do"
		elif [ $CHOOSE == "Palm-TX" ]
		then
			SIZE="480:320"
		elif [ $CHOOSE == "Palm-Tungsten-E" ]
		then
			SIZE="150:114"
		else
			$ZENITY --error --text="Whoops! Not sure how I got here."
		fi

	        # Split the file in two at the .  Assume there is only one .
	        # in the file name.  PATHFILE is the path an file name; EXT
	        # is the extension (mov, mpg, mpeg, etc)
	        PATHFILE=`echo $FILE | awk -F. '{print $1}'`
	        EXT=`echo $FILE | awk -F. '{print $2}'`

	        # If the file has a file extension for a video, ask where the user
	        # wants to save the file and what name to give it.  Fill in the
	        # field with the current PATHFILE and add a .avi extension to it.
	        # If the file does not have a file extension for a video, tell
	        # the user that the file is not in the correct format.
	        if [ $EXT = "mov" ] || [ $EXT = "mp4" ] || [ $EXT = "mpg" ] || [ $EXT = "mpeg" ] || [ $EXT = "vob" ] || [ $EXT = "avi" ] || [ $EXT = "wmv" ] || [ $EXT = "asf" ]
	        then
			fileName
		else
			$ZENITY --error --text="That file is not a video file format!"
	                fileSel
		fi
       else
		fileSel
       fi
}

###############################################################
# The function that asks what you want to name the file.
# Clicking OK jumps to the fileConv function. Clicking Cancel 
# returns back to the fileSel function.
###############################################################
fileName() {
	SAVEAS=`$ZENITY --list --window-icon=$SHOME/$ICON --radiolist --column "" --column "Type" --column "" FALSE avi "MS Audio Video Interface" FALSE asf "MS Advanced Systems Format" FALSE "mpg" "Moving Pictures Experts Group" FALSE mov "Apple Movie" FALSE mp4 "MPEG-4" FALSE wmv "Windows Media Video"`
	if [ "$?" == "1" ]
	then
		fileSel
	fi

	# Dialog to let user specify location and name of file; extension automatically added.
	FILENAME=`$ZENITY --entry --title="Name File" --text="Name of output file" --entry-text="$PATHFILE.$SAVEAS"`
	if [ "$?" == "0" ]
	then
		fileConv
	else
		fileSel
	fi
}

###############################################################
# The function that does the conversion.
###############################################################
fileConv() {
	if [ $FILENAME == "$FILE" ]
	then
		$ZENITY --error --text="You are attempting to overwrite the source file.  You can't do that."
		fileName
	fi

	if [ -e $FILENAME ]
	then
		OVWR=`$ZENITY --warning --title="Overwrite file?" --text="A file by that name already exists!  Overwrite it?"`
		if [ "$?" == "0" ]
		then
			echo "Nothing to do";
			# Cludge again.
		else
			fileName
		fi
	else
		echo "Nothing to do";
		# Cludge again.
	fi

	# Give the user one more chance to back out an return to the 
	# file selection dialog.
	GO=`$ZENITY --question --title="Proceed?" --text="Proceed with transcoding?"`

	# If the user selected OK, do the transcoding and pipe it to 
	# Zenity's progress indicator.  When the transcoding is done, 
	# pop-up a dialog to let the user know.  Clicking on OK there 
	# returns them to the file selection dialog.
	if [ "$?" == "0" ]
	then
		if [ $EXT == "vob" ]
		then
			$MENCODER $FILE -oac lavc -ovc lavc -lavcopts vcodec=mpeg4 -vf spp,scale -o $FILENAME|zenity --progress --text="Processing..." --pulsate --auto-close --width=100 --title="Progress" 2>/dev/null
		else
			if [ $CHOOSE == "Normal" ]
			then
				$MENCODER $FILE -oac lavc -ovc lavc -lavcopts vcodec=mjpeg $FILE -o $FILENAME|zenity --progress --text="Processing..." --pulsate --auto-close --width=100 --title="Progress" 2>/dev/null
			else
				$MENCODER $FILE -oac lavc -ovc lavc -lavcopts vcodec=mpeg4 -vf scale=$SIZE -o $FILENAME|zenity --progress --text="Processing..." --pulsate --auto-close --width=100 --title="Progress" 2>/dev/null
			fi
		fi

		$ZENITY --info --title="Transcode Complete" --text="The file completed transcoding."
		fileSel	
	else
		fileSel
	fi
}

fileSel


Back to Movie Converter Page