#!/usr/local/bin/bash
# 
# mkxvcd version: 2.0.2
#
# Usage: mkxvcd movieyouwanttoconvert.whateverextensionmplayercanread
#
# Instalation: make this script executeable (chmod 755) and place it in your path.
#
# This script is for creating a kvcd mpeg1 video-cd to fit up to 2 hours of movie on a 80 min. cdr.
# If your movie is longer as that it will be split over more cdr's.
# The tools used for this are minimal mplayer-0.9.x and mjpegtools-1.6.0 and optionaly mpgtx.
# The actual video is a (almost) standard vcd pal one, only with vbr instead of cbr, and adapted the gop and video buffer size
# and using the kvcd intra and non-intra matrixes if available in mjpegtools version.
# Furthermore I adapted the audio bitrate to sqeeze the size a bit more.
# BTW, a 80 min. cdr can contain 807,5Mb. of video... So don't be fooled with the 700 Mb. printed on it!
# Info:
# http://www.kvcd.net
# http://www.mplayerhq.hu
# http://mjpeg.sourceforge.net
# http://mpgtx.sourceforge.net
# http://zebra.fh-weingarten.de/~transcode
#
# Script written by Thomas, use and adapt as you please ;-)
# Codec config by Japie, you may also use and adapt them too!
#
# thnx2: Karl,Hunter,Alex,Ted,Manuel,Mike and Nicolas.

# localisation settings will make the script fail
LANG=en_US
LC_ALL=en_US
export LANG LC_ALL
		
#################################################
# User settings					#
#################################################
MPLAYER=1 				# Set to 0 if you want transcode as default
bypass_fullscreen=1 			# ByPass fullscreen prompt
Log=1 					# 0 For Removal of Log, 1 to Keep Stats Log
log_cmds=0 				# 1 Log Command line to stats file
dvd_a_delay="--dvd_access_delay 3" 	# Make number larger if you have a slow drive
mode288=0 		# Make all encoding to the Pal Standard.
modedashZ=1 		# 1 for Lanczos3 resize (SLOW)
Fast=1 			# 0 for Lanczos3 Resize, 1 for New Fast Resize Transcode 0.6.11 or newer
mode528=0 		# 0 for 352x240/288, 1 for 528x480
modestandard=0 		# 1 for VCD, SVCD mode Options. # 0 for KVCD only Mode
mod_quant_vcdbitrate=0 	# Modify Quantisation and Max_bitrate before Conversion
audio_compat=0		# For the DvD players out there that seems to skip or start and stop Set this to 1
			# This Seems to help on my JVC DvD Player
			# What setting this to 1 does is uses Transcode for Audio when using Mplayer Mode.
			#
preview=0		# Set to 1 for PreView of Video via Mplayer
nice_level=10   	# Nice Level to run Programs at.
alt_scale=0     	# Use y4mscaler instead of yuvscaler Mplayer Mode only.
advanced=0		# Set to 1 to get Prompted for Wide Screen or 0 for Default by source aspect
cpus="-M 1"		# Sets Multi-Threading for mpeg2enc [0..32], 0=no multithreading, (default: 1)
#
#
#################################################

# System settings...
MkxVcd_Version="Mkxvcd-2.0.2"
started=`date +%s`
max_bitrate=1152
quantisation=4
video_buffer=40
gopmin_size=6
gopmax_size=18
audio_bitrate=112
RATIO=RESAMPLE
Format=n
aspect=2
dashy="-Y 0,0,0,0"
mpeg_type=2
altfps=0
dashx=
dashj=
dashs="-s 1.234"
export=
cflag=0
mpgtxflag=0
dvdcd=0
dashm="-M 0"
dashd=
dashD=
dashJ="-J "
dasht=
dashh=
dasha=
dvd_aspect=0
subs=
vop=
export_aspect="--export_asr 2"
aspect_flag=1
angles=0
dashE=
dashR=
dashV="-V"
dashZ=
pulld=
wflag=0
matrices=
output_size="795"
dashP= #"-P"  #Preserve two B frames between I/P frames when placing GOP boundaries
p_aspect=
wide_flag=0
filters=32detect=force_mode=3
movie_aspect="0"
dashaf= #"-af volume=-20"
norm=NTSC
present=vcd
mdashD=

# Colors
black='\E[30;47m'
red='\E[31;47m'
green='\E[32;47m'
yellow='\E[33;47m'
blue='\E[34;47m'
magenta='\E[35;47m'
cyan='\E[36;47m'
white='\E[37;47m'
reset="tput sgr0"

# Quantisation & Max_Bitrate Function
select_quant ()
{
# Ogm Files have no length...
	if [ $total -eq '0' ]; then
		total=85
		max_bitrate=1500
	fi
# Aspects 2 = WideScreen
	if [ $Aspects -eq '2' ]; then
# encoder settings for WideScreen...
	if [ $total -gt '60' ]; then
		quantisation=3
		max_bitrate=1500
	fi
	if [ $total -gt '75' ]; then
		quantisation=3
		max_bitrate=1500
	fi
	if [ $total -gt '90' ]; then
		quantisation=4
		max_bitrate=1500
	fi
	if [ $total -gt '105' ]; then
		quantisation=3
		max_bitrate=1152
	fi
	fi

	if [ $Aspects -eq '1' ]; then
# encoder settings for FullScreen...
	if [ $total -gt '60' ]; then
		quantisation=4
		max_bitrate=2400
	fi
	if [ $total -gt '75' ]; then
		quantisation=4
		max_bitrate=1500
	fi
	if [ $total -gt '90' ]; then
		quantisation=3
		max_bitrate=1152
	fi
	if [ $total -gt '105' ]; then
		quantisation=6
		max_bitrate=2400
	fi
   fi
}

videotype ()
{
		echo ""
	if [ $mjpeg_version -gt '16190' -o $mjpeg_version -eq '16190' ]; then
		matrices="-K kvcd"
	else
		matrices="-N -h"
	fi
	if [ $modestandard -eq '1' ]; then
		select_vid_type="   Select Output Type (vcd/svcd/xvcd/kvcd/tmpgenc/hi-res) Default is "$red"kvcd "
		echo -e -n "$select_vid_type"
		$reset
		read line

		case $line in
			'VCD') output_type="VCD"
				audio_bitrate=224
				mpeg_type=1 ;;
			'vcd') output_type="VCD"
				audio_bitrate=224
				mpeg_type=1 ;;
			'SVCD') output_type="SVCD"
				vop="-vf scale=480:460,expand=480:480"
				SIZE=SIZE_480x480
				present=svcd
				mpeg_type=5 ;;
			'svcd') output_type="SVCD"
				vop="-vf scale=480:460,expand=480:480"
				SIZE=SIZE_480x480
				present=svcd
				mpeg_type=5 ;;
			'KVCD') output_type="KVCD"
				mpeg_type=2 ;;
			'kvcd') output_type="KVCD"
				mpeg_type=2 ;;
			'xvcd') output_type="XVCD"
				mpeg_type=2
				matrices="-N -h" ;;
			'XVCD') output_type="XVCD"
				mpeg_type=2
				matrices="-N -h" ;;
			'TMPGENC') output_type="TMPGENC"
				mpeg_type=2
				matrices="-K tmpgenc" ;;
			'tmpgenc') output_type="TMPGENC"
				mpeg_type=2
				matrices="-K tmpgenc" ;;
			'hi-res') output_type="HI-RES"
				mpeg_type=2
				matrices="-K hi-res" ;;
               'HI-RES') output_type="HI-RES" 
                    mpeg_type=2
                    matrices="-K hi-res" ;; 
				*) output_type="KVCD"
				mpeg_type=2 ;;
		esac #
	fi
}

