1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Code to download youtube videos

Discussion in 'PHP' started by scriptjerk, Jul 13, 2013.

  1. #1
    Here is the code i created to download youtube videos.

    PHP:
    1.  
    2. /**
    3.  * Page Scrapper
    4.  * @returns string
    5.  */
    6. function curl_content($url){
    7.    $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)';
    8.    $ch = curl_init();
    9.    curl_setopt($ch, CURLOPT_USERAGENT, $useragent);
    10.    curl_setopt($ch, CURLOPT_URL, $url);
    11.    curl_setopt($ch, CURLOPT_HEADER, 0);
    12.    ob_start();
    13.    curl_exec($ch);
    14.    curl_close($ch);
    15.    $string = ob_get_contents();
    16.    ob_end_clean();        
    17.    return $string;    
    18. }
    19.  
    20.  
    21. /**
    22. * Youtube Video download
    23. * Available Known ITAG's:
    24. * ----------------------------------------------
    25. * itag=5 ( Very Low Definition FLV )
    26. * itag=17 ( Very Low Definition 3GP )
    27. * itag=18 ( Low Definition MP4 )
    28. * itag=22 ( High Definition MP4 )
    29. * itag=34 ( Low Definition FLV )
    30. * itag=35 ( Standard Definition FLV )
    31. * itag=36 ( Low Definition 3GP )
    32. * itag=37 ( Full High Definition MP4 )
    33. * itag=38 ( Ultra High Definition MP4 )
    34. * itag=43 ( Low Definition WebM )
    35. * itag=44 ( Standard Definition WebM )
    36. * itag=45 ( High Definition WebM )
    37. * itag=46 ( Full High Definition WebM )
    38. * itag=82 ( Low Definition 3D MP4 )
    39. * itag=83 ( Standard Definition 3D MP4 )
    40. * itag=84 ( High Definition 3D MP4 )
    41. * itag=85 ( Full High Definition 3D MP4)
    42. * itag=100 ( Low Definition 3D WebM )
    43. * itag=101 ( Standard Definition 3D WebM )
    44. * itag=102 ( High Definition 3D WebM )
    45. * -----------------------------------------------
    46. */  
    47. function youtube_video($url){
    48.  
    49.     $_locale = 'US';
    50.     $pref = 'mp4';
    51.     $qual = 'medium';
    52.    
    53.     // Curl Get Page
    54.     $data = curl_content($url);
    55.  
    56.    
    57.   $ftmd = array(
    58.       '5' => 'Very Low Definition FLV',
    59.       '17' => 'Very Low Definition 3GP',
    60.       '18' => 'Low Definition MP4',
    61.       '22' => 'High Definition MP4',
    62.       '34' => 'Low Definition FLV',
    63.       '35' => 'Standard Definition FLV',
    64.       '36' => 'Low Definition 3GP',
    65.       '37' => 'Full High Definition MP4',
    66.       '38' => 'Ultra High Definition MP4',
    67.       '43' => 'Low Definition WebM',
    68.       '44' => 'Standard Definition WebM',
    69.       '45' => 'High Definition WebM',
    70.       '46' => 'Full High Definition WebM',
    71.       '82' => 'Low Definition 3D MP4',
    72.       '83' => 'Standard Definition 3D MP4',
    73.       '84' => 'High Definition 3D MP4',
    74.       '85' => 'Full High Definition 3D MP4',
    75.       '100' => 'Low Definition 3D WebM',
    76.       '101' => 'Standard Definition 3D WebM',
    77.       '102' => 'High Definition 3D WebM'
    78.   );  
    79.    
    80.  
    81.     // If Valid Link Data Found
    82.     if(preg_match('/(fmt_stream_map=)(.*?)(")/i',$data,$matches)){
    83.     // Core Data
    84.     $link = $matches[2];
    85.   }else{
    86.     if(preg_match('/(fmt_stream_map"\: ")(.*?)(")/i',$data,$matches)){
    87.       // Core Data
    88.       $link = $matches[2];
    89.     }
    90.   }
    91.   if($link != ''){
    92.  
    93.     // Split Link Entires
    94.     if(strpos($link,',') !== false){
    95.       $split = explode(',',$link);
    96.     }else{
    97.       $split = explode('%2C',$link);
    98.     }  
    99.     // Define Arrays
    100.     $mp4_links = array();
    101.     $flv_links = array();
    102.     $ukn_links = array();
    103.    
    104.     // Loop Link Entries
    105.     foreach($split as $vlink){
    106.  
    107.       // Clear Values
    108.       $_url = '';
    109.       $_sig = '';
    110.       $_type = '';
    111.       $_quality = '';
    112.       $_itag = '';
    113.  
    114.      
    115.       // Split into Parts
    116.       if(strpos($vlink,',') !== false){
    117.         $part = explode(',',$vlink);
    118.       }else{
    119.         $vlink = urldecode($vlink);
    120.         $part = explode('&',$vlink);
    121.       }
    122.      
    123.       // Loop Parts
    124.       foreach($part as $vpart){
    125.    
    126.         // Key => Value Pair  
    127.         $arr = explode('=',$vpart);
    128.      
    129.         // Get all the parts
    130.         if($arr[0] == 'url'){
    131.           $_url = trim($arr[1]);
    132.         }
    133.         if($arr[0] == 'sig'){
    134.           $_sig = trim($arr[1]);
    135.         }
    136.         if($arr[0] == 'type'){
    137.           $_type = trim($arr[1]);
    138.         }
    139.         if($arr[0] == 'quality'){
    140.           $_quality = trim($arr[1]);
    141.         }
    142.         if($arr[0] == 'itag'){
    143.           $_itag = trim($arr[1]);
    144.         }
    145.        
    146.       }  
    147.            
    148.       $entry = $_url.'&signature='.$_sig;
    149.       $link = urldecode($entry);
    150.      
    151.       /*
    152.       echo "_url = ".$_url."<br/>";
    153.       echo "_sig = ".$_sig."<br/>";
    154.       echo "_type = ".$_type."<br/>";
    155.       echo "_quality = ".$_quality."<br/>";
    156.       echo "_itag = ".$_itag."<br/>";
    157.       echo "Final URL = ".$link."<br/><br/>";
    158.       */
    159.                                
    160.       // Group videos by extension
    161.       if($_itag == '85' || $_itag == '84' || $_itag == '83' || $_itag == '82' || $_itag == '38' || $_itag == '37' || $_itag == '22' || $_itag == '18'){
    162.         $mp4_links[] = $link;
    163.         $mp4_itags[] = $_itag;
    164.         $all[$_itag] = $link;
    165.       }elseif($_itag == '35' || $_itag == '34' || $_itag == '5'){
    166.         $flv_links[] = $link;
    167.         $flv_itags[] = $_itag;
    168.         $all[$_itag] = $link;
    169.       }elseif($_itag == '102' || $_itag == '101' || $_itag == '100' || $_itag == '46' || $_itag == '45' || $_itag == '44' || $_itag == '43'){
    170.         $webm_links[] = $link;
    171.         $webm_itags[] = $_itag;
    172.         $all[$_itag] = $link;
    173.       }elseif($_itag == '17'){
    174.         $gp_links[] = $link;
    175.         $gp_itags[] = $_itag;
    176.         $all[$_itag] = $link;
    177.       }else{
    178.         $ukn_links[] = $link;
    179.         $ukn_itags[] = $_itag;
    180.       }
    181.                    
    182.     }// <<-- End Foreach Loop
    183.  
    184.     // Find a Preferred Video  
    185.     if($pref == 'mp4' && $qual == 'high'){
    186.       $data = $mp4_links[0];
    187.     }elseif($pref == 'mp4' && $qual == 'medium'){
    188.       $data = (isset($mp4_links[1])) ? $mp4_links[1] : $mp4_links[0];
    189.     }elseif($pref == 'mp4' && $qual == 'low'){
    190.       $data = end($mp4_links);
    191.     }elseif($pref == 'flv' && $qual == 'high' || ($data == '')){
    192.       $data = $flv_links[0];
    193.     }elseif($pref == 'flv' && $qual == 'medium'){
    194.       $data = (isset($flv_links[1])) ? $flv_links[1] : $flv_links[0];
    195.     }elseif($pref == 'flv' && $qual == 'low'){
    196.       $data = end($flv_links);
    197.     }
    198.  
    199.     if(strlen($data)<125){
    200.       $data = $flv_links[0];
    201.     }
    202.    
    203.     // Assign Simple
    204.     $flv_url = $data;
    205.  
    206.     // Output some data
    207.     $arr['flvs'] = $flv_links;
    208.     $arr['flvt'] = $flv_itags;
    209.     $arr['mp4s'] = $mp4_links;
    210.     $arr['mp4t'] = $mp4_itags;
    211.     $arr['webms'] = $webm_links;
    212.     $arr['webmt'] = $webm_itags;
    213.     $arr['gps'] = $gp_links;
    214.     $arr['gpt'] = $gp_itags;
    215.     $arr['ukns'] = $ukn_links;
    216.     $arr['uknt'] = $ukn_itags;
    217.     $arr['cdebug'] = $cdebug;
    218.     $arr['link'] = $flv_url;
    219.     $arr['ext'] = $pref;
    220.     $arr['all'] = $all;
    221.  
    222.     return $arr;
    223.        
    224.     }else{
    225.     if(eregi('verify-age-details',$data)){
    226.       //echo 'Age verification needed<br>';
    227.     }
    228.     return(false);
    229.     }
    230. }      
    231.  
    232. $data = youtube_video('https://www.youtube.com/watch?v=bAsA00-5KoI');
    233.  
    234. // You now have available many formats to choose from.
    235. // You can see how to edit the code to be able to pass in desired quality, this is just to get started.
    236. $download_link = $data['mp4s'][0];
    237.  
    238.  
    239.  
    scriptjerk, Jul 13, 2013 IP
    EmmanuelFlossie likes this.
  2. Slincon

    Slincon Well-Known Member

    Messages:
    1,257
    Likes Received:
    38
    Best Answers:
    0
    Trophy Points:
    130
    #2
    Thanks for sharing - I'll give it a try!
    Slincon, Jul 13, 2013 IP
  3. scriptjerk

    scriptjerk Member

    Messages:
    34
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    33
    #3
    Here is a way to list all available formats for download in one shot.

    PHP:
    1. $ftmd = array(
    2.     '5' => 'Very Low Definition FLV',
    3.     '17' => 'Very Low Definition 3GP',
    4.     '18' => 'Low Definition MP4',
    5.     '22' => 'High Definition MP4',
    6.     '34' => 'Low Definition FLV',
    7.     '35' => 'Standard Definition FLV',
    8.     '36' => 'Low Definition 3GP',
    9.     '37' => 'Full High Definition MP4',
    10.     '38' => 'Ultra High Definition MP4',
    11.     '43' => 'Low Definition WebM',
    12.     '44' => 'Standard Definition WebM',
    13.     '45' => 'High Definition WebM',
    14.     '46' => 'Full High Definition WebM',
    15.     '82' => 'Low Definition 3D MP4',
    16.     '83' => 'Standard Definition 3D MP4',
    17.     '84' => 'High Definition 3D MP4',
    18.     '85' => 'Full High Definition 3D MP4',
    19.     '100' => 'Low Definition 3D WebM',
    20.     '101' => 'Standard Definition 3D WebM',
    21.     '102' => 'High Definition 3D WebM'
    22. );
    23. foreach($data['all'] as $itag => $link){
    24.   $alink = "<a href='".$link."'>".$ftmd[$itag]."</a>";
    25.   echo $alink."<br/>";
    26. }
    scriptjerk, Jul 13, 2013 IP
  4. rhoula

    rhoula Active Member

    Messages:
    856
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    95
    #4
    Would you be able to paste the whole code here if you don't mind.

    Thank you
    rhoula, Jul 13, 2013 IP
  5. scriptjerk

    scriptjerk Member

    Messages:
    34
    Likes Received:
    4
    Best Answers:
    1
    Trophy Points:
    33
    #5
    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.
    scriptjerk, Jul 13, 2013 IP
  6. sabato

    sabato Member

    Messages:
    410
    Likes Received:
    6
    Best Answers:
    1
    Trophy Points:
    43
    #6
    Only none VEVO videos can be downloaded. Vevo videos use some sort of encryption
    sabato, Jul 14, 2013 IP