#!/bin/bash
 
# this script is designed to take a url from from YouTube and rip the mp3 and avi from it
 
# Arron M Finnon - afinnon@gmail.com

DIRECTORY=/home/$USER/YouTemp

clear
echo ""
echo "Finux's YouTube Ripper, this script is designed to take a URL from YouTube.Com and Rip the Mp3 and the avi from it.  It will Move the files into the user's home folder. You Must Have YouTube-dl, ffmpeg, and Mplayer installed for it to function"

#
# Check whether youtube-dl, mplayer and ffmpeg is installed
#

if [ ! -x /usr/bin/youtube-dl  ];then
 echo "Error! youtube-dl isn't installed. This Sript will not work without it if your running a debian like system then a quick 'sudo aptitude install youtube-dl mplayer ffmpeg' should fix it if you have multimedia repos set up, or visit http://www.arrakis.es/~rggi3/youtube-dl/"

 exit 0
fi

if [ ! -x /usr/bin/mplayer  ];then
 echo "Error! mplayer isn't installed. This Sript will not work without it if your running a debian like system then a quick 'sudo aptitude install mplayer youtube-dl ffmpeg' should fix it if you have the multimedia repos set up, or visit http://www.mplayerhq.hu/design7/news.html"
 exit 0
fi

if [ ! -x /usr/bin/ffmpeg  ];then
 echo "Error! ffmpeg isn't installed. This Sript will not work without it if your running a debian like system then a quick 'sudo aptitude install ffmpeg youtube-dl mplayer' should fix it if you have the multimedia repos set up, or visit http://www.ffmpeg.org/"
 exit 0
fi

echo ""

echo "Please supply the YouTube url for the Rip"

read -e YOUTUBEURL

echo "Checking $DIRECTORY "


#
# Creating Youtemp folder for download, and convertion of files  
#


if [ -e $DIRECTORY ]
then
        echo "'$DIRECTORY' already exists"

else
        echo "Creating directory: '$DIRECTORY' . . ."
        mkdir $DIRECTORY

fi

cd $DIRECTORY


#
# Selecting Mp3 name for file  
#


echo "please supply a name for the Mp3"
read -e MP3NAME

youtube-dl $YOUTUBEURL


#
# Download and convert YouTube file  
#


for DOWNLOADEDFLV in *.flv
do
echo ".flv downloaded, convertion starting"


#
# ffmpeg and mplayer being used to make .avi and mp3  
#


ffmpeg -i $DOWNLOADEDFLV $MP3NAME.avi
mplayer -dumpaudio $DOWNLOADEDFLV -dumpfile $MP3NAME.mp3


#
# Checking downloaded .flv file  
#


echo "$MP3NAME has been converted to AVI and MP3"
echo "Removing .flv files"
rm $DOWNLOADEDFLV
done

#
# Checking Directory and moving .mp3 to /home/$USER/Music/Videos  
#

echo "Checking if Music directory exists"

if [ -e  /home/$USER/Music ]
then
        echo "'Music' folder already exists, you must have downloaded the Videos"
	echo "Moving " $MP3NAME.mp3 " to Music Folder"
	mv $DIRECTORY/*.mp3 /home/$USER/Music

else
        echo "Creating directory: 'Music' folder . . ."
        mkdir /home/$USER/Music
	echo "Moving " $MP3NAME.mp3 " to Music folder"
	mv $DIRECTORY/*.mp3 /home/$USER/Music

fi

#
# Checking Directory and moving .avi to /home/$USER/Music/Videos  
#

echo "Checking if Video/Music directory exists"

if [ -e /home/$USER/Music/Videos ]
then
        echo "'/Music/Video' already exists"
	echo "Moving " $MP3NAME.avi "to Videos.Music folder" 
	mv $MP3NAME.avi /home/$USER/Music/Videos

else
        echo "Creating directory: 'Music/Videos' . . ."
        mkdir /home/$USER/Music/Videos
	echo "Moving " $MP3NAME.avi " To Video/Nusic folder"
	mv $MP3NAME.avi /home/$USER/Music/videos

fi

#
# Cleaning up YouTemp Folder
#
echo "Clearing Up " $DIRECTORY "folder "
rm -r $DIRECTORY
echo $DIRECTORY "Deleted "

#
# Exit Script  
#

exit 0

