ÿØÿà JFIF ÿÛ „ ( %"1"%)+...383,7(-.-
![]() Server : Apache/2.4.6 (CentOS) OpenSSL/1.0.2k-fips PHP/7.4.20 System : Linux st2.domain.com 3.10.0-1127.10.1.el7.x86_64 #1 SMP Wed Jun 3 14:28:03 UTC 2020 x86_64 User : apache ( 48) PHP Version : 7.4.20 Disable Function : NONE Directory : /var/www/html/123vid/vendor/athlon1600/youtube-downloader/src/ |
<?php namespace YouTube; // YouTube is capitalized twice because that's how youtube itself does it: // https://developers.google.com/youtube/v3/code_samples/php class YouTubeDownloader { protected $client; /** @var string|null */ protected $error; function __construct() { $this->client = new Browser(); } public function getBrowser() { return $this->client; } public function getLastError() { return $this->error; } /** * Look for a player script URL. E.g: * <script src="//s.ytimg.com/yts/jsbin/player-fr_FR-vflHVjlC5/base.js" name="player/base"></script> * * @param $video_html * @return null|string */ public function getPlayerScriptUrl($video_html) { $player_url = null; // check what player version that video is using if (preg_match('@<script\s*src="([^"]+player[^"]+js)@', $video_html, $matches)) { $player_url = $matches[1]; // relative protocol? if (strpos($player_url, '//') === 0) { $player_url = 'http://' . substr($player_url, 2); } elseif (strpos($player_url, '/') === 0) { // relative path? $player_url = 'http://www.youtube.com' . $player_url; } } return $player_url; } public function getPlayerCode($player_url) { $contents = $this->client->getCached($player_url); return $contents; } // extract youtube video_id from any piece of text public function extractVideoId($str) { if (preg_match('/[a-z0-9_-]{11}/i', $str, $matches)) { return $matches[0]; } return false; } /** * @param array $links * @param string $selector mp4, 360, etc... * @return array */ private function selectFirst($links, $selector) { $result = array(); $formats = preg_split('/\s*,\s*/', $selector); // has to be in this order foreach ($formats as $f) { foreach ($links as $l) { if (stripos($l['format'], $f) !== false || $f == 'any') { $result[] = $l; } } } return $result; } public function getVideoInfo($url) { // $this->client->get("https://www.youtube.com/get_video_info?el=embedded&eurl=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3D" . urlencode($video_id) . "&video_id={$video_id}"); } public function getPageHtml($url) { $video_id = $this->extractVideoId($url); return $this->client->get("https://www.youtube.com/watch?v={$video_id}"); } public function getPlayerResponse($page_html) { if (preg_match('/player_response":"(.*?)","/', $page_html, $matches)) { $match = stripslashes($matches[1]); $ret = json_decode($match, true); return $ret; } return null; } public function parsePlayerResponse($player_response, $js_code) { $parser = new Parser(); try { $formats = array(); if (isset($player_response['streamingData']) && isset($player_response['streamingData']['formats'])) { $formats = $player_response['streamingData']['formats']; } $adaptiveFormats = array(); if (isset($player_response['streamingData']) && isset($player_response['streamingData']['adaptiveFormats'])) { $adaptiveFormats = $player_response['streamingData']['adaptiveFormats']; } if (!is_array($formats)) { $formats = array(); } if (!is_array($adaptiveFormats)) { $adaptiveFormats = array(); } $formats_combined = array_merge($formats, $adaptiveFormats); // final response $return = array(); foreach ($formats_combined as $item) { $cipher = ''; if (isset($item['cipher']) || isset($item['signatureCipher'])) { $cipher = isset($item['cipher']) ? $item['cipher'] : $item['signatureCipher']; } $itag = $item['itag']; // some videos do not need to be decrypted! if (isset($item['url'])) { $return[] = array( 'url' => $item['url'], 'itag' => $itag, 'format' => $parser->parseItagInfo($itag) ); continue; } parse_str($cipher, $result); $url = $result['url']; $sp = $result['sp']; // typically 'sig' $signature = $result['s']; $decoded_signature = (new SignatureDecoder())->decode($signature, $js_code); // redirector.googlevideo.com $return[] = array( 'url' => $url . '&' . $sp . '=' . $decoded_signature, 'itag' => $itag, 'format' => $parser->parseItagInfo($itag) ); } return $return; } catch (\Exception $exception) { // do nothing } catch (\Throwable $throwable) { // do nothing } return null; } public function getDownloadLinks($video_id, $selector = false) { $this->error = null; $page_html = $this->getPageHtml($video_id); if (strpos($page_html, 'We have been receiving a large volume of requests') !== false || strpos($page_html, 'systems have detected unusual traffic') !== false) { $this->error = 'HTTP 429: Too many requests.'; return array(); } // get JSON encoded parameters that appear on video pages $json = $this->getPlayerResponse($page_html); // get player.js location that holds signature function $url = $this->getPlayerScriptUrl($page_html); $js = $this->getPlayerCode($url); $result = $this->parsePlayerResponse($json, $js); // if error happens if (!is_array($result)) { return array(); } // do we want all links or just select few? if ($selector) { return $this->selectFirst($result, $selector); } return $result; } }