Here is the code i created to download youtube videos. /** * Page Scrapper * @returns string */ function curl_content($url){ $useragent = 'Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0; SLCC1; .NET CLR 2.0.50727; .NET CLR 3.0.04506; .NET CLR 1.1.4322; InfoPath.2; .NET CLR 3.5.21022)'; $ch = curl_init(); curl_setopt($ch, CURLOPT_USERAGENT, $useragent); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_HEADER, 0); ob_start(); curl_exec($ch); curl_close($ch); $string = ob_get_contents(); ob_end_clean(); return $string; } /** * Youtube Video download * Available Known ITAG's: * ---------------------------------------------- * itag=5 ( Very Low Definition FLV ) * itag=17 ( Very Low Definition 3GP ) * itag=18 ( Low Definition MP4 ) * itag=22 ( High Definition MP4 ) * itag=34 ( Low Definition FLV ) * itag=35 ( Standard Definition FLV ) * itag=36 ( Low Definition 3GP ) * itag=37 ( Full High Definition MP4 ) * itag=38 ( Ultra High Definition MP4 ) * itag=43 ( Low Definition WebM ) * itag=44 ( Standard Definition WebM ) * itag=45 ( High Definition WebM ) * itag=46 ( Full High Definition WebM ) * itag=82 ( Low Definition 3D MP4 ) * itag=83 ( Standard Definition 3D MP4 ) * itag=84 ( High Definition 3D MP4 ) * itag=85 ( Full High Definition 3D MP4) * itag=100 ( Low Definition 3D WebM ) * itag=101 ( Standard Definition 3D WebM ) * itag=102 ( High Definition 3D WebM ) * ----------------------------------------------- */ function youtube_video($url){ $_locale = 'US'; $pref = 'mp4'; $qual = 'medium'; // Curl Get Page $data = curl_content($url); $ftmd = array( '5' => 'Very Low Definition FLV', '17' => 'Very Low Definition 3GP', '18' => 'Low Definition MP4', '22' => 'High Definition MP4', '34' => 'Low Definition FLV', '35' => 'Standard Definition FLV', '36' => 'Low Definition 3GP', '37' => 'Full High Definition MP4', '38' => 'Ultra High Definition MP4', '43' => 'Low Definition WebM', '44' => 'Standard Definition WebM', '45' => 'High Definition WebM', '46' => 'Full High Definition WebM', '82' => 'Low Definition 3D MP4', '83' => 'Standard Definition 3D MP4', '84' => 'High Definition 3D MP4', '85' => 'Full High Definition 3D MP4', '100' => 'Low Definition 3D WebM', '101' => 'Standard Definition 3D WebM', '102' => 'High Definition 3D WebM' ); // If Valid Link Data Found if(preg_match('/(fmt_stream_map=)(.*?)(")/i',$data,$matches)){ // Core Data $link = $matches[2]; }else{ if(preg_match('/(fmt_stream_map"\: ")(.*?)(")/i',$data,$matches)){ // Core Data $link = $matches[2]; } } if($link != ''){ // Split Link Entires if(strpos($link,',') !== false){ $split = explode(',',$link); }else{ $split = explode('%2C',$link); } // Define Arrays $mp4_links = array(); $flv_links = array(); $ukn_links = array(); // Loop Link Entries foreach($split as $vlink){ // Clear Values $_url = ''; $_sig = ''; $_type = ''; $_quality = ''; $_itag = ''; // Split into Parts if(strpos($vlink,',') !== false){ $part = explode(',',$vlink); }else{ $vlink = urldecode($vlink); $part = explode('&',$vlink); } // Loop Parts foreach($part as $vpart){ // Key => Value Pair $arr = explode('=',$vpart); // Get all the parts if($arr[0] == 'url'){ $_url = trim($arr[1]); } if($arr[0] == 'sig'){ $_sig = trim($arr[1]); } if($arr[0] == 'type'){ $_type = trim($arr[1]); } if($arr[0] == 'quality'){ $_quality = trim($arr[1]); } if($arr[0] == 'itag'){ $_itag = trim($arr[1]); } } $entry = $_url.'&signature='.$_sig; $link = urldecode($entry); /* echo "_url = ".$_url."<br/>"; echo "_sig = ".$_sig."<br/>"; echo "_type = ".$_type."<br/>"; echo "_quality = ".$_quality."<br/>"; echo "_itag = ".$_itag."<br/>"; echo "Final URL = ".$link."<br/><br/>"; */ // Group videos by extension if($_itag == '85' || $_itag == '84' || $_itag == '83' || $_itag == '82' || $_itag == '38' || $_itag == '37' || $_itag == '22' || $_itag == '18'){ $mp4_links[] = $link; $mp4_itags[] = $_itag; $all[$_itag] = $link; }elseif($_itag == '35' || $_itag == '34' || $_itag == '5'){ $flv_links[] = $link; $flv_itags[] = $_itag; $all[$_itag] = $link; }elseif($_itag == '102' || $_itag == '101' || $_itag == '100' || $_itag == '46' || $_itag == '45' || $_itag == '44' || $_itag == '43'){ $webm_links[] = $link; $webm_itags[] = $_itag; $all[$_itag] = $link; }elseif($_itag == '17'){ $gp_links[] = $link; $gp_itags[] = $_itag; $all[$_itag] = $link; }else{ $ukn_links[] = $link; $ukn_itags[] = $_itag; } }// <<-- End Foreach Loop // Find a Preferred Video if($pref == 'mp4' && $qual == 'high'){ $data = $mp4_links[0]; }elseif($pref == 'mp4' && $qual == 'medium'){ $data = (isset($mp4_links[1])) ? $mp4_links[1] : $mp4_links[0]; }elseif($pref == 'mp4' && $qual == 'low'){ $data = end($mp4_links); }elseif($pref == 'flv' && $qual == 'high' || ($data == '')){ $data = $flv_links[0]; }elseif($pref == 'flv' && $qual == 'medium'){ $data = (isset($flv_links[1])) ? $flv_links[1] : $flv_links[0]; }elseif($pref == 'flv' && $qual == 'low'){ $data = end($flv_links); } if(strlen($data)<125){ $data = $flv_links[0]; } // Assign Simple $flv_url = $data; // Output some data $arr['flvs'] = $flv_links; $arr['flvt'] = $flv_itags; $arr['mp4s'] = $mp4_links; $arr['mp4t'] = $mp4_itags; $arr['webms'] = $webm_links; $arr['webmt'] = $webm_itags; $arr['gps'] = $gp_links; $arr['gpt'] = $gp_itags; $arr['ukns'] = $ukn_links; $arr['uknt'] = $ukn_itags; $arr['cdebug'] = $cdebug; $arr['link'] = $flv_url; $arr['ext'] = $pref; $arr['all'] = $all; return $arr; }else{ if(eregi('verify-age-details',$data)){ //echo 'Age verification needed<br>'; } return(false); } } $data = youtube_video('https://www.youtube.com/watch?v=bAsA00-5KoI'); // You now have available many formats to choose from. // You can see how to edit the code to be able to pass in desired quality, this is just to get started. $download_link = $data['mp4s'][0]; PHP:
Here is a way to list all available formats for download in one shot. $ftmd = array( '5' => 'Very Low Definition FLV', '17' => 'Very Low Definition 3GP', '18' => 'Low Definition MP4', '22' => 'High Definition MP4', '34' => 'Low Definition FLV', '35' => 'Standard Definition FLV', '36' => 'Low Definition 3GP', '37' => 'Full High Definition MP4', '38' => 'Ultra High Definition MP4', '43' => 'Low Definition WebM', '44' => 'Standard Definition WebM', '45' => 'High Definition WebM', '46' => 'Full High Definition WebM', '82' => 'Low Definition 3D MP4', '83' => 'Standard Definition 3D MP4', '84' => 'High Definition 3D MP4', '85' => 'Full High Definition 3D MP4', '100' => 'Low Definition 3D WebM', '101' => 'Standard Definition 3D WebM', '102' => 'High Definition 3D WebM' ); foreach($data['all'] as $itag => $link){ $alink = "<a href='".$link."'>".$ftmd[$itag]."</a>"; echo $alink."<br/>"; } PHP:
Its not really for downloading to local pc, unless you arre running it from a local webserver, but its more for a server to server download script for any website.