crop_detect ()
{
        if [ $dvdcd -eq '0' ]; then
                FILE="$FILE"
	else
		FILE=$FILE1
		ext2=".vob"
        fi
        if [ $mplayerv1 -eq '0' ]; then 
		cropd="-vop cropdetect"
	else
		cropd="-vf cropdetect"
	fi
	if [ $ext2 == ".ogm" -o $ext2 == ".Ogm" -o $ext2 == ".OGM" ]; then
mplayer -really-quiet -nosound -forceidx -vo null $cropd "$FILE"  -frames 10 -sstep 10 -nocache > mplayer.info
	else
mplayer -really-quiet $cropd "$FILE" -nosound -vo null -frames 30 -sstep 202 -nocache > mplayer.info
	fi
	if [ $ext2 == ".vob" -o $ext2 == ".VOB" ]; then
mplayer -really-quiet $cropd "$FILE" -nosound  -frames 30 -sstep 102 -nocache > mplayer.info
	fi
        while read line; do
        if [ "$line" != "${line#crop}" ]; then
                vopc=${line#crop}
                vopc=`echo $vopc | awk '{print $7}'`
                vopc=`echo $vopc | sed 's/[(]//g' | sed 's/[)]//g'`
        fi
        done < mplayer.info
        rm -f mplayer.info
	if [ $vopc ]; then
                scale1=`echo $vopc | awk -F : '{print $1":"$2}' | awk -F = '{print $2}'`
		scale2=`echo $scale1 | grep "^-"`
	if [ $scale2 ]; then
		echo 
		vopc=
	else
                width_orig1=`echo $scale1 | awk -F : '{print $1}'`
                height_orig1=`echo $scale1 | awk -F : '{print $2}'`
	if [ $width -gt $width_orig1 -o $height -gt $height_orig1 ]; then 
		width=$width_orig1
		height=$height_orig1
		width_orig=$width_orig1
		height_orig=$height_orig1
		vopc="$vopc,"
	else
		vopc=
	fi
	fi
	fi
}

calctype ()
{

                height1=$[height_orig /8]
                height1=$[height1 * 8]
                height1=$[height_orig - $height1 ]
                j1=$[height1 /2]
        if [ $j1 -gt '0' -a $framerate -ne '25' ]; then
                dashj="-j $j1,0,$j1,0"
        fi
	if [ $Aspects -eq '1' ]; then
		b1=$[height-240]
		dashy=$dashy
	fi
	if [ $Aspects -eq '2' -a $dvdcd -eq '1' ]; then
		b1=$[height-240]
		dashy="-Y 0,0,0,0"
		export_aspect="--export_asr 3"
		aspect_flag=2
	fi

	if [ $Aspects -eq '2' -a $dvdcd -eq '0' -a $vflag -gt '0' ]; then
		b1=$[height-160]
		dashy="-Y -40,0,-40,0"
		export_aspect="--export_asr 2"
		aspect_flag=1
	fi
	if [ $wide_flag -ne '1' ]; then
select_wide
	fi
		b1=$[b1/8]
		b2=$[width-352]
		b2=$[b2/8]
		dashB="-B $b1,$b2,8"

	if [ $mode288 -eq '1' -a $Aspects -eq '2' -a $dvdcd -eq '0' -a $framerate -ne '25' ]; then
		b1=$[height_orig-165]
		dashy="-Y -68,0,-68,0"
		SIZE=SIZE_352x288
	if [ $modedashZ -eq '1' ]; then
		dashy=
		dashB=
	if [ $mode528 -eq '1' ]; then
		dashZ="-Z 528x400"
		dashy="-Y -64,0,-64,0"
	else
		dashZ="-Z 352x200"
		dashy="-Y -20,0,-20,0"
	fi
	fi
	fi

	if [ $modedashZ -eq '1' -a $mode288 -ne '1' -a $Aspects -eq '2' -a $dvdcd -eq '0' -a $framerate -ne '25' ]; then
                dashy=
                dashB=
	if [ $mode528 -eq '1' -a $modedashZ -eq '1' ]; then
		dashZ="-Z 528x400"
		dashy="-Y -40,0,-40,0"
	fi
	if [ $mode528 -eq '0' -a $modedashZ -eq '1' ]; then
                dashZ="-Z 352x240"
		dashy="-Y 0,0,0,0"
	fi
	fi
	if [ $modedashZ -eq '1' -a $mode288 -ne '1' -a $Aspects -eq '1' -a $dvdcd -eq '0' -a $framerate -ne '25' ]; then
		dashy=
		dashB=
	if [ $mode528 -eq '1' ]; then
		dashZ="-Z 528x480"
		dashy=
	else
	if [ $altfps -eq '23' ]; then
		dashZ="-Z 352x240"
		dashy="-Y 0,0,0,0"
	else
		dashZ="-Z 352x240"
		dashy="-Y 0,0,0,0"
	fi
	fi
        fi

	if [ $height_orig -lt '240' -a $modedashZ -ne '1' ]; then
		dashy=
		dashB=
		alheight=$[240-$height_orig]
		p1=$[alheight/2]
		dashZ="-Z 352x$alheight"
		dashy="-Y -$p1,0,-$p1,0"
		dv=`echo "scale=1 ; ($alheight/4)"| bc -l`
		dv=`echo $dv | awk -F . '{print $2}'`
	if [ $dv -ne '0' ]; then
		alheight=$[height_orig+2]
		dashZ="-Z 352x$alheight"
		p1=$[240-$alheight]
		p1=$[p1/2]
		dashy="-Y -$p1,0,-$p1,0"
	fi
	fi

# format settings for pal or ntsc...
	if [ "`expr $framerate`" -eq '29' ]; then 
		fps=4
		gopmax_size=30
	fi
	if [ "`expr $framerate`" -eq '30' ]; then 
		fps=5
	fi
	if [ "`expr $framerate`" -eq '25' ]; then
		fps=3
		gopmax_size=25
		Format=p
		norm=PAL
	if [ $mpeg_type -eq '5' ]; then
		SIZE=SIZE_480x576
	if [ $Aspects -eq '1' ]; then
		vop="-vf scale=480:536,expand=480:576"		
		asp=`echo "scale=2 ; ($width_orig / $height_orig)"| bc -l`
		p_aspect="-aspect $asp"
	else
		vop="-vf scale=480:496,expand=480:576"
		asp=`echo "scale=2 ; ($width_orig / $height_orig)"| bc -l`
		p_aspect="-aspect $asp"
	fi
	else
		SIZE=SIZE_352x288
	fi
		height1=$[height_orig /8]
		height1=$[height1 * 8]
		height1=$[height_orig - $height1 ]
		j1=$[height1 /2]
	if [ $j1 -gt '0' ]; then
		dashj="-j $j1,0,$j1,0"
	fi
	if [ $Aspects -eq '1' ]; then
		b1=$[height-288]
		dashy="-Y 0,0,0,0"
	fi
	if [ $Aspects -eq '2' -a $dvdcd -eq '1' ]; then
		b1=$[height-288]
		dashy="-Y 0,0,0,0"
		export_aspect="--export_asr 3"
		aspect_flag=2
	fi
	if [ $Aspects -eq '2' -a $dvdcd -eq '0' -a $vflag -eq '1' ]; then
		b1=$[height-176]
		dashy="-Y -56,0,-56,0"
		export_aspect="--export_asr 2"
		aspect_flag=1
	fi

	if [ $wide_flag -ne '1' ]; then
select_wide
	fi
	if [ $vflag -eq '2' ]; then
		b1=$[height-176]
		aspect=2
		dashy="-Y -56,0,-56,0"
	fi
		b1=$[b1/8]
		b2=$[width-352]
		b2=$[b2/8]
		dashB="-B $b1,$b2,8"

	if [ $height -lt '288' ]; then  
		dashy=
		dashB=
		alheight=$[288-$height_orig]
		p1=$[alheight/2]
		dashZ="-Z 352x$alheight"
		dashy="-Y -$p1,0,-$p1,0"
		dv=`echo "scale=1 ; ($alheight/4)"| bc -l`
		dv=`echo $dv | awk -F . '{print $2}'`
	if [ $dv -ne '0' ]; then
		alheight=$[height_orig+2]
		dashZ="-Z 352x$alheight"
		p1=$[288-$alheight]
		p1=$[p1/2]
		dashy="-Y -$p1,0,-$p1,0"
	fi
	fi
	if [ $modedashZ -eq '1' -a $Aspects -eq '2' ]; then
		dashB=
		dashZ="-Z 352x208"
		dashy="-Y -40,0,-40,0"

	fi
	if [ $modedashZ -eq '1' -a $Aspects -eq '1' ]; then
		dashB=
		dashZ="-Z 352x248"
		dashy="-Y -20,0,-20,0"
	fi
	if [ $modedashZ -eq '1' -a $vflag -eq '1' ]; then
		dashB=
		dashZ="-Z 352x248"
		dashy="-Y -20,0,-20,0"
	fi

	fi

	if [ "`expr $framerate`" -eq '24' ]; then
		fps=2
		gopmax_size=24
	fi
	if [ "`expr $framerate`" -eq '23' ]; then 
		fps=1
		gopmax_size=24
	if [ $mpeg_type -eq '5' -a $MPLAYER -eq '0' ]; then
		pulld="--pulldown"
		dashD="-d"
	fi
	if [ $mpeg_type -eq '5' -a $MPLAYER -eq '1' ]; then
		pulld="-p -d"
	fi
	fi
	if [ "`expr $framerate`" -eq '69' -o $altfps -ne '0' ]; then
		framerate="23"
		framerate_orig="23.976"
		fps=1
		gopmax_size=24
		ivtc="-vf pp=fd:c,pp=tn:64:128:256"
		filters=$filter4
	fi
	if [ $mode528 -eq '1' -a $modedashZ -eq '1' -a $vflag -ne '1' -a $framerate -ne '25' ]; then
		dashZ="-Z 528x400"
		dashy="-Y -40,0,-40,0"
	fi
	if [ $mode528 -eq '0' -a $modedashZ -eq '1' -a $vflag -ne '1' -a $framerate -ne '25' -a $Aspects -eq '2' -a $dvdcd -eq '0' ]; then
		dashZ="-Z 352x200"
		dashy="-Y -20,0,-20,0"
	fi
	if [ $mode528 -eq '0' -a $modedashZ -eq '1' -a $vflag -ne '1' -a $framerate -ne '25' -a $Aspects -eq '1' ]; then
		dashZ="-Z 352x240"
		dashy="-Y 0,0,0,0"
	fi
	if [ $mode288 -eq 1 ]; then
	if [ $mode528 -eq '1' -a $modedashZ -eq '1' -a $vflag -ne '1' -a $framerate -ne '25' ]; then
		dashZ="-Z 528x400"
		dashy="-Y -64,0,-64,0"
	fi

	fi

	if [ $dvdcd -eq '1' -o $modedashZ -eq '1' ]; then
	if [ $mode528 -eq '1' -a $modedashZ -eq '1' -a $vflag -ne '1' -a $framerate -ne '25' ]; then
		dashZ="-Z 528x480"
		dashy=
	fi
	fi

	if [ $width_orig -lt '352' -a $modedashZ -ne '1' ]; then
		dashB="-B $b1,0,8"
                width1=$[352-$width_orig]
                j1=$[width1 /2]
        if [ $j1 -gt '0' -a $framerate -ne '25' ]; then
                dashj="-j 0,-$j1,0,-$j1"
        fi
	fi
# SVCD Mode
	if [ $mpeg_type -eq '5' -a $framerate -ne '25' ]; then
		modedashZ=1
        if [ $vflag -eq '1' ]; then
                dashZ="-Z 480x400"
                dashy="-Y -40,0,-40,0"
        fi
	if [ $wflag -eq '1' ]; then
		dashZ="-Z 480x400"
		dashy="-Y -40,0,-40,0"
	else
		dashZ="-Z 480x460"
		dashy="-Y -10,0,-10,0"
	fi
	fi
	if [ $mpeg_type -eq '5' -a $framerate -eq '25' ]; then
		modedashZ=1
		pulld=
	if [ $vflag -eq '1' ]; then
		dashZ="-Z 480x576"
		dashy=
	fi
	if [ $wflag -eq '1' ]; then
		dashZ="-Z 480x500"
		dashy="-Y -38,0,-38,0"
	else
		dashZ="-Z 480x576"
		dashy=
	fi
	fi				
# End SVCD Mode
	if [ $modedashZ -eq '1' ]; then
                dashB=
	fi
}

# Transcode Coversion to 23.976 fps Function
convert ()
{
	if [ $altfps -ne '23' -a $mpeg_type -ne '5' ]; then
		echo "            Current FPS is $framerate_orig "
		FPS_PROMPT='Do you wish to convert to 23.98 fps Format? (y/n)'

		echo -n "$FPS_PROMPT"
		read line
                        
                case $line in
                        'y')

                    height1=$[height /8]
                    height=$[height1 * 8]

			if [ $Aspects -eq '1' ]; then
				b1=$[height-240]
				b1=$[b1/8]
				b2=$[width-352]
				b2=$[b2/8]
				dashB="-B $b1,$b2,8"
				dashy="-Y 0,0,0,0"
			fi
			if [ $Aspects -eq '2' -a $dvdcd -eq '1' ]; then
				b1=$[height-240]
				b1=$[b1/8]
				b2=$[width-352]
				b2=$[b2/8]
				dashB="-B $b1,$b2,8"
				dashy="-Y 0,0,0,0"
			fi
			if [ $Aspects -eq '2' -a $dvdcd -eq '0' ]; then
				b1=$[height-160]
				b1=$[b1/8]
				b2=$[width-352]
				b2=$[b2/8]
				dashB="-B $b1,$b2,8"
				dashy="-Y -40,0,-40,0"
			fi
				cflag=1
                    export="--export_fps 23.98"   
                    Format=n ;;
                        'Y')    

                    height1=$[height /8]
                    height=$[height1 * 8]

			if [ $Aspects -eq '1' ]; then
				b1=$[height-240]
				b1=$[b1/8]
				b2=$[width-352]
				b2=$[b2/8]
				dashB="-B $b1,$b2,8"
				dashy="-Y 0,0,0,0"
			fi
			if [ $Aspects -eq '2' -a $dvdcd -eq '1' ]; then
				b1=$[height-240]
				b1=$[b1/8]
				b2=$[width-352]
				b2=$[b2/8]
				dashB="-B $b1,$b2,8"
				dashy="-Y 0,0,0,0"
			fi
			if [ $Aspects -eq '2' -a $dvdcd -eq '0' ]; then
				b1=$[height-160]
				b1=$[b1/8]
				b2=$[width-352]   
				b2=$[b2/8]
				dashB="-B $b1,$b2,8"
				dashy="-Y -40,0,-40,0"
			fi
				cflag=1
                    export="--export_fps 23.98"
                    Format=n ;;
                        'n')    cflag=0
				Mode= ;;
                        'N')    cflag=0
				Mode= ;;
                          *)    cflag=0
				Mode= ;;
                esac #

	if [ $height -lt '240' ]; then  
		dashy=
		dashB=
		alheight=$[240-$height_orig]
		p1=$[alheight/2]
		dashZ="-Z 352x$alheight"
		dashy="-Y -$p1,0,-$p1,0"
		dv=`echo "scale=1 ; ($alheight/4)"| bc -l`
		dv=`echo $dv | awk -F . '{print $2}'`

	if [ $dv -ne '0' ]; then
		alheight=$[height_orig+2]
		dashZ="-Z 352x$alheight"
		p1=$[240-$alheight]
		p1=$[p1/2]
		dashy="-Y -$p1,0,-$p1,0"
	fi
	fi
	fi
}

