Download YouTube Playlist

Print Friendly, PDF & Email

This script downloads all videos in your YouTube playlist . The script is extremely basic in nature and doesnot offer any choices.
Change the url in the below to reflect playlist of your choice.

Pseudo-algorithm as follows

  1. Download YouTube playlist page
  2. Parse the page for video names and their ids
  3. Store them in an array
  4. Iterate the array to download single video at a time
  5. Loop Start
  6. Download video page and grab session cookie
  7. Grab the download parameters
  8. Formulate download url
  9. Download the url and submit the session cookie
  10. Loop End
#download the url page
debug=`wget -O /tmp/utube $url 2>&1`
#extract titles from the downloaded page
temp1=$(sed -n -e 's/.*\(.*\)<\/span>.*/\1/p' /tmp/utube | sed -e 's/ /_/g' )
title=($(echo $temp1 | tr " " "\n"))
#extract unique video id from the playlists
temp2=$(cat /tmp/utube | grep -i "/watch?v" | grep -i "a href" | sed 's/.*&1`
	  sleep 2
	  # get host details from testfile
	  host_url=$(sed -n -e 's/.*img.src = "\(.*\)generate.*/\1/p' $ty_dir/testout.txt | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e)
      dl_param=$(sed -n -e 's/.*\/generate_204\(.*\)";.*/\1/p' $ty_dir/testout.txt | sed 's/\\u0026/\&/g' | sed -e's/%\([0-9A-F][0-9A-F]\)/\\\\\x\1/g' | xargs echo -e)
	  #devise download url
      #attempt download.. return the cookies with regards
      debug=`wget -c --cookies=on --load-cookies=$ty_dir/cookie.txt --keep-session-cookies --save-cookies=$ty_dir/cookie.txt -O $O_FILE $dl_url 2>&1`
	  rm $ty_dir/cookie.txt
	  rm $ty_dir/testout.txt
	  arch_size=$(stat -c %s  $O_FILE)	 
	  #delete file if file too small
	  if [[ $arch_size -lt "1024" ]]
		echo "....Failed.. FileSize too Small $arch_size bytes"
		rm $O_FILE
	  #on successful download
	  if [[ $arch_size -gt "1024" ]]
		echo "....Downloaded.. FileSize $arch_size bytes"

Ishan Karve
This entry was posted in BASH, Programming and tagged , , , , , , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *