#!/bin/bash 

############################################################################
#    Copyright (C) 2005 by David                                           #
#    david@base                                                            #
#                                                                          #
#    This program is free software; you can redistribute it and#or modify  #
#    it under the terms of the GNU General Public License as published by  #
#    the Free Software Foundation; either version 2 of the License, or     #
#    (at your option) any later version.                                   #
#                                                                          #
#    This program is distributed in the hope that it will be useful,       #
#    but WITHOUT ANY WARRANTY; without even the implied warranty of        #
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         #
#    GNU General Public License for more details.                          #
#                                                                          #
#    You should have received a copy of the GNU General Public License     #
#    along with this program; if not, write to the                         #
#    Free Software Foundation, Inc.,                                       #
#    59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             #
###########################################################################



# quick and nasty general purpose video editing menu by DaveQB
# 15/01/2006
#
# need to work on file creation for all menu options, right now the small scripts dont verbose well
# duration 
# rm *.txt -f && touch list.txt ;  for i in *.avi ; do echo " " >> list.txt ; echo $i >> list.txt  ; tcprobe -i $i | awk '/duration/ {print $6}'  >> list.txt  ; done



if [ $# -lt 1 ]
then echo "Not enough arguments"
	echo "Please supply a file name as an argument"
	exit 1
fi


FULL_INPUT="$1"
INPUT=`basename "$*"`
OUTPUT=`basename "${INPUT%.*}"-done.avi`
#INFOV=$(file "$X" | awk  -F , '{print $5$3$4}')
#INFOA=$(file) 
BITRATE="1000"
ORIG_WD=`file "$FULL_INPUT" | awk -F , '{print $3}' | awk '{print $1}'`
# WD="512"
ORIG_HT=`file "$FULL_INPUT" | awk -F , '{print $3}' | awk '{print $3}'`
# HT="384"
DEINT="y"
PS3="Choose:  "
AUD="n"
WD=`file "$FULL_INPUT" | awk -F , '{print $3}' | awk '{print $1}'`
HT=`file "$FULL_INPUT" | awk -F , '{print $3}' | awk '{print $3}'`


function input {
        echo -n "Type in $1:  "
	        read $2
	}
function header {
#	echo "Writing file to "$1""
        echo "#!/bin/bash" > "$1"
        echo "# MEnocder bash script created by DaveQB's mini script" >> "$1"
        echo " " >>  "$1"
        echo " " >>  "$1"
        echo "renice 19 -p \$\$ > /dev/null " >>  "$1"
		}
				
function menu {
#clear
while true
do	
	if [ $AUD == "y" ]
	then PASS="2"
	else PASS="3"
	fi

#clear
echo "###################################" 
echo "Input file $X"
file "$FULL_INPUT" | awk -F , '{print $5$3$4}'
file "$FULL_INPUT" | awk -F , '{print $6$7}'
echo "###################################" 
a="Bitrate ($BITRATE)"
b="Output name ("$OUTPUT")"
c="Resolution ($WD:$HT)"
d="Deinterlace ($DEINT)"
e="Remove audio from a video file (direct copy)"
f="Encode the audio from video to Ogg"
g="Copy the audio from video to Wave"
h="Encode video ONLY, removing the audio ($AUD)"
j="Theora encoding"
x="Write QB script and quit"
y="Write ${PASS}-pass script and quit"
z="Quit without saving"
select i in "$a" "$b" "$c" "$d" "$e" "$f" "$g" "$h" "$j" "$x" "$y" "$z"
do 

# input is a function created earlier
if [ "$i" == "$a" ]
	then input bitrate BITRATE
	break	
elif [ "$i" == "$b" ]
	then input "output name" OUTPUT
		# test for dir existance, if no, warn user and set flag NO_DIR so we can create it in the script later
		if [ -d "${OUTPUT%\/*}" ]
		then echo "Directory currently exists" 
		else 
			NO_DIR="${OUTPUT%\/*}"
			echo "WARNING Directory doesnt exist" 
			echo "Hit any key to continue"
		 	read garbage	
		fi
		break
		
# Resolution entry
elif [ "$i" == "$c" ] 
	then
	# loop until valid answer entered eg between 768-80
	
		input "width (eg 720)" WD
			while [ "$WD" -gt 768 ] || [ "$WD" -lt 80 ]
			do
				echo "Width out of range"
				echo "Width needs to be less then 768 and greater then 80"
				input "width (eg 720)" WD
				
			done
		if [ -z "$WD" ]
		then WD=720
		fi
		
	# loop until valid answer is entered eg bewteen 576-40
	
		input "height (eg 576)" HT
			while  [ "$HT" -gt 576 ] || [ "$WD" -lt 40 ]
			do
				echo "Height out of range"
				echo "Height needs to be less then 576 and greater then 40"
				input "height (eg 576)" HT
			done
		if [ -z "$HT" ]
		then HT=576
		fi
		break	
		
elif [ "$i" == "$d" ]
	then if [ "$DEINT" == "y" ]
	then DEINT="n"
	else DEINT="y"
	fi
	break
	
elif [ "$i" == "$e" ]
	then file="${1%.*}"
#	header "${OUTPUT%-done.*}-nosound.sh" 
	(mencoder "$FULL_INPUT" -ovc copy -nosound  -o "${OUTPUT%-done.*}-nosound.avi")
	#chmod 775 "${OUTPUT%-done.*}-nosound.sh"
	break
	
elif [ "$i" == "$f" ]
	then echo "Outputting to ${OUTPUT%-done.*}.ogg"
	(ffmpeg -i "$FULL_INPUT"  -vn -f wav - | oggenc -o ${OUTPUT%-done.*}.ogg -)
		break
		
elif [ "$i" == "$g" ]
	then
		echo "Outputting WAVE to ${OUTPUT%-done.*}.wav"
		(mplayer -vc dummy -vo null -ao pcm:file=${OUTPUT%-done.*}.wav "$FULL_INPUT")
		break
		
elif [ "$i" == "$h" ]
	then 
		if [ "$AUD" == "y" ]
			then AUD="n"
			else AUD="y"
		fi
	break

# for Theora script creating or live encoding

elif [ "$i" == "$j" ]
	then	
	while true
	do
		if [ -n "$QUAL" ]
			then zero="Set bitrate rather then quality($QUAL)"
			else zero="Set quality rather then bitrate($BITRATE)"
		fi
		
		echo "Change settings on Main Menu"
		one="Write script"
		two="Encode NOW!"
		three="Go back to Main Menu"
		four="or q to Quit"
		
		select i in "$zero" "$one" "$two" "$three" "$four"
		do 
			if [ "$i" == "$zero" ]
				then 
				if [ -n "$QUAL" ]
					then 	
						input bitrate BITRATE 
					       	QUAL=""
					else input quality QUAL 
						while [ "$QUAL" -gt 10 ] || [ "$QUAL" -lt "-1" ]
						do echo "quality out of range"
							echo "Quality must be between -1 and 10"
							input quality QUAL
						done
				fi
				break
				
			elif [ "$i" == "$one" ]
				then
					if [ "$DEINT" == "y" ]
						then INTER="--deinterlace"
					fi
					if [ "$AUD" == "y" ]
						then aud="--nosound"
					fi
					
					echo "Writing file to ${INPUT%.*}.sh"
					header "${INPUT%.*}.sh"
				echo "ffmpeg2theora --nice 19 -o "$OUTPUT" -x "$WD" -y "$HT" "$INTER" "$aud" -v "$QUAL"  "$FULL_INPUT" " >> "${INPUT%.*}.sh"
				chmod 755 "${INPUT%.*}.sh"
				exit
				# res high ${res#*:}  width ${RES%:*}

			elif [ "$i" == "$two" ]
				then echo "$i"
					
			elif [ "$i" == "$three" ]
				then break 2
			elif [ "$i" == "$four" ] 
				then echo "exiting...."
				exit 0

			fi
			
		done		
	done 
	break
		


# resume normal menu options
elif [ "$i" == "$y" ] || [ "$i" == "$x" ]
	then 
	echo "Writing file to ${INPUT%.*}.sh"
	header "${INPUT%.*}.sh"
	if [ $DEINT == "y" ]
	then INTERLACE="lavcdeint,"
	else INTERLACE=""
	fi

	if [ $AUD == "n" ]
	then AUDIO1="-oac copy" && AUDIO2="-oac mp3lame -lameopts mode=3:vbr=0:br=80:ratio=1"
	else AUDIO1="-nosound"
	fi
	
	if [ "$i" == "$y" ]
	then



###############  3 pass with mp3
	echo "mencoder -of avi -ffourcc XVID -sws 2 -vf ${INTERLACE}scale=${WD}:${HT} -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:vbitrate=$BITRATE:trell:vhq:keyint=125:vpass=1 "$AUDIO1" "$FULL_INPUT"   -o /dev/null"  >> "${INPUT%.*}.sh"

	echo " " >> "${INPUT%.*}.sh"

	echo "mencoder -of avi -ffourcc XVID -sws 2 -vf ${INTERLACE}scale=${WD}:${HT} -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:vbitrate=$BITRATE:trell:vhq:keyint=125:vpass=2 "$AUDIO1" "$FULL_INPUT"  -o "$OUTPUT""   >> "${INPUT%.*}.sh"
	
	echo " " >> "${INPUT%.*}.sh"

if [ $AUD == "n" ]
then
	echo "mencoder -of avi -ffourcc XVID -sws 2 -vf ${INTERLACE}scale=${WD}:${HT} -mc 0 -noskip  -ovc copy  "$AUDIO2" "$OUTPUT"  -o "${OUTPUT%.*}-3pass.avi""   >> "${INPUT%.*}.sh"

fi
	chmod 755 "${INPUT%.*}.sh"


############# Quality based, 1 pass	
	else 
	input quality QUAL
	echo " " >> "${INPUT%.*}.sh"

	echo "mencoder -of avi -ffourcc XVID -sws 2 -vf ${INTERLACE}scale=${WD}:${HT} -ovc lavc -lavcopts vcodec=mpeg4:mbd=2:vqscale="${QUAL}":trell:vhq:keyint=125 "$AUDIO2" "$FULL_INPUT"  -o "$OUTPUT""  >> "${INPUT%.*}.sh"

	fi

	chmod 755 "${INPUT%.*}.sh"	
	echo "Quitting ...."
	exit 0

elif [ "$i" == "$z" ] 
	then 
	exit 0

fi
done
# while done
done
}

menu
exit 0