# Command Lines

cmds ()
{

	filter1=modfps=mode=1
	filter2=dnr
	filter3=32detect=force_mode=3
	filter4=ivtc
	filter5=denoise3d=luma=-1

	mjpeg2enc1="cat stream.yuv "
		if [ $alt_scale -eq '1' ]; then
	mjpeg2enc2="nice -n $nice_level y4mscaler -v 0 -S option=box -I norm=$norm -I sar=$norm -O preset=$present"
		else
	mjpeg2enc2="nice -n $nice_level yuvscaler -M $RATIO -v 0  -n $Format "
		fi
	mjpeg2enc3="nice -n $nice_level mpeg2enc -S 10000 -v 0 -f $mpeg_type $matrices -b $max_bitrate \
	$Quantisation -V $video_buffer -n $Format -F $fps -a $aspect \
	-g $gopmin_size -G $gopmax_size $pulld $dashP $dashE -d $cpus $mdashD -o $FILE2.$mpeg_extention "

	mp2enc1="cat stream.pcm "
	mp2enc2="nice -n $nice_level mp2enc -v 0 -b $audio_bitrate -r 44100 -s -o $FILE2.mpa"
	snd="transcode -H 10 -F 2 -b 128 -M 0 --a52_drc_off -V -s 1.234 -y null,mpeg \
	-E 44100 -o $FILE2 -q 0 -x null --nice $nice_level --write_pid $FILE2.pid --print_status 0 -i"

	mplay="nice -n $nice_level mplayer -really-quiet $dashaf $ivtc $vop -noframedrop -ao pcm -aofile stream.pcm -vo yuv4mpeg \
	-osdlevel 0 "
	mplay1="nice -n $nice_level mplayer -really-quiet -nosound $ivtc $vop -noframedrop -vo yuv4mpeg -osdlevel 0 "

	mux="mplex -v 0 -V -r 3000 -b 224 -f 2 -S $output_size \
	-o $FILE2-$output_type%d.mpg $FILE2.$mpeg_extention $FILE2.mpa"

	mux1="mplex -v 0 -f $mpeg_type -S $output_size \
	-o $FILE2-$output_type%d.mpg $FILE2.$mpeg_extention $FILE2.mpa" 

	trans1="transcode -F $mpeg_type,"
	trans2=`echo -S 10000 -g $gopmin_size -G $gopmax_size $matrices $Quantisation -V $video_buffer \
	-n $Format $dashP $dashD $dashE $dashR $cpus $mdashD ` 
	trans3=" $export_aspect -b $audio_bitrate --a52_drc_off $dashd 
	$dashJ $filters $dashV $dashZ -f $framerate_orig $export $dashs -g $hxw $dashm $dashj $dashB 
	-y mpeg2enc,mpeg -E 44100 -o $FILE2 -q 1 -w $max_bitrate 
	$dashx $Nice $dashy $pulld $dashh $dasha $dasht --print_status 20 $subs $dvd_a_delay -i "

} # Function declaration must precede call.

dvdread ()
                
