Jun 272012

Several people have asked me if I could create a playlist downloader similar to my single downloader.  I set out to do just that.

Here is the code

20120628 – added a check to validate if file already exists

# set -x
# By Ed Wiget
# This script automates downloading youtube video plylists and converting to mp3 file
# 20120625 - initial script based off of http://www.edwiget.name/2011/08/ge%E2%80%A0-%C2%A5d-%C2%A7hd%C2%A2k-dn/
# 20120628 - add a check to see if the name already exists and if so skips it
# you should do this sudo command and the svn rtmpdump commands below before running this script
# sudo apt-get install youtube-dl lame faad ffmpeg ffprobe
# for rtmp and librtmp do this:
# svn co svn://svn.mplayerhq.hu/rtmpdump rtmpdump
# cd rtmpdump/trunk
# make
# sudo make install
# set this variable to the location of your scripts:
# this sets the location of the python youtube-dl script, relative to above
# this sets the path of the downloaded files and temp directory
# this sets the path to were the final mp3 is stored
# this sets the location of the video file downloaded
# this checks for the youtube-dl script and if it doesn't exist, it gets it
# if it does exist, it checks to make sure its the latest version
if [ ! -d ${BASE_DIR}/youtube-dl ]; then
	echo "grabbing the youtube-dl script"
	cd ${BASE_DIR}
	git clone git://github.com/rg3/youtube-dl.git youtube-dl
	chmod 700 ${BASE_DIR}/youtube-dl/youtube-dl
	echo -e "youtube-dl already exists\n\nMaking sure we have the latest version"
	chmod 700 ${BASE_DIR}/youtube-dl/youtube-dl
# this checks to make sure we have ffmpeg and lame installed, and if not, grabs them
FFMPEG1=`which ffmpeg | wc -l`
LAME1=`which lame | wc -l`
if [ ${FFMPEG1} = 1 ]; then
	echo "ffmpeg already exists"
	echo "grabbing ffmpeg"
	# for ubuntu based distros, use this line
	sudo apt-get install ffmpeg
	# for redhat based distros, use this line
	#sudo yum install ffmpeg
if [ ${LAME1} = 1 ]; then
	echo "lame already exists"
	echo "grabbing lame"
	# for ubuntu use this line
	sudo apt-get install lame
	# for redhat use this line
	# sudo yum install lame
# next we ask the user for the video file, it should be in format like:
# http://www.youtube.com/playlist?list=PL4E71AC1BFE99D171
echo -e "What is the video playlist to download, ie. http://www.youtube.com/playlist?list=PL4E71AC1BFE99D171"
echo "You entered ${VIDEO_URL} is this correct? ( y / n )"
read ANS
	if [ ${ANS} = "y" ]; then
		wget -O /tmp/playlist.output ${VIDEO_URL} 
			for i in `cat /tmp/playlist.output | grep --color=auto -E 'watch\?v=' | cut -d '=' -f3 | sed 's/&list//g' | grep -iv "hid"`; do
				cd ${DL_FILES}
				# grab the song title
				SONG_TITLE=`${YOUTUBE_DL} --get-title ${URL}`
				echo -e "the song title is ${SONG_TITLE}"
                                # here we check if the song already exists, if so it skips it....
                                SONGCHK=`ls ${MP3_LOCATION}/"${SONG_TITLE}"* | wc -l`
                                if [ "${SONGCHK}" -ge "1" ]; then
                                        echo "song already exists....."
				# downloading video
				echo "downloading video....please wait"
				${YOUTUBE_DL} ${URL}
				# we need to convert the dl url to a filename for later processing
				# the url is like:  http://www.youtube.com/watch?v=6E2hYDIFDIU 
				# the downloaded file will be 6E2hYDIFDIU.mp4
				MP4_FILE=`echo ${URL} | awk -F/ '{print$4}' | awk -F= '{print$2}'`
				# get the downloaded file extension
				FILE_EXT=`ls ${DL_FILES}/${MP4_FILE}* | awk -F. '{print$2}'`
				echo "your video is located in ${DL_FILES}/${MP4_FILE}.${FILE_EXT}"
				echo ""
				echo "converting ${DL_FILES}/${MP4_FILE}.${FILE_EXT} to wav.....please wait"
				# ffmpeg -i 6E2hYDIFDIU.flv 6E2hYDIFDIU.wav
				ffmpeg -i ${DL_FILES}/${MP4_FILE}.${FILE_EXT} ${DL_FILES}/"${SONG_TITLE}".wav
				echo "video converted to wav file....converting wav to mp3"
				lame -b 128 ${DL_FILES}/"${SONG_TITLE}".wav ${MP3_LOCATION}/"${SONG_TITLE}".mp3
				echo "${SONG_TITLE} is now available at ${MP3_LOCATION}/${SONG_TITLE}.mp3"
				# added 20110813
				# check to see if video is mp4, if so, mv to Music folder.  If not, convert to mp4 then move.
					if [ "`echo ${FILE_EXT}`" = "mp4" ]; then
       					echo -e "\n\nmoving video download file ${DL_FILES}/${MP4_FILE}.${FILE_EXT} to ${VID_LOCATION}/${SONG_TITLE}.${FILE_EXT}"
       					mv ${DL_FILES}/${MP4_FILE}.${FILE_EXT} ${VID_LOCATION}/"${SONG_TITLE}".${FILE_EXT}
       					echo "video file is now at ${VID_LOCATION}/"${SONG_TITLE}".${FILE_EXT}"
						echo -e "converting ${DL_FILES}/${MP4_FILE}.${FILE_EXT} to ${DL_FILES}/${MP4_FILE}.mp4....please wait"
						ffmpeg -i ${DL_FILES}/${MP4_FILE}.${FILE_EXT} ${DL_FILES}/${MP4_FILE}.mp4
						mv ${DL_FILES}/${MP4_FILE}.mp4 ${VID_LOCATION}/"${SONG_TITLE}".mp4
						echo "video file is now at ${VID_LOCATION}/"${SONG_TITLE}".mp4"
                echo -e "\n\ndone .... and enjoy"
		echo "there was an error...."
echo "all done.....enjoy"

This site uses Akismet to reduce spam. Learn how your comment data is processed.