ZhuJinFeng

Get link Youtube

Nov 13th, 2015
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.00 KB | None | 0 0
  1. <?php
  2. Class YoutbeDownloader
  3. {
  4.     private static $endpoint = "http://www.youtube.com/get_video_info";
  5.     public static function getInstance()
  6.     {
  7.         static $instance = null;
  8.         if (null === $instance) {
  9.             $instance = new static();
  10.         }
  11.         return $instance;
  12.     }
  13.     public function getLink($id)
  14.     {
  15.         preg_match('#https://www.youtube.com/watch\?v=(.*)#', $id, $Match);
  16.         $API_URL                    = self::$endpoint . "?&video_id=" . $Match[1];
  17.         $video_info                 = $this->curlGet($API_URL);
  18.         $url_encoded_fmt_stream_map = '';
  19.         parse_str($video_info);
  20.         if (isset($reason)) {
  21.             return $reason;
  22.         }
  23.         if (isset($url_encoded_fmt_stream_map)) {
  24.             $my_formats_array = explode(',', $url_encoded_fmt_stream_map);
  25.         } else {
  26.             return 'No encoded format stream found.';
  27.         }
  28.         if (count($my_formats_array) == 0) {
  29.             return 'No format stream map found - was the video id correct?';
  30.         }
  31.         $avail_formats[] = '';
  32.         $i               = 0;
  33.         $ipbits          = $ip = $itag = $sig = $quality = $type = $url = '';
  34.         $expire          = time();
  35.         foreach ($my_formats_array as $format) {
  36.             parse_str($format);
  37.             $avail_formats[$i]['itag']    = $itag;
  38.             $avail_formats[$i]['quality'] = $quality;
  39.             $type                         = explode(';', $type);
  40.             $avail_formats[$i]['type']    = $type[0];
  41.             $avail_formats[$i]['url']     = urldecode($url) . '&signature=' . $sig;
  42.             parse_str(urldecode($url));
  43.             $avail_formats[$i]['expires'] = date("G:i:s T", $expire);
  44.             $avail_formats[$i]['ipbits']  = $ipbits;
  45.             $avail_formats[$i]['ip']      = $ip;
  46.             $i++;
  47.         }
  48.         if (is_string($avail_formats)) {
  49.             echo $avail_formats;
  50.         } else {
  51.             foreach ($avail_formats as $video) {
  52.                 if (strpos($video['url'], 'itag=22')) {
  53.                     $m22 = $video['url'];
  54.                 } elseif (strpos($video['url'], 'itag=18')) {
  55.                     $m18 = $video['url'];
  56.                 }
  57.             }
  58.             if (isset($m22, $m18)) {
  59.                 $js = '<source data-res="720" src="' . $m22 . '" type="video/mp4" />
  60.                        <source data-res="360" src="' . $m18 . '" type="video/mp4" />';
  61.             } elseif (isset($m18)) {
  62.                 $js = '<source data-res="360" src="' . $m18 . '" type="video/mp4" />';
  63.             } else {
  64.                 $js = 'Not support';
  65.             }
  66.             return $js;
  67.         }
  68.     }
  69.     function curlGet($URL)
  70.     {
  71.         $ch      = curl_init();
  72.         $timeout = 3;
  73.         curl_setopt($ch, CURLOPT_URL, $URL);
  74.         curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  75.         curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  76.         $tmp = curl_exec($ch);
  77.         curl_close($ch);
  78.         return $tmp;
  79.     }
  80. }
  81. ?>
Advertisement
Add Comment
Please, Sign In to add comment