{
tcprobe -H 20 -T $ii -i "$FILE" >& tcprobe.info
        while read line; do
                line=`echo $line | sed 's/[[]//g'`
                line=`echo $line | sed 's/[]]//g'`
        if [ "$line" != "${line#(dvd_reader.c) DVD title}" ]; then
                angle=${line#(dvd_reader.c) DVD title}
                angle=`echo $angle | awk '{print $4}'`
        fi
        if [ "$line" != "${line#import}" ]; then
                dashg=${line#import}
                dashg=`echo $dashg | awk '{print $4}'`
                width=`echo $dashg | awk -F x '{print $1}'`
                height=`echo $dashg | awk -F x '{print $2}'`
                dashg=`echo $dashg | awk '{print "-g "$1}'`
		hxw="$width"x"$height"
	fi
	if [ "$line" != "${line#aspect}" ]; then
		aspect=${line#aspect}
		aspect=`echo $aspect | awk '{print $2}'`
	fi
        if [ "$line" != "${line#frame}" ]; then
                framerate=${line#frame}
                framerate=`echo $framerate | awk '{print $3}'`
        fi
        if [ "$line" != "${line#tcprobe V:}" ]; then
                frames=${line#tcprobe V:}
                frames=`echo $frames | awk '{print $1}'`
        fi
 done < tcprobe.info
		quant=`echo "scale=0 ; ($frames / $framerate)"| bc -l`
		quant1=$quant
                HOURS=`echo "scale=0 ; ($frames / $framerate /60 / 60)"| bc -l`
                MINUTES=`echo "scale=0 ; ($frames / $framerate / 60 - $HOURS*60 )"| bc -l`
                total=$[HOURS * 60 + $MINUTES]
        if [ $aspect == "4:3" ]; then
                Aspects=1
		dvd_aspect=1
        else
                Aspects=2
		dvd_aspect=2
        fi
}

dvdcdrom ()
{
err=0
i=1
t=0
	while [ "$i" -lt "98" ] ; do

		tcprobe -H 10 -T $i -i "$FILE" >& tcprobe.info

        while read line; do
	# detect movie format...        
		line=`echo $line | sed 's/[[]//g'`
		line=`echo $line | sed 's/[]]//g'`
	if [ "$line" != "${line#Invalid title}" ]; then
		err=1
	fi
	if [ "$line" != "${line#(iodump.c) unable}" ]; then
		echo "Error Reading DvD Drive!"
		exit   
	fi
        if [ "$line" != "${line#(dvd_reader.c) DVD title}" ]; then
                angle=${line#(dvd_reader.c) DVD title}
		title=${line#(dvd_reader.c) DVD}
		title=`echo $title |sed 's/[:]//g'| awk '{print $1" "$2}'`
		dtitle=`echo $title | awk -F / '{print $2}'`
		ti="DVD "$title
		angle=`echo $angle | awk '{print $4}'`
		pflag=0
	if [ $i -eq '1' -a $pflag -eq '0' ]; then
		echo ""
		echo "             Processing $dtitle DvD Titles, This May take a few Seconds"
		echo ""
		pflag=1
	fi
	fi
        if [ "$line" != "${line#import}" ]; then
                dashg=${line#import}
                dashg=`echo $dashg | awk '{print $4}'`     
		hxw=$dashg
                width=`echo $dashg | awk -F x '{print $1}'` 
                height=`echo $dashg | awk -F x '{print $2}'`
                dashg=`echo $dashg | awk '{print "-g "$1}'`
		hxw="$width"x"$height"
        fi      
        if [ "$line" != "${line#aspect}" ]; then
                aspect=${line#aspect}
                aspect=`echo $aspect | awk '{print $2}'`
        fi
        if [ "$line" != "${line#frame}" ]; then
                framerate=${line#frame}
                framerate=`echo $framerate | awk '{print $3}'`
        fi
        if [ "$line" != "${line#tcprobe V:}" ]; then
                frames=${line#tcprobe V:}
                frames=`echo $frames | awk '{print $1}'`
        fi
	done < tcprobe.info
		rm -f tcprobe.info
		quant=`echo "scale=0 ; ($frames / $framerate /60)"| bc -l`
	if [ $err -eq '1' ]; then
		i=100
		quant=0
	fi

	if [ $quant -gt '30' ]; then
		quant=`echo "scale=0 ; ($frames / $framerate)"| bc -l`
		quant1=$quant
                HOURS=`echo "scale=0 ; ($frames / $framerate /60 / 60)"| bc -l`
                MINUTES=`echo "scale=0 ; ($frames / $framerate / 60 - $HOURS*60 )"| bc -l`
                total=$[HOURS * 60 + $MINUTES]
		ii=$i
	if [ $ii -lt '10' ]; then
		echo $ii" )  $ti  $total Minutes" >>titles.select
		angles=$angle
		t=$[t+1]
	else
		echo $ii")  $ti  $total Minutes" >>titles.select
		angles=$angle
		t=$[t+1]
	fi
        if [ $aspect == "4:3" ]; then
                Aspects=1
		dvd_aspect=1
        else
                Aspects=2
		dvd_aspect=2
        fi
	fi
		i=$[i+1]
	done
	if [ $t -gt '1' ]; then
		cat titles.select
		DVD_Title_PROMPT='Which DVD Title to Convert --> '
		echo ""
		echo -n "  $DVD_Title_PROMPT"
		read ii
# Call DvDRead Function
		dvdread
	fi
		rm -f titles.select 

dashg=
framerate_orig=$framerate
framerate=`echo $framerate | awk -F . '{print $1}'`
dashx="-x dvd"
dvdcd=1
FILE2=DVD
vformat=DVD
acodec=a52
width_orig=$width
height_orig=$height
dashm=
bitrate=5000
dashJ=
dasht="-T $ii,-1,1"
dashh="-H 10 "
s=0
a=0
	if [ $angles -gt '1' ]; then
		echo "-----------------------------------------------"
		echo "  There are $angles Angles"
		echo "-----------------------------------------------"
		DVD_Angle_PROMPT='Which  Angle to Convert --> '	
		echo -n "  $DVD_Angle_PROMPT"
		read angle
		dasht="-T $ii,-1,$angle"
	fi
tcprobe -H 10 -T $ii -i "$FILE" >tcprobe.info 2> /dev/null


	while read line; do
	if [ "$line" != "${line#(dvd_reader.c) ac3}" -o "$line" != "${line#(dvd_reader.c) dts}" ]; then
		enc=${line#(dvd_reader.c)}
		enc2=`echo $enc | awk '{print $5}'`
		enc1=`echo $enc | awk '{print $1}'`
		tmp=`echo $enc | awk '{print $2}'`
# Call Sellect Language Function
		sellang
		echo $a")   " "$tmp" $enc1 $enc2 "Audio">>select.lang
		aa=$a
		a=$[a+1]
	fi
	if [ "$line" != "${line#(dvd_reader.c) subtitle}" ]; then
		line=`echo $line | sed 's/[<]//g'`
		line=`echo $line | sed 's/[>]//g'`
		sub=${line#(dvd_reader.c) subtitle}
		tmp=`echo $sub | awk -F = '{print $2}'`
# Call Sellect Language Function
		sellang
		sub=`echo $sub | awk -F = '{print $1}'`
		sub=`echo "scale=0 ; $sub "| bc -l`
		echo $sub")   " "$tmp" "SubTitles" >>select.subs
		s=$[s+1]
	fi
		done < tcprobe.info
		rm -r tcprobe.info
	if [ $aa -gt '0' ]; then
		echo "-----------------------------------------------"
		cat select.lang
		echo "-----------------------------------------------"
		echo ""
		DVD_Lang_PROMPT='Select the Language for Audio ---> '
		echo -n "$DVD_Lang_PROMPT"
		read audio  
		dasha="-a $audio"
	fi
		rm -f select.lang

	if [ $s -gt '0' ]; then
		echo ""
		echo "-----------------------------------------------"
		cat select.subs
		echo "-----------------------------------------------"
		DVD_SUBS_PROMPT='Select a Subtitle or just hit \033[1mEnter for None\033[0m ---> '
		echo ""
		echo -n -e " $DVD_SUBS_PROMPT"
		read Subs
		case $Subs in
			'0')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'1')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'2')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'3')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'4')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'5')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'6')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'7')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'8')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'9')	subs="-J extsub=$Subs:0:0:0:0" ;;
			'10')	subs="-J extsub=$Subs:0:0:0:0" ;;
			*)	subs="" ;;
		esac #

	fi
		rm -f select.subs
		quant=$quant1
		DVD_PROMPT='Enter the KVCD/XVCD Title Name --> '
		echo ""
		echo -n "  $DVD_PROMPT"
		read FILE2

	if [ $FILE2  ]; then 
		echo
	else
		exit
	fi
} # End Dvd Read Section...

sellang ()

{

	if [ $tmp == "aa" ]; then
		tmp="Afar           "
	fi
        if [ $tmp == "ab" ]; then
                tmp="Abkhazian      "
        fi
        if [ $tmp == "af" ]; then
                tmp="Afrikaans      "
        fi
        if [ $tmp == "am" ]; then
                tmp="Amharic        "
        fi
	if [ $tmp == "ar" ]; then
		tmp="Arabic         "
	fi
	if [ $tmp == "as" ]; then
		tmp="Assamese       "
	fi
	if [ $tmp == "ay" ]; then
		tmp="Aymara         "
	fi
	if [ $tmp == "az" ]; then
		tmp="Azerbaijani    "
	fi
	if [ $tmp == "ba" ]; then
		tmp="Bashkir        "
	fi
	if [ $tmp == "be" ]; then
		tmp="Byelorussian   "
	fi
	if [ $tmp == "bg" ]; then
		tmp="Bulgarian      "
	fi
	if [ $tmp == "bh" ]; then
		tmp="Bihari         "
	fi
	if [ $tmp == "bi" ]; then
		tmp="Bislama        "
	fi
	if [ $tmp == "bn" ]; then
		tmp="Bengali, Bangla"
	fi
	if [ $tmp == "bo" ]; then
		tmp="Tibetan        "
	fi
	if [ $tmp == "br" ]; then
		tmp="Breton         "
	fi
	if [ $tmp == "ca" ]; then
		tmp="Catalan        "
	fi
	if [ $tmp == "co" ]; then
		tmp="Corsican       "
	fi
	if [ $tmp == "cs" ]; then
		tmp="Czech          "
	fi
	if [ $tmp == "cy" ]; then
		tmp="Welsh          "
	fi
	if [ $tmp == "da" ]; then
		tmp="Dansk          "
	fi
	if [ $tmp == "de" ]; then
		tmp="Deutsch        "
	fi
	if [ $tmp == "dz" ]; then
		tmp="Bhutani        "
	fi
	if [ $tmp == "el" ]; then
		tmp="Greek          "
	fi
	if [ $tmp == "en" ]; then
		tmp="English        "
	fi
	if [ $tmp == "eo" ]; then
		tmp="Esperanto      "
	fi
	if [ $tmp == "es" ]; then
		tmp="Espanol        "
	fi
	if [ $tmp == "et" ]; then
		tmp="Estonian       "
	fi
	if [ $tmp == "eu" ]; then
		tmp="Basque         "
	fi
	if [ $tmp == "fa" ]; then
		tmp="Persian        "
	fi
	if [ $tmp == "fi" ]; then
		tmp="Suomi          "
	fi
	if [ $tmp == "fj" ]; then
		tmp="Fiji           "
	fi
	if [ $tmp == "fo" ]; then
		tmp="Faroese        "
	fi
	if [ $tmp == "fr" ]; then
		tmp="Francais       "
	fi
	if [ $tmp == "fy" ]; then
		tmp="Frisian        "
	fi
	if [ $tmp == "ga" ]; then
		tmp="Gaelic         "
	fi
	if [ $tmp == "gd" ]; then
		tmp="Scots, Gaelic  "
	fi
	if [ $tmp == "gl" ]; then
		tmp="Galician       "
	fi
	if [ $tmp == "gn" ]; then
		tmp="Guarani        "
	fi
	if [ $tmp == "gu" ]; then
		tmp="Gujarati       "
	fi
	if [ $tmp == "ha" ]; then
		tmp="Hausa          "
	fi
	if [ $tmp == "he" ]; then
		tmp="Hebrew         "
	fi
	if [ $tmp == "hi" ]; then
		tmp="Hindi          "
	fi
	if [ $tmp == "hr" ]; then
		tmp="Hrvatski       "
	fi
	if [ $tmp == "hu" ]; then
		tmp="Magyar         "
	fi
	if [ $tmp == "hy" ]; then
		tmp="Armenian       "
	fi
	if [ $tmp == "ia" ]; then
		tmp="Interlingua    "
	fi
	if [ $tmp == "id" ]; then
		tmp="Indonesian     "
	fi
	if [ $tmp == "ie" ]; then
		tmp="Interlingue    "
	fi
	if [ $tmp == "ik" ]; then
		tmp="Inupiak        "
	fi
	if [ $tmp == "in" ]; then
		tmp="Indonesian     "
	fi
	if [ $tmp == "is" ]; then
		tmp="Islenska       "
	fi
	if [ $tmp == "it" ]; then
		tmp="Italiano       "
	fi
	if [ $tmp == "iu" ]; then
		tmp="Inuktitut      "
	fi
	if [ $tmp == "iw" ]; then
		tmp="Hebrew         "
	fi
	if [ $tmp == "ja" ]; then
		tmp="Japanese       "
	fi
	if [ $tmp == "ji" ]; then
		tmp="Yiddish        "
	fi
	if [ $tmp == "jw" ]; then
		tmp="Javanese       "
	fi
	if [ $tmp == "ka" ]; then
		tmp="Georgian       "
	fi
	if [ $tmp == "kk" ]; then
		tmp="Kazakh         "
	fi
	if [ $tmp == "kl" ]; then
		tmp="Greenlandic    "
	fi
	if [ $tmp == "km" ]; then
		tmp="Cambodian      "
	fi
	if [ $tmp == "kn" ]; then
		tmp="Kannada        "
	fi
	if [ $tmp == "ko" ]; then
		tmp="Korean         "
	fi
	if [ $tmp == "ks" ]; then
		tmp="Kashmiri       "
	fi
	if [ $tmp == "ku" ]; then
		tmp="Kurdish        "
	fi
	if [ $tmp == "ky" ]; then
		tmp="Kirghiz        "
	fi
	if [ $tmp == "la" ]; then
		tmp="Latin          "
	fi
	if [ $tmp == "ln" ]; then
		tmp="Lingala        "
	fi
	if [ $tmp == "lo" ]; then
		tmp="Laothian       "
	fi
	if [ $tmp == "lt" ]; then
		tmp="Lithuanian     "
	fi
	if [ $tmp == "lv" ]; then
		tmp="Latvian        "
	fi
	if [ $tmp == "mg" ]; then
		tmp="Malagasy       "
	fi
	if [ $tmp == "mi" ]; then
		tmp="Maori          "
	fi
	if [ $tmp == "mk" ]; then
		tmp="Macedonian     "
	fi
	if [ $tmp == "ml" ]; then
		tmp="Malayalam      "
	fi
	if [ $tmp == "mn" ]; then
		tmp="Mongolian      "
	fi
	if [ $tmp == "mo" ]; then
		tmp="Moldavian      "
	fi
	if [ $tmp == "mr" ]; then
		tmp="Marathi        "
	fi
	if [ $tmp == "ms" ]; then
		tmp="Malay          "
	fi
	if [ $tmp == "mt" ]; then
		tmp="Maltese        "
	fi
	if [ $tmp == "my" ]; then
		tmp="Burmese        "
	fi
	if [ $tmp == "na" ]; then
		tmp="Nauru          "
	fi
	if [ $tmp == "ne" ]; then
		tmp="Nepali         "
	fi
	if [ $tmp == "nl" ]; then
		tmp="Nederlands     "
	fi
	if [ $tmp == "no" ]; then
		tmp="Norsk          "
	fi
	if [ $tmp == "oc" ]; then
		tmp="Occitan        "
	fi
	if [ $tmp == "om" ]; then
		tmp="Oromo          "
	fi
	if [ $tmp == "or" ]; then
		tmp="Oriya          "
	fi
	if [ $tmp == "pa" ]; then
		tmp="Punjabi        "
	fi
	if [ $tmp == "pl" ]; then
		tmp="Polish         "
	fi
	if [ $tmp == "ps" ]; then
		tmp="Pashto         "
	fi
	if [ $tmp == "pt" ]; then
		tmp="Portugues      "
	fi
	if [ $tmp == "qu" ]; then
		tmp="Quechua        "
	fi
	if [ $tmp == "rm" ]; then
		tmp="Rhaeto-Romance "
	fi
	if [ $tmp == "rn" ]; then
		tmp="Kirundi        "
	fi
	if [ $tmp == "ro" ]; then
		tmp="Romanian       "
	fi
	if [ $tmp == "ru" ]; then
		tmp="Russian        "
	fi
	if [ $tmp == "rw" ]; then
		tmp="Kinyarwanda    "
	fi
	if [ $tmp == "sa" ]; then
		tmp="Sanskrit       "
	fi
	if [ $tmp == "sd" ]; then
		tmp="Sindhi         "
	fi
	if [ $tmp == "sg" ]; then
		tmp="Sangho         "
	fi
	if [ $tmp == "sh" ]; then
		tmp="Serbo-Croatian "
	fi
	if [ $tmp == "si" ]; then
		tmp="Sinhalese      "
	fi
	if [ $tmp == "sk" ]; then
		tmp="Slovak         "
	fi
	if [ $tmp == "sl" ]; then
		tmp="Slovenian      "
	fi
	if [ $tmp == "sm" ]; then
		tmp="Samoan         "
	fi
	if [ $tmp == "sn" ]; then
		tmp="Shona          "
	fi
	if [ $tmp == "so" ]; then
		tmp="Somali         "
	fi
	if [ $tmp == "sq" ]; then
		tmp="Albanian       "
	fi
	if [ $tmp == "sr" ]; then
		tmp="Serbian        "
	fi
	if [ $tmp == "ss" ]; then
		tmp="Siswati        "
	fi
	if [ $tmp == "st" ]; then
		tmp="Sesotho        "
	fi
	if [ $tmp == "su" ]; then
		tmp="Sundanese      "
	fi
	if [ $tmp == "sv" ]; then
		tmp="Svenska        "
	fi
	if [ $tmp == "sw" ]; then
		tmp="Swahili        "
	fi
	if [ $tmp == "ta" ]; then
		tmp="Tamil          "
	fi
	if [ $tmp == "te" ]; then
		tmp="Telugu         "
	fi
	if [ $tmp == "tg" ]; then
		tmp="Tajik          "
	fi
	if [ $tmp == "th" ]; then
		tmp="Thai           "
	fi
	if [ $tmp == "ti" ]; then
		tmp="Tigrinya       "
	fi
	if [ $tmp == "tk" ]; then
		tmp="Turkmen        "
	fi
	if [ $tmp == "tl" ]; then
		tmp="Tagalog        "
	fi
	if [ $tmp == "tn" ]; then
		tmp="Setswana       "
	fi
	if [ $tmp == "to" ]; then
		tmp="Tonga          "
	fi
	if [ $tmp == "tr" ]; then
		tmp="Turkish        "
	fi
	if [ $tmp == "ts" ]; then
		tmp="Tsonga         "
	fi
	if [ $tmp == "tt" ]; then
		tmp="Tatar          "
	fi
	if [ $tmp == "tw" ]; then
		tmp="Twi            "
	fi
	if [ $tmp == "ug" ]; then
		tmp="Uighur         "
	fi
	if [ $tmp == "uk" ]; then
		tmp="Ukrainian      "
	fi
	if [ $tmp == "ur" ]; then
		tmp="Urdu           "
	fi
	if [ $tmp == "uz" ]; then
		tmp="Uzbek          "
	fi
	if [ $tmp == "vi" ]; then
		tmp="Vietnamese     "
	fi
	if [ $tmp == "vo" ]; then
		tmp="Volapuk        "
	fi
	if [ $tmp == "wo" ]; then
		tmp="Wolof          "
	fi
	if [ $tmp == "xh" ]; then
		tmp="Xhosa          "
	fi
	if [ $tmp == "yi" ]; then
		tmp="Yiddish        "
	fi
	if [ $tmp == "yo" ]; then
		tmp="Yoruba         "
	fi
	if [ $tmp == "za" ]; then
		tmp="Zhuang         "
	fi
	if [ $tmp == "zh" ]; then
		tmp="Chinese        "
	fi
	if [ $tmp == "zu" ]; then
		tmp="Zulu           "
	fi
	if [ $tmp == "xx" ]; then
		tmp="Unknown        "
	fi
	if [ $tmp == "drc" ]; then
		tmp="Unknown        "
	fi
}

select_wide ()
{

	if [ $height_orig -gt $width_orig ]; then
		echo
	else
		wide_flag=1

	if [ $dvdcd -eq '0' -a $vflag -eq '0' -a $mpeg_type -ne '5' ]; then

	if [ $framerate -ne '25' ]; then
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/4*3)) * (240)+.0001)"| bc -l | awk -F . '{print $1}'`
	else
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/4*3)) * (288)+.0001)"| bc -l | awk -F . '{print $1}'`
	fi
	if [ $advanced -eq '1' -a $Aspects -eq '2' ]; then
		Boarders_PROMPT='  Do you Wish Black Boarders? (y/n)'
	echo -n "$Boarders_PROMPT"
                read line
	else
	if [ $Aspects -eq '2' ]; then
		line="y"
	else
		line="n"
	fi
	fi
                
                case $line in
			'y')    b1=$[height-160]
				aspect=2
			if [ $Aspects -eq '1' ]; then
				asp=`echo "scale=2 ; ($width_orig / $height_orig)"| bc -l`
				p_aspect="-aspect $asp"
			else
				asp=`echo "scale=2 ; ($width_orig / $height_orig)"| bc -l`
				p_aspect="-aspect $asp"
			fi
				wflag=1
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
				dashy="-Y -40,0,-40,0"
				export_aspect="--export_asr 2"
				Aspects=2
				aspect_flag=1 #;;

			if [ $framerate -eq '25' ]; then
                                b1=$[height-176]
                                aspect=2
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:288"
				dashy="-Y -56,0,-56,0" #;;
			fi
				;;

			'Y')    b1=$[height-160]
				aspect=2
			if [ $Aspects -eq '1' ]; then
				asp=`echo "scale=2 ; ($width_orig / $height_orig)"| bc -l`
				p_aspect="-aspect $asp"
			else
				asp=`echo "scale=2 ; ($width_orig / $height_orig)"| bc -l`
				p_aspect="-aspect $asp"
			fi
				wflag=1
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
				dashy="-Y -40,0,-40,0"
				export_aspect="--export_asr 2"
				Aspects=2
				aspect_flag=1 #;;
                        if [ $framerate -eq '25' ]; then
                                b1=$[height-176]
                                aspect=2
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:288"
				dashy="-Y -56,0,-56,0"
                        fi
				;;

			'n')    b1=$[height-240]
				Aspects=1
				p_aspect="-aspect 1"
				vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
				wflag=2
				aspect=2
				dashy="-Y 0,0,0,0" #;;
			if [ $framerate -eq '25' ]; then
                                b1=$[height-288]
                                Aspects=1
				vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:288"
                                aspect=2
                                wflag=2
                                dashy="-Y 0,0,0,0" #;;
			fi
				;;

			'N')    b1=$[height-240]
				Aspects=1
				p_aspect="-aspect 1"
				vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
				aspect=2
				wflag=2
				dashy="-Y 0,0,0,0" #;;   

                        if [ $framerate -eq '25' ]; then
                                b1=$[height-288]
                                Aspects=1
				vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:288"
                                aspect=2
                                wflag=2
                                dashy="-Y 0,0,0,0" #;;
                        fi
                                ;;

			  *)    b1=$[height-240]
				Aspects=1
				vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
				aspect=2
				wflag=2
				dashy="-Y 0,0,0,0" #;;

			if [ $framerate -eq '25' ]; then
                                b1=$[height-288]
                                Aspects=1
				vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:288"
                                aspect=2
                                wflag=2
                                dashy="-Y 0,0,0,0" #;;
			fi
				;;
		esac #
		export_aspect="--export_asr 2"
		aspect_flag=1
	fi
	fi
}

detect ()
{
	if [ -e "$FILE" -a $dvdcd -eq '1' ]; then
                mplayer "$FILE1" -identify -vo null -ao null -frames 23 > movie.info 2> /dev/null
        while read line; do

# detect movie framerate...
        if [ "$line" != "${line#ID_VIDEO_BITRATE=}" ]; then
                bitrate=${line#ID_VIDEO_BITRATE=}
		bitrate_orig=$bitrate
                bitrate=$[bitrate/1000]
        fi
	if [ $width -lt '720' ]; then
# detect movie height...
	if [ "$line" != "${line#ID_VIDEO_HEIGHT=}" ]; then
		height=${line#ID_VIDEO_HEIGHT=}
	fi
# detect movie width...
	if [ "$line" != "${line#ID_VIDEO_WIDTH=}" ]; then
		width=${line#ID_VIDEO_WIDTH=}
	fi
		width_orig=$width
		height_orig=$height
	fi
# detect aspect ratio (if defined)..."
        if [ "$line" != "${line#Movie-Aspect is }" ]; then
                movie_aspect=${line#Movie-Aspect is }
                movie_aspect=`echo "$movie_aspect" | sed s/:.*//`
                if [ "$movie_aspect" == "undefined - no prescaling applied." ]; then
			movie_aspect=`echo "scale=0 ; ($width_orig / $height / 2 * 2)" | bc -l`
                fi
                if [ $input_aspect ]; then
                       movie_aspect=$input_aspect
                fi
        fi

                done < movie.info
		rm -f movie.info
		hxw="$width"x"$height"
	fi

	if [ -e "$FILE" -a $dvdcd -eq '0' ]; then

		mplayer "$FILE" -identify -vo null -ao null -frames 23 > movie.info 2> /dev/null
	while read line; do

# detect movie format...
        if [ "$line" != "${line#ID_VIDEO_FORMAT=}" ]; then
                vformat=${line#ID_VIDEO_FORMAT=}
        fi
# detect movie framerate...
	if [ "$line" != "${line#ID_VIDEO_BITRATE=}" ]; then
		bitrate=${line#ID_VIDEO_BITRATE=}
		bitrate_orig=$bitrate
		bitrate=$[bitrate/1000]
	fi
# detect movie height...
	if [ "$line" != "${line#ID_VIDEO_HEIGHT=}" ]; then
		height=${line#ID_VIDEO_HEIGHT=}
	fi
# detect movie width...
	if [ "$line" != "${line#ID_VIDEO_WIDTH=}" ]; then
		width=${line#ID_VIDEO_WIDTH=}
		width_orig=$width
	fi
# detect fps to determine output file fps...
	if [ "$line" != "${line#ID_VIDEO_FPS=}" ]; then
		framerate=${line#ID_VIDEO_FPS=}
		framerate_orig="$framerate"
	if [ $framerate = '24.999' ] ; then
		framerate=25
		framerate_orig="24.999"
	else 
		framerate=`echo $framerate| awk -F . '{print $1}'`
	fi
    fi

# detect movie audio codec...
        if [ "$line" != "${line#ID_AUDIO_CODEC=}" ]; then
                acodec=${line#ID_AUDIO_CODEC=}
        fi
# figure out how long the movie is...
	if [ "$line" != "${line#ID_LENGTH=}" ]; then
		quant=${line#ID_LENGTH=}
	fi
# detect 3:2 TELECINE
	if [ "$line" != "${line#demux_mpg}" ]; then
		altfps=23
                framerate=$[altfps*3]
	fi
# detect aspect ratio (if defined)..."
        if [ "$line" != "${line#Movie-Aspect is }" ]; then
                movie_aspect=${line#Movie-Aspect is }
                movie_aspect=`echo "$movie_aspect" | sed s/:.*//`
                if [ "$movie_aspect" == "undefined - no prescaling applied." ]; then
			movie_aspect=`echo "scale=0 ; ($width_orig / $height)"| bc -l`
		if [ $movie_aspect -eq '0' -o $movie_aspect -eq '1' ]; then
			movie_aspect=`echo "scale=2 ; ($width_orig / $height) + .55"| bc -l`
		fi
                fi
                if [ $input_aspect ]; then
                        movie_aspect=$input_aspect
                fi
        fi

		done < movie.info

# check for supported FPS...
		test_framerate=`echo "scale=0 ; $framerate"| bc -l`
	if [ $framerate -eq '23' -a $test_framerate -ne  '23' ]; then
		MPLAYER=0
	fi
	if [ $framerate -eq '25' -a $test_framerate -ne '25' ]; then
		MPLAYER=0
	fi
	if [ $framerate -eq '29' -a $test_framerate -ne '29' ]; then
		MPLAYER=0
	fi

# end FPS...
	 else
	if [ $dvdcd -eq '0' ]; then
		echo -e "\a"
		echo "------------------------> $FILE File does not Exist"
		echo "      Can not Continue"
		echo "         EXITING "
		exit 0
 	fi
  fi
	if [ $dvdcd -eq '0' ]; then
		HOURS=$[quant / 3600]  
		MINUTES=$[quant - 3600 * $HOURS]
		MINUTES=$[MINUTES /60]
		HOURS=$[HOURS * 60]
			rm -f movie.info
	fi

                FileLength=`basename "$FILE" | wc -c`
                for ext in .asf .avi .mov .mpg .mpeg .ogm .rm .vob \
                        .ASF .AVI .MOV .MPG .MPEG .OGM .RM .VOB \
                        .Avi .Asf .Mov .Mpg .Mpeg .Ogm .Rm .Vob; do

        if [ $FileLength -gt "`basename "$FILE" $ext | wc -c`" ]; then
                FILE2=`basename "$FILE" $ext`
		ext2=$ext
	fi
		done

# get the correct length for vbr mpeg's with mpgtx
	if [ $dvdcd -eq '0' ]; then
	if [ $quant -eq '0' -o $altfps -ne '0' -a $dvdcd -eq '0' ]; then

	if [ $ext2 == ".mpg" -o $ext2 == ".Mpg" -o $ext2 == ".MPG" \
		-o $ext2 == ".mpeg" -o $ext2 == ".Mpeg" -o $ext2 == ".MPEG" ]; then

		echo "Check for MPGTX needed for mpeg file conversion..."
		echo ""
		for exe in mpgtx ; do
	if [ -z "`which $exe`" ]; then   
		echo -e "\a"
		echo ""
		echo ""
                echo "Error: $exe isn't installed or isn't in your path!"
		echo "You really need this if you want to encode vbr video."
		echo ""
		echo "You can download this utility from the link below."
		echo "http://mpgtx.sourceforge.net/"
		echo ""
		echo ""
                exit 1
        fi
 done

		mpgtx -i $1 > movie.info 2>/dev/null

# make time readings human readable...
       while read line; do
	if [ "$line" != "${line#Estimated}" ]; then
		quant=${line#Estimated}
		quant=`echo $quant | awk '{print $2}'`
		q_length=`echo $quant | wc -c`

	if [ $q_length -eq '13' ]; then
		HOURS=`echo $quant | awk -F : '{print $1}'`
		HOURS=$[HOURS * 60]
		MINUTES=`echo $quant | awk -F : '{print $2}'`
		MINUTES=$[MINUTES/1]
	  else
		HOURS=0
	fi

	if [ $q_length -eq '10' ]; then
		MINUTES=`echo $quant | awk -F : '{print $1}'`
		MINUTES=$[MINUTES/1]
	fi

	if [ $q_length -eq '7' ]; then
		MINUTES=0
	  fi
	fi
		quant=$[HOURS+MINUTES]
		quant=$[quant*60]
		mpgtxflag=1
	done < movie.info
		rm -f movie.info
	fi

	fi

	if [ $ext2 == ".ogm" -o $ext2 == ".Ogm" -o $ext2 == ".OGM" ]; then   
		echo "Check for OGMINFO needed for ogm file conversion..."
		ogm_info=0
                for exe in ogminfo ; do
        if [ -z "`which $exe`" ]; then
                echo -e "\a"
                echo ""
                echo ""
                echo "Error: $exe isn't installed or isn't in your path!"
                echo "You really need this if you want to encode ogm video."
                echo ""
                echo "You can download this utility from the link below."
                echo "http://www.bunkus.org/videotools/ogmtools/"
                echo ""
                echo ""
		sleep 3
		ogm_info=1
        fi
 done
	if [ $ogm_info -eq '0' ]; then
	ogminfo -s "$FILE" > movie.info 2> /dev/null

       while read line; do
        if [ "$line" != "${line#(ogminfo.c) (v1/serial 0) stream size:}" ]; then
                quant=${line#(v1/serial 0) stream size:}
		bitrate=`echo $quant | awk '{print $8}' | awk -F . '{print $1}' | sed 's/[(]//g'`
                quant=`echo $quant | awk '{print $19}'`
		quant=`echo $quant | awk -F . '{print $1}'`
		quant=$[quant/1000]
		quant=$[quant/60]
		HOURS=$[quant/60]
		MINUTES=$[quant-$HOURS*60]
		HOURS=$[HOURS*60]
        fi        
	done < movie.info	
	rm -f movie.info
        fi
        fi
	fi

}

scan_versions ()

{
transcode -v>&transcode.version
transdisplay=`cat transcode.version | awk '{print $2}' | awk -F '(' '{print $1}'`
transversion=`cat transcode.version | awk '{print $2}' | awk -F . '{print $1""$2""$3}' | sed 's/v/ /g'`
	if [ $transversion -gt '0610' ]; then
		dvd_a_delay=$dvd_a_delay
	else
		dvd_a_delay=
	fi
	if [ $transversion -gt '069' ]; then
		Nice="--nice $nice_level"
		dashx="-x mplayer"
	else
		dashx="-x mplayer"
		Nice=
	fi
	if [ $transversion -gt '0610' ]; then
		dashx= #"-x mplayer"
	fi
	if [ $transversion -lt '0611' -a $MPLAYER -eq '0' ]; then
		echo " You May want to Upgrade Transcode to 0.6.11 "
		echo " Version 0.6.11 uses ffmpeg for importing files being converted "
		echo " Plus you also get Fast Resize for the -Z option "
		sleep 3
	fi	
		rm -f transcode.version
	if [ -x stream.yuv ]; then
		rm -f stream.yuv
	fi

        mplayer --version >mplayer.version 2>/dev/null
        while read line; do
        if [ "$line" != "${line#MPlayer}" ]; then
		mplayerv=${line#MPlayer}
		mplayerv1=`echo $mplayerv | awk '{print $1}' | awk -F . '{print $1}'`
		mplver=`echo $mplayerv | awk '{print $1}'`
		mplayerv=`echo $mplayerv | awk '{print $1}' | awk -F - '{print $1}'`
        fi

        done < mplayer.version
        rm -r mplayer.version

	if [ $mplayerv == "dev" ]; then
		mplayerv=`echo $mplver | awk -F - '{print $2"-"$3}'`
		mplayerv1=1
	fi

        if [ $mplayerv1 -eq '0' -a $dvdcd -eq '1' ]; then
                FILE1="-dvd $ii"
        else
                FILE1="dvd://$ii"
        fi

# Check for Version of MjpegTools 1.x or 2.x (or pre-2 ;-)
  mplex >& mjpeg.version
		mjpeg_version=`grep version mjpeg.version | awk '{ print $4}'`
		mjpeg_subversion=`echo $mjpeg_version | awk -F . '{print $4}'`
		mjpeg_version=`echo $mjpeg_version | awk -F . '{print $1""$2""$3}'`
	if [ $mjpeg_subversion ]; then
		mjpeg_subversion=$mjpeg_subversion
	else
		mjpeg_subversion=00
	fi
		mjpeg_version=`echo $mjpeg_version$mjpeg_subversion`

	if [ $mjpeg_version -gt '16190' -o $mjpeg_version -eq '16190' -a $modestandard -eq '0' ]; then
		matrices="-K kvcd"
		output_type="KVCD"
	fi
	if [ $mjpeg_version -eq '16100'  -o $mjpeg_version -le '16100' -a $modestandard -eq '0' ]; then
		matrices="-N -h"
		output_type="XVCD"
		output_size="794"
	fi
	if [ $mjpeg_version -gt '16190' ]; then
		dashE= #"-E -20"
		dashR= #"-R 2"
		mdashD="-D 10"
	fi
	if [ $mjpeg_version -gt '16192' ]; then
		video_buffer=120
	fi
		rm -f mjpeg.version

}

select_16:9 ()

{

		test_aspect=`echo "scale=0 ; ($width_orig / $height_orig)"| bc -l`
		current_aspect=`echo "scale=2 ; ($width_orig / $height_orig)"| bc -l`
	if [ $test_aspect -ne '1' -o $dvd_aspect -eq '2' -o $Aspects -ne '3' ]; then
	if [ $wflag -eq '1' ]; then


	if [ $advanced -eq '1' ]; then
Aspect_PROMPT="  Do you want 16:9 widescreen dvd-style black-borders? (y/n)"
		echo -n "$Aspect_PROMPT"
		read line
	else
		line="y"
	if [ $test_framerate -ne '25' -a $cflag -eq '0' ]; then
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/16*9)) * (240)+.0001)"| bc -l | awk -F . '{print $1}'`
	else
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/16*9)) * (288)+.0001)"| bc -l | awk -F . '{print $1}'`
	fi

	fi
		case $line in
			'y')    Mode=
				aspect=3
				export_aspect="--export_asr 3"
				aspect_flag=2
				p_aspect="-aspect $movie_aspect"

	if [ $test_framerate -ne '25' -a $cflag -eq '0' ]; then
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/16*9)) * (240)+.0001)"| bc -l | awk -F . '{print $1}'`
	else
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/16*9)) * (288)+.0001)"| bc -l | awk -F . '{print $1}'`
	fi

			if [ $test_framerate -eq '25' -a $cflag -eq '0' ]; then
				dashZ="-Z 352x208"
				dashy="-Y -40,0,-40,0"
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:288"
			fi
			if [ $cflag -eq '1' ]; then
				dashZ="-Z 352x160"
				dashy="-Y -40,0,-40,0"
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
			fi
			if [ $test_framerate -ne '25' -a $cflag -eq '0' ]; then
				dashZ="-Z 352x160"
				dashy="-Y -40,0,-40,0"
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
			fi
				Aspects=2 ;;
			'Y')    Mode=
				aspect=3
				export_aspect="--export_asr 3"
				aspect_flag=2
				p_aspect="-aspect $movie_aspect"
	if [ $test_framerate -ne '25' -a $cflag -eq '0' ]; then
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/16*9)) * (240)+.0001)"| bc -l | awk -F . '{print $1}'`
	else
exp_height=`echo "scale=9 ; (($height_orig/($width_orig/16*9)) * (288)+.0001)"| bc -l | awk -F . '{print $1}'`
	fi

                        if [ $test_framerate -eq '25' -a $cflag -eq '0' ]; then
                                dashZ="-Z 352x208"
                                dashy="-Y -40,0,-40,0"
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:288"
                        fi  
                        if [ $cflag -eq '1' ]; then
                                dashZ="-Z 352x160"
                                dashy="-Y -40,0,-40,0"
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
                        fi
                        if [ $test_framerate -ne '25' -a $cflag -eq '0' ]; then
                                dashZ="-Z 352x160"
                                dashy="-Y -40,0,-40,0"
		vop="-vf scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240"
                        fi
				Aspects=2 ;;
			'n')    Mode=RESAMPLE
				aspect_flag=1
				aspect=2
				p_aspect="-aspect 1.78"
				export_aspect="--export_asr 2" ;;
			'N')    Mode=RESAMPLE
				aspect_flag=1
				aspect=2
				p_aspect="-aspect 1.333"
				export_aspect="--export_asr 2" ;; 
			  *)    p_aspect="-aspect 1.78"
				line=n
				Mode= ;;
		esac #

fi
fi
        if [ $line == "y" -o $line == "Y" ]; then
        if [ $max_bitrate -lt '900' ]; then
                max_bitrate=2400
        fi
        fi


}

# show a litle hint if no file is given...
PARAM=$#
	if [ $PARAM -eq 0 ]; then
		echo -e "\a"
		echo "---------------------------------------------------------------------------"
		echo "        $MkxVcd_Version"
		echo ""
	if [ $MPLAYER -eq '0' ]; then
		echo -e "        \033[1mThis Script is currently setup to use Transcode\033[0m "
		echo ""
	else
		echo -e "        \033[1mThis Script is currently setup to use Mplayer\033[0m "
		echo ""
	fi
		echo "       mkxvcd Yourfilename.whateverextensionmplayercanread to Convert "
		echo "       -d for debug, -m to force Mplayer conversion, -t for Transcode "
		echo ""
		echo "       Or use your DvD Drive for the Source "
		echo "       mkxvcd /dev/dvd or /dev/cdrom "
		echo "       DvD's at present only work with Transcode "
		echo "---------------------------------------------------------------------------"
		exit 1
	else

# Say welcome...
		echo "Lets boogie..."
		echo ""
Option=$2
	if [  $Option  ]; then
		Option=`echo $Option | sed 's/-//g'`   # Removes Dashes in Options
	if [ $Option == "d" ]; then
		set -x
	fi
	if [ $Option == "m" ]; then
		MPLAYER=1
	fi
	if [ $Option == "t" ]; then
		MPLAYER=0
	fi
        if [ $Option == "a" ]; then
                input_aspect="$3"               #force aspect ratio of input file
                input_aspect_cmd="-aspect $3"
        fi
	fi
# check for needed applications...
for exe in transcode mplex mplayer awk wc sed tcprobe bc grep yuvscaler mp2enc mpeg2enc; do
	if [ -z "`which $exe`" ]; then
		echo -e "\a"
		echo "Error: $exe isn't installed or isn't in your path!"
		exit 1
	fi
	done

	if [ $alt_scale -eq '1' ]; then
for exe in y4mscaler; do
	if [ -z "`which $exe`" ]; then
	alt_scale=0
	fi
	done
	fi

# Call Scan Versions to detect the Versions of the Supported Utilities
scan_versions
# extract info from file...
FILE=$1

	if [ "$FILE" == "/dev/cdrom" -o "$FILE" == "/dev/dvd" ]; then
	if [ $MPLAYER -eq '0' ]; then
# Call DvDCdrom function
		dvdcdrom
        else
        echo -e "\a"  
        echo ""
        echo " DvD Access not functional with the Mplayer Version of Mkxvcd "
        echo " Edit The Script and set MPLAYER=0 or use -t on commandline "
        echo ""
        echo -e "\a"
        exit
	fi	
	fi


#Call Detect Source Video Stats Function
detect

#Call Video Type Function
videotype
	if [ $height -lt $width ]; then

#Call Crop Detect Function
crop_detect
	fi
        if [ $dvdcd -eq '0' ]; then
                hxw="$width"x"$height"
                height_orig=$height
                width_orig=$width
                total=$[HOURS+$MINUTES]
                Aspects=`echo "scale=2 ; ($width_orig / $height)" | bc -l  | sed 's/[.]//g'`
        if [ $Aspects -gt '176' ]; then
                Aspects=2
        else                    
                Aspects=1
        fi
        fi

echo ""
echo -e "$black     Source Video Stats"
echo -e "$black---------------------------------------------"
echo -e "$black   BitRate       $red$bitrate"
echo -e "$black   Width/Height  $red$width$black"x"$red$height"                   
echo -e "$black   Total Minutes $red$total"                     
movie_aspect1=`echo "scale=2 ; ($width_orig / $height)"| bc -l`
echo -e "$black   Aspect Ratio  $red$movie_aspect1:1"          
echo -e "$black---------------------------------------------"
echo ""
$reset

# see if fullscreen is wanted...
vflag=0
	if [ `echo "scale=0 ; ($width / $height)"| bc -l` -eq '1' -a $dvdcd -eq '0' -a $bypass_fullscreen -eq '0' -a $mpeg_type -ne '5' ]; then
		Vid_PROMPT='Video Is Full Screen, add black borders? (y/n)'

	echo -n "$Vid_PROMPT"
	read line

		case $line in
			'y')    Mode=
				aspect=2
				vflag=2
				Aspects=2 ;;
			'Y')    Mode=
				aspect=2
				vflag=2
				Aspects=2 ;;
			'n')    Mode=RESAMPLE
				export_aspect="--export_asr 2"
				aspect=2
				Aspects=1
				vflag=1 ;;
			'N')    Mode=RESAMPLE
				export_aspect="--export_asr 2"
				aspect=2
				Aspects=1
				vflag=1 ;;
			  *)    Mode=
				aspect=2 ;;
		esac #
	fi


# set basename of the movie...
	if [ $dvdcd -eq '0' ]; then
		FileLength=`basename "$FILE" | wc -c`
		for ext in .asf .avi .mov .mpg .mpeg .ogm .rm .vob \
			.ASF .AVI .MOV .MPG .MPEG .OGM .RM .VOB \
			.Avi .Asf .Mov .Mpg .Mpeg .Ogm .Rm .Vob; do
	if [ $FileLength -gt "`basename "$FILE" $ext | wc -c`" ]; then
		FILE2=`basename "$FILE" $ext`
	if [ $ext == ".ogm" ]; then
		dashx="-x mplayer,ogg"
	fi
        fi
		done
 fi

# Tell people that there format is not supported :-( 
FileLength=`basename "$FILE2" | wc -c`
	if [ $FileLength -eq '1' -a $dvdcd -eq '0' ]; then
		echo -e "\a"
		echo "Supported Formats -> asf avi mov mpg mpeg ogm rm"
		echo ""
		echo "Unsupported File Format $FILE"
		echo "Nothing to Do."
		echo ""
		exit 0
	fi
	FILE2=`echo "$FILE2" | sed 's/ /-/g'| sed 's/[{]//g' | sed 's/[}]/-/g'\
		| sed 's/[[]//g' | sed 's/[]]//g' | sed 's/[(]//g' | sed 's/[)]//g' | sed 's/[..]/./g'`	# Replace Spaces in File Names with a "-"

	if [ -e $FILE2.mpa ]; then
		echo "------------------------------------------"
		echo " Is there another Mkxvcd Process running? "
		echo "------------------------------------------"
		echo -n " $FILE2.mpa exists, Continue? (y/n)"
		read line
               case $line in
                        'y')    echo ;;
                        'Y')    echo ;;
                        'n')    echo " Ending Session "
				exit 0 ;;
                        'N')    echo " Ending Session "
				exit 0 ;;
			 *)	echo " Inproper Responce Exiting! "
				exit ;;
                esac #
	fi


                tmpframerate=`echo $framerate_orig | awk -F . '{print $1}'`
        if [ $framerate -ne '23' -a $transversion -gt '068' -a $MPLAYER -eq '0' ]; then
# Call Conversion Function
convert
	if [ $cflag -eq '1' ]; then
		framerate=23
	fi
        fi

#Call Resize Function
calctype

#Call Quantisation and Bitrate Function
select_quant

# abort if fps is not 29, 30, 25, 24, 23...
	if [ "$fps" == "" -a $framerate -ne '25' ]; then
		echo Source Movie is not standard Exiting!!!
		echo FrameRate $framerate
		exit 1
	fi

	if [ $mpeg_type -eq '0' -o $mpeg_type -eq '1' -o $mpeg_type -eq '2' ]; then
		mpeg_extention=m1v
	else
		mpeg_extention=m2v
	fi

#Call Select 16:9 WideScreen
select_16:9

#Call Quantisation and Bitrate Function
select_quant
	fi
		vop=`echo $vop | awk '{print $2}'`
	if [ $mplayerv1 -eq '0' ]; then
		vop=`echo $vop | awk -F , '{print $3","$2","$1}'`
		vopc=`echo $vopc | awk -F , '{print $1}'`
		vop="-vop $vop,$vopc"
	else
		vop="-vf $vopc$vop"
	fi
	if [ $dvdcd -eq '1' ]; then
		FILE=$FILE1

		vop="-vf $vopc"
		vop="$vop"scale=$width_orig:$height_orig,scale=352:$exp_height,expand=352:240""
	fi
	if [ $preview -eq '1' ]; then
p_aspect=
forceidx=
#pop up mplayer to show a sample
	if [ $ext2 == ".ogm" -o $ext2 == ".Ogm" -o $ext2 == ".OGM" ]; then
		forceidx="-forceidx"
	fi
	if [ $MPLAYER -eq '1' -a $mplayerv1 -eq '0' ]; then
		vop=`echo $vop | awk '{print "-vop "$2}'`
		echo "mplayer $forceidx -really-quiet -ss 250 -nosound $p_aspect $vop "$FILE" -frames 500"
		mplayer $forceidx -really-quiet -ss 250 -nosound $p_aspect $vop "$FILE" -frames 500 -vo x11
	else
		echo "mplayer $forceidx -really-quiet -ss 250 -nosound $p_aspect $vop "$FILE" -frames 500"
		mplayer $forceidx -really-quiet -ss 250 -nosound $p_aspect $vop "$FILE" -frames 500 -vo x11
	fi
	fi
		FILE="$1"
	if [ $mpeg_type -eq '1' ]; then
		Quantisation=
	else
		Quantisation="-q $quantisation"
	fi
	if [ $mod_quant_vcdbitrate -eq '1' ]; then
echo ""
echo -e "                 Current Quantisation is \033[1m$quantisation\033[0m "
Quant_PROMPT="  Whould you like to Select another \033[1mQuantisation 1,2,3,4,5,6\033[0m ? "
                echo -n -e "$Quant_PROMPT"
                read line
                          
                case $line in

			'1') quantisation=1 ;;
			'2') quantisation=2 ;;
			'3') quantisation=3 ;;
			'4') quantisation=4 ;;
			'5') quantisation=5 ;;
			'6') quantisation=6 ;;
			  *) ;;
		esac #

     if [ $quantisation -eq '3' -a $aspect_flag -ne '2' ]; then
          max_bitrate=`echo "scale=0 ; (($exp_height * 795) / $total)"| bc -l`
     fi
     if [ $quantisation -eq '3' -a $aspect_flag -eq '2' ]; then
          max_bitrate=`echo "scale=0 ; ((($exp_height-57) * 795) / $total)"| bc -l`
     fi
     if [ $quantisation -eq '4' -a $aspect_flag -ne '2' ]; then
          max_bitrate=`echo "scale=0 ; ((($exp_height+25) * 795) / $total)"| bc -l`
     fi
     if [ $quantisation -eq '4' -a $aspect_flag -eq '2' ]; then
          max_bitrate=`echo "scale=0 ; ((($exp_height) * 795) / $total)"| bc -l`
     fi
	if [ $quantisation -gt '5' ]; then
		max_bitrate=`echo "scale=0 ; ((($exp_height+$exp_height) * 795) / $total)"| bc -l`
	fi
echo -e "                 Current Video Bitrate is \033[1m$max_bitrate\033[0m "
Bitrate_PROMPT="  Change the value or \033[1mEnter for Defaults\033[0m ? "
		line=$max_bitrate
                echo -n -e "$Bitrate_PROMPT"
                read line   

	if [ $line ]; then
		max_bitrate=$line
		echo ""
	else
		echo ""
	fi
	fi
        if [ $mpeg_type -eq '1' ]; then
                Quantisation=
        else
                Quantisation="-q $quantisation"
        fi
        if [ $acodec == "a52" ]; then
                dashaf="-af volume=10"
        fi
