Created
August 1, 2014 22:47
-
-
Save ConAntonakos/d64d8559280346f5dd19 to your computer and use it in GitHub Desktop.
Downloads YouTube playlists.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
$playlist_id= 'PLzJZ3ahfm9Q_VSVXmANa8s6v99t3uRkkL'; | |
$api_key = 'AIzaSyAL7YE_8oGFPPWJ6pgQVbAYF-G7tsbf15I'; | |
$base_url = 'https://www.googleapis.com/youtube/v3/'; | |
$playlist_items_url = 'playlistItems?part=contentDetails%2Csnippet&maxResults=50&playlistId='.$playlist_id.'&key='.$api_key; | |
$playlist_url = $base_url . 'playlists?part=contentDetails%2Csnippet&id='.$playlist_id.'&key='.$api_key; | |
$playlist_output = curl_get($playlist_url); | |
$json_playlist_info = json_decode($playlist_output, true); | |
$playlist_title = $json_playlist_info['items'][0]['snippet']['title']; | |
$playlist_title = preg_replace('/[\s]+/', '', $playlist_title); | |
$folder_title = '~/Movies/'.$playlist_title; | |
mkdir($folder_title); | |
$url = $base_url . $playlist_items_url; | |
$output = curl_get($url); | |
$json_youtube_videos = json_decode($output,true); | |
$videos = $json_youtube_videos['items']; | |
//print_r($json_youtube_videos); | |
while(isset($json_youtube_videos['nextPageToken'])){ | |
if(preg_match('/&pageToken=/', $url)){ | |
$exploded_url = explode('&', $url); | |
foreach($exploded_url as $i => $chunk){ | |
if(preg_match('/pageToken=/', $chunk)){ | |
$exploded_url[$i] = 'pageToken='.$json_youtube_videos['nextPageToken']; | |
$url = implode('&', $exploded_url); | |
} | |
} | |
} else { | |
$url = $url.'&pageToken='.$json_youtube_videos['nextPageToken']; | |
} | |
$output = curl_get($url); | |
$json_youtube_videos = json_decode($output,true); | |
$additional_videos = $json_youtube_videos['items']; | |
$videos = array_merge($videos, $additional_videos); | |
} | |
//print_r(count($videos)); | |
$video_template_url = 'http://www.youtube.com/watch?v='; | |
//$get_video_info = 'http://www.youtube.com/get_video_info?&video_id='; | |
foreach($videos as $i => $video){ | |
$video_id = $video['contentDetails']['videoId']; | |
$resolved_video_url = $video_template_url. $video_id; | |
$video_title = $video['snippet']['title']; | |
//echo $video_url .PHP_EOL.implode('', explode(' ', $video_title)).PHP_EOL.PHP_EOL; | |
echo "Downloading... ".$video_title.PHP_EOL; | |
$video_title = preg_replace('/[\s]+/', '', $video_title); | |
$output_file = $folder_title . '/'. $video_title; | |
exec("youtube-dl -o ".$output_file.".mp4 '".$resolved_video_url."'"); | |
} | |
function curl_get($URL) { | |
$ch = curl_init(); | |
$timeout = 3; | |
curl_setopt( $ch , CURLOPT_URL , $URL ); | |
curl_setopt( $ch , CURLOPT_RETURNTRANSFER , 1 ); | |
curl_setopt( $ch , CURLOPT_CONNECTTIMEOUT , $timeout ); | |
/* if you want to force to ipv6, uncomment the following line */ | |
//curl_setopt( $ch , CURLOPT_IPRESOLVE , 'CURLOPT_IPRESOLVE_V6'); | |
$tmp = curl_exec( $ch ); | |
curl_close( $ch ); | |
return $tmp; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment