#!/bin/bash
#
Encoding=UTF-8
#
#############################################################################
#
#Author D.M-Wilhelm aka Leiche <kellerleicheorig@aol.com> at Nov 05 2010
#Website <www.pclinuxos.com> 
#Designed for PCLinuxOS'10
#License GPL2
#
#############################################################################
#
# test for zenity - Teste auf Zenity
#
if which zenity >/dev/null; then
  DIALOG=`which zenity`
else 
  $DIALOG --yesno $"Zenity is required to run this script.\n\nWould you like to install it now?"
  if [ "$?" == "0" ]; then
    synaptic &
    exit 1
  elif [ "$?" == "1" ]; then
    $DIALOG --sorry $"Exiting now"
    exit 1
  fi
fi
#
# i18n - Internationalization - Internationalisierung
#
export TEXTDOMAIN=myconverter
export TEXTDOMAINDIR="/usr/share/locale"
#
# Define variable - Variable definieren
#
TITLE="MyConverter"
ICON="/usr/share/icons/myconvert.png"
#
#=================================================
# function - Funktion 
usage ()
{
OPEN1=`zenity --file-selection --title=$TITLE \
--file-filter="*.avi *.mp4 *.flv *.mp3 *.ogg *.aac *.flac *.wav *.wma *.mkv" \
--file-filter="*.AVI *.MP4 *.MP3 *.FLV *.OGG *.AAC *.FLAC *.WAV *.WMA *.MKV" \
--file-filter="* all files" \
--window-icon=$ICON`
if [ "$OPEN1" = "" ]; then
	exit
fi
}
#
if [ $# -eq 0 ];
then
usage
fi
#
OPEN="$1"
#
#
#cut old file extension - entfernen der alten Dateierweiterung
#
SEL=$(echo $OPEN$OPEN1 |  cut -d "." -f 1)
INFO=`basename "$SEL"`

#
#encoder settings - Encoder Einstellungen
#
function settings {
SET=$(zenity --list --width=600 --height=400 --title=$TITLE \
--window-icon="$ICON" \
--text $"<b>Audio File:</b> <i>'$INFO'</i>\n\nSelect a option to use." \
--hide-column "1" --column $"Settings" --column $"Information" \
Profiles_MP3_96kb      "MP3  96KB Stereo 44100Hz Low" \
Profiles_MP3_128kb     "MP3 128KB Stereo 44100Hz Default" \
Profiles_MP3_160kb     "MP3 160KB Stereo 44100Hz Good" \
Profiles_MP3_192kb     "MP3 192KB Stereo 44100Hz High" \
Profiles_MP3_224kb	   "MP3 224KB Stereo 44100Hz High+" \
Profiles_MP3_256kb	   "MP3 256KB Stereo 44100Hz High++" \
Profiles_MP3_320kb	   "MP3 320KB Stereo 44100Hz Best" \
Profiles_Wave          "Wave Stereo 44100Hz" \
Profiles_Flac		   "Flac Stereo 44100Hz" \
Profiles_OGG_0_Low     "OGG '0' Stereo 44100Hz (Low)" \
Profiles_OGG_1         "OGG '1' Stereo 44100Hz" \
Profiles_OGG_2         "OGG '2' Stereo 44100Hz" \
Profiles_OGG_3         "OGG '3' Stereo 44100Hz" \
Profiles_OGG_4         "OGG '4' Stereo 44100Hz" \
Profiles_OGG_5         "OGG '5' Stereo 44100Hz" \
Profiles_OGG_6_Best    "OGG '6' Stereo 44100Hz (Best)" \
Profiles_WMA_128kb     "WMA 128KB Stereo 44100Hz" \
Profiles_WMA_160kb     "WMA 160KB Stereo 44100Hz" \
Profiles_WMA_192kb     "WMA 192KB Stereo 44100Hz" \
Profiles_AAC_AUDIO_112kb   "AAC 112KB Stereo 44100Hz" \
Profiles_AAC_AUDIO_128kb   "AAC 128KB Stereo 44100Hz" \
Profiles_AAC_AUDIO_152kb   "AAC 152KB Stereo 44100Hz" \
)
if [ -z "$SET" ];then
	zenity --title "$TITLE" --info --window-icon=$ICON \
	--text $"No selection, program will now exit!" && exit 0
elif [ "$SET" = Profiles_MP3_96kb ];then
	OPT="-acodec libmp3lame -ab 96k -ac 2 -ar 44100"
	EXT="mp3"
elif [ "$SET" = Profiles_MP3_128kb ];then
	OPT="-acodec libmp3lame -ab 128k -ac 2 -ar 44100"
	EXT="mp3"
elif [ "$SET" = Profiles_MP3_160kb ];then
	OPT="-acodec libmp3lame -ab 160k -ac 2 -ar 44100"
	EXT="mp3"
elif [ "$SET" = Profiles_MP3_192kb ];then
	OPT="-acodec libmp3lame -ab 192k -ac 2 -ar 44100"
	EXT="mp3"
elif [ "$SET" = Profiles_MP3_224kb ];then
	OPT="-acodec libmp3lame -ab 224k -ac 2 -ar 44100"
	EXT="mp3"
elif [ "$SET" = Profiles_MP3_256kb ];then
	OPT="-acodec libmp3lame -ab 256k -ac 2 -ar 44100"
	EXT="mp3"
elif [ "$SET" = Profiles_MP3_320kb ];then
	OPT="-acodec libmp3lame -ab 320k -ac 2 -ar 44100"
	EXT="mp3"
elif [ "$SET" = Profiles_Wave ];then
	OPT="-vn -ar 44100"
	EXT="wav"
elif [ "$SET" = Profiles_Flac ];then
	OPT="-acodec flac -ar 44100 -ac 2"
	EXT="flac"	
elif [ "$SET" = Profiles_OGG_0_Low ];then
	OPT="-acodec libvorbis -aq 0 -vn -ac 2"
	EXT="ogg"
elif [ "$SET" = Profiles_OGG_1 ];then
	OPT="-acodec libvorbis -aq 1 -vn -ac 2"
	EXT="ogg"
elif [ "$SET" = Profiles_OGG_2 ];then
	OPT="-acodec libvorbis -aq 2 -vn -ac 2"
	EXT="ogg"
elif [ "$SET" = Profiles_OGG_3 ];then
	OPT="-acodec libvorbis -aq 3 -vn -ac 2"
	EXT="ogg"
elif [ "$SET" = Profiles_OGG_4 ];then
	OPT="-acodec libvorbis -aq 4 -vn -ac 2"
	EXT="ogg"
elif [ "$SET" = Profiles_OGG_5 ];then
	OPT="-acodec libvorbis -aq 5 -vn -ac 2"
	EXT="ogg"			
elif [ "$SET" = Profiles_OGG_6_Best ];then
	OPT="-acodec libvorbis -aq 6 -vn -ac 2"
	EXT="ogg"
elif [ "$SET" = Profiles_WMA_128kb ];then
	OPT="-vn  -acodec wmav2 -ab 128k"
	EXT="wma"
elif [ "$SET" = Profiles_WMA_160kb ];then
	OPT="-vn  -acodec wmav2 -ab 160k"
	EXT="wma"
elif [ "$SET" = Profiles_WMA_192kb ];then
	OPT="-vn  -acodec wmav2 -ab 192k"
	EXT="wma"
elif [ "$SET" = Profiles_AAC_AUDIO_112kb ];then
	OPT="-vn -acodec libfaac -ab 112k -ac 2"
	EXT="aac"
elif [ "$SET" = Profiles_AAC_AUDIO_128kb ];then
	OPT="-vn -acodec libfaac -ab 128k -ac 2"
	EXT="aac"
elif [ "$SET" = Profiles_AAC_AUDIO_152kb ];then
	OPT="-vn -acodec libfaac -ab 152k -ac 2"
	EXT="aac"		
fi

}
settings
#
#only basename - Nur Basisname
#
ASK=`basename "$SEL.$EXT"`
#
#file save - Datei speichern
#
SAVE=`zenity --file-selection --save --filename="$SEL.$EXT" \
--confirm-overwrite=$"$ASK.$EXT exist, you want to overwrite?" \
--file-filter="*.$EXT" --title=$TITLE --window-icon=$ICON`
if [ "$SAVE" = "" ]; then
		exit
fi
#
#convert file - Datei konvertieren
#
(echo && ffmpeg -y -i "$OPEN""$OPEN1" $OPT "$SAVE") | zenity --progress --pulsate \
--auto-close --width=300 --title "$TITLE" --window-icon=$ICON \
--text=$"<b>Convert</b>\n$OPEN$OPEN1 \n<b>to</b> \n'$SAVE' \n<b>please wait...</b>\n\n<span color='red'><b>Don't disturb!</b></span>" 
#
#job done - Arbeit beendet
#
if [ -f "$SAVE" ]; then
	zenity --info --no-wrap --window-icon=$ICON --title="$TITLE" \
	--text=$"<b>Finish with converting!</b>\n\nThank you for using PCLinuxOS." 
	else
	zenity --info --no-wrap --window-icon=$ICON --title="$TITLE" \
	--text=$"<b>Something went wrong, check You settings.</b>"
fi	
exit