# Call Commands Function
cmds

	if [ $MPLAYER -eq '1' ]; then
		echo "$total Minutes"
		echo ""
		echo "$hxw  $framerate_orig"
		echo ""
		echo "$mjpeg2enc2"  
		echo ""
		echo "$mjpeg2enc3 "
		echo ""

mkfifo -m 660 stream.yuv                                # create video and audio pipes...


	$mjpeg2enc1 | $mjpeg2enc3 &
#	$mjpeg2enc1 | $mjpeg2enc2 | $mjpeg2enc3 &


	if [ $audio_compat -eq '0' ]; then
		echo "$mplay"
		mkfifo -m 660 stream.pcm
		$mp2enc1 | $mp2enc2 &   # capture the audio pipe and encode...
		$mplay "$FILE"
	else
		echo "$mplay1"
		$snd "$FILE" 2> /dev/null &
		$mplay1 "$FILE"
                pid=`cat $FILE2.pid`
                kill -9 $pid 2> /dev/null
                rm -f $FILE2.pid

	fi

                        rm -f stream.yuv                # after finishing, remove the pipes...
                rm -f stream.pcm
        else

	if [ $modedashZ -eq '1' -a $MPLAYER -eq '0' -a $transversion -gt '0610' -a $Fast -eq '1' ]; then
		Transcode_DashZ='    Do you wish to use Transcodes New Fast Resize ---> '
		echo -n "$Transcode_DashZ"
		read line
                case $line in
			'y')    dashB=
				dashZ="$dashZ,fast"
				Fast=1 ;;
			'Y')    dashB=
				dashZ="$dashZ,fast"
				Fast=1 ;;
			'n')    dashB=
				Fast=0 ;;
			'N')    dashB=
				Fast=0 ;;
			  *)    dashB=
				Fast=0 ;;
                esac #

	fi

	 test_framerate=`echo "scale=0 ; $framerate"| bc -l`
	if [ $altfps != '0' ]; then
		filters="$filters"
	fi
	if [ $acodec == "a52" ]; then
		dashs="-s 1.234"
	fi
	if [ $bitrate -lt '900' -a $transversion -gt '0611' ]; then
		filters="$filter5,$filters"
	fi
	if [ $bitrate -lt '900' -a $transversion -lt '0612' ]; then
		filters="$filter2,$filters"
	fi
	if [ $cflag -eq '1' ]; then
		filters="$filter1,$filters"
	fi
	if [ $framerate -eq '23' -a $framerate_orig != "23.976" -a $cflag -ne '1' -a $altfps -eq '0' ]; then
		filters="$filters,$filter1"
		export="--export_fps 23.98"
	fi
	if [ $framerate -eq '25' -a $test_framerate -ne '25' -a $cflag -ne '1' -a $altfps -eq '0' ]; then
		filters="$filters,$filter1"
		export="--export_fps 25.00"
	fi
	if [ $framerate -eq '29' -a $framerate_orig != "29.970" -a $cflag -eq '1' -a $altfps -ne '0' -o $framerate -eq '29' -a $framerate_orig != "29.971" -a $cflag -eq '1' -a $altfps -ne '0' ]; then
		filters="$filters,$filter1"
		export="--export_fps 29.971"
	fi
		echo "$total Minutes"
		echo ""
		echo $filters
		echo ""
cmds
echo "$trans1 "$trans2" $trans3 "$FILE" "
echo ""

$trans1 "$trans2" $trans3 "$FILE"
fi


	if [ $mpeg_type -eq '1' -o $mpeg_type -eq '5' ]; then
		$mux1 >& mplex.error.$FILE2
	else
		$mux >& mplex.error.$FILE2				# multiplex the streams to a mpg...
	fi
		mux_error=`grep write mplex.error.$FILE2 | awk '{print $4}' | wc -w`
	if [ $mux_error -eq '0' ]; then
   rm -f $FILE2.$mpeg_extention 					# remove what we don't need anymore...
  rm -f $FILE2.mpa
 rm -f mplex.error.$FILE2
echo ""
	else
                echo " There has been a Write error "$FILE2""-$output_type1.mpg" "
                echo " Please Check the output files or disk space "
                echo "$FILE2.m1v and $FILE2.mp2 have not been Removed. "
	fi

# say some nice things and also put some usefull info on the screen and in a file...
echo -e "\a"
echo ""
echo "All Done, enjoy your mpeg!"
echo ""
echo "If you have vcdimager and cdrdao on your system around, "
echo "and the rights to cdrdao and your devices are all setup, "
echo "you could also use the following;"
echo "vcdimager -t vcd11 -c xvcd.cue -b xvcd.bin yourmovie.mpg"
echo "cdrdao write --device 1,0,0 --driver generic-mmc-raw --speed 24 xvcd.cue"
echo "Off-course you have to replace the --device, --driver and --speed settings to your system"
echo "Or yust use your favourite tool to burn the cd..."
echo "BTW, don't be fooled by the 700 Mb. printed on your cd, it can contain 795Mb. of video... (I think ;)"

# Show statistics on commandline and also put them in a file...

	if [ $log_cmds -eq '1' -a $MPLAYER -eq '0' ]; then
echo "$trans1 "$trans2" $trans3 "$FILE" " >$FILE2-"$output_type".stats
	else
echo "$mjpeg2enc1 | $mjpeg2enc2 | $mjpeg2enc3" >$FILE2-"$output_type".stats
echo "$mp2enc1 | $mp2enc2"  >>$FILE2-"$output_type".stats
echo "$mplay $FILE "  >>$FILE2-"$output_type".stats
	fi

	if [ $Aspects -eq '1' ]; then
		Aspect="Full Screen Video"
	else
		Aspect="Wide Screen Video"
	fi

	if [ $log_cmds -eq '1' -a $MPLAYER -eq '0' ]; then
		echo "                  Video Source Stats " >>$FILE2-"$output_type".stats
	else
		echo "                  Video Source Stats " >>$FILE2-"$output_type".stats
	fi
		echo "       ------------------------------------------------">>$FILE2-"$output_type".stats
		echo "       Width Height "$width_orig"X"$height_orig"  $Aspect ">>$FILE2-"$output_type".stats
	if [ $bitrate -eq '0' ]; then
		bitrate=VBR             
	fi
		echo "       BitRate      $bitrate ">>$FILE2-"$output_type".stats
		echo "       FrameRate    $framerate_orig ">>$FILE2-"$output_type".stats
		echo "       Length       $[$HOURS / 60] Hours $MINUTES Minutes ">>$FILE2-"$output_type".stats


	if [ $framerate  -ne '25' -a $mpeg_type -ne '5' ]; then
		width="352x240"
	else
		width="352x288"
	fi
	if [ $framerate  -ne '25' -a $mpeg_type -eq '5' ]; then
		width="480x480"
	fi
	if [ $framerate  -eq '25' -a $mpeg_type -eq '5' ]; then
		width="480x576"
	fi
	if [ $cflag -eq '1' ]; then
		width="352x240"
	fi

	if [ $aspect_flag -eq '1' ]; then
		aspect_type="4:3"
	else
		aspect_type="16:9"
	fi

	if [ $Aspects -eq '1' ]; then
		Aspect="Full Screen $aspect_type $output_type Video"
	else
		Aspect="Wide Screen $aspect_type $output_type Video"
	fi

		echo "">>$FILE2-"$output_type".stats
		echo "">>$FILE2-"$output_type".stats
		echo "                  Conversion Stats ">>$FILE2-"$output_type".stats
		echo "       ------------------------------------------------">>$FILE2-"$output_type".stats
		echo "       Width Height "$width"  $Aspect ">>$FILE2-"$output_type".stats
		echo "       BitRate      $max_bitrate ">>$FILE2-"$output_type".stats
		echo "       Quantisation  $quantisation ">>$FILE2-"$output_type".stats
	if [ $cflag -eq '1' ]; then
		echo "       FrameRate    23.976 ">>$FILE2-"$output_type".stats
	else
		echo "       FrameRate    $framerate_orig ">>$FILE2-"$output_type".stats
	fi
		echo "       Length       $[$HOURS / 60 ] Hours $MINUTES Minutes ">>$FILE2-"$output_type".stats
	if [ $MPLAYER -eq '1' ]; then
		echo "">>$FILE2-"$output_type".stats
		echo "       Conversion done with Mplayer Version $mplayerv">>$FILE2-"$output_type".stats
	else
		echo "">>$FILE2-"$output_type".stats
		echo "       Conversion done with Transcode $transdisplay">>$FILE2-"$output_type".stats
	fi
	if [ $MPLAYER -eq '0' ]; then
		echo "">>$FILE2-"$output_type".stats
		echo "                  Plugin Filters Used ">>$FILE2-"$output_type".stats
		echo "       ------------------------------------------------">>$FILE2-"$output_type".stats
		echo "       $filters " >>$FILE2-"$output_type".stats
	else
		echo "">>$FILE2-"$output_type".stats
	fi
		echo "">>$FILE2-"$output_type".stats
		echo "                  Size of Converted files ">>$FILE2-"$output_type".stats
		echo "       ------------------------------------------------">>$FILE2-"$output_type".stats
# Check and Display for output files and Size of each

	for mpg in "$FILE2"-"$output_type"1.mpg "$FILE2"-"$output_type"2.mpg \
		"$FILE2"-"$output_type"3.mpg "$FILE2"-"$output_type"4.mpg \
		"$FILE2"-S"$output_type"1.mpg "$FILE2"-S"$output_type"2.mpg \
		"$FILE2"-S"$output_type"3.mpg "$FILE2"-S"$output_type"4.mpg; do
 
	if [ -s "$mpg" ]; then
		mpg=`du -h "$mpg"`
		echo "       $mpg">>$FILE2-"$output_type".stats
	else     
		echo
	fi
		done

		ended=`date +%s`
		total_seconds=$[$ended-$started]
		HOURS=$[total_seconds / 3600]
		MINUTES=$[total_seconds - 3600 * $HOURS]
		MINUTES=$[MINUTES/60]
		SEC=$[$total_seconds % 60]

	if [ $HOURS -eq '1' ]; then
		b_hours=Hour
	else
		b_hours=Hours
	fi
	if [ $MINUTES -eq '1' ]; then
		b_min=Minute
	else
		b_min=Minutes
	fi
	if [ $SEC -eq '1' ]; then
		b_sec=Second
	else
		b_sec=Seconds
	fi

		echo "">>$FILE2-"$output_type".stats
		echo "">>$FILE2-"$output_type".stats
		echo "      Total Run Time ------------------->   $HOURS $b_hours $MINUTES $b_min $SEC $b_sec ">>$FILE2-"$output_type".stats
		echo "">>$FILE2-"$output_type".stats
		echo "">>$FILE2-"$output_type".stats
		cat $FILE2-"$output_type".stats
	if [ $Log -eq '0' ]; then
		rm -f $FILE2-"$output_type".stats
	fi
	if [ $Log -eq '0' ]; then
		rm -f $FILE2-"$output_type".stats
	fi
