Download a whole youtube playlist to your computer in php.

I don’t know if I should be sharing this code snippet, but this is just too cool!

  1. Get the code from the links on this page:
    http://1chris.com/post/39/using-php-to-download-youtube-flvs/
  2. Append this hack at the bottom of the php code:
$watch_ids	=	array();
$url = "http://www.youtube.com/watch?v=%s";
$plyst	=	file_get_contents("http://www.youtube.com/playlist?list=PLFE3074A4CB751B2B");
$matchall	=	preg_match_all('/<li class="playlist-video-item[^>]+>([\s\S]*?)<\/li>/', $plyst, $matches);

$info		=	preg_match_all('/<span class="title video-title "[^>]+>([^<]+)/', $plyst, $desc);

#var_dump($desc, $matches);exit;
for($i = 0; $i < $matchall ; $i++)
{
  preg_match('/watch\?v=([a-zA-Z0-9]+)/', $matches[1][$i], $id);
  $id	=	$id[1];

  $itemurl	=	sprintf($url, $id);
  $itemdesc	=	preg_replace('/[^A-Za-z0-9\s\.]+/', '', $desc[1][$i]);

  $filename	=	"$itemdesc.flv";
  if(file_exists($filename)) continue;

  echo "Fetching: " , $itemdesc, "\n";
  $youtubegrabber = new youtubegrabber($itemurl, $filename, 0);
}

3. modify and enjoy

Bonus: Add this to the options in the get_curl_binary method to get download progress info.

        curl_setopt($ch, CURLOPT_NOPROGRESS, FALSE);

Adios!

  • Tosin

    Hello
    Link in step one not working