ytclass.php 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. <?php
  2. class YTDownloader {
  3. private $cache_dir;
  4. private $cookie_dir;
  5. private $itag_info = array(
  6. // Full Video
  7. 5 => "FLV[400x240]",
  8. 6 => "FLV[450x270]",
  9. 17 => "3GP[176x144]",
  10. 18 => "MP4[640x360]",
  11. 22 => "HD MP4[1280x720]",
  12. 34 => "FLV[640x360]",
  13. 35 => "FLV[854x480]",
  14. 36 => "3GP[320x180]",
  15. 37 => "MP4[1920x1080]",
  16. 38 => "MP4[4096x3072]",
  17. 43 => "WEBM[640x360]",
  18. 44 => "WEBM[854x480]",
  19. 45 => "WEBM[1280x720]",
  20. 46 => "WEBM[1920x1080]",
  21. 59 => "MP4[854x480]",
  22. 78 => "MP4[854x480]",
  23. // DASH videos
  24. 137 => "(Video Only) MP4[1920x1080]",
  25. 248 => "(Video Only) WEBM[1920x1080]",
  26. 136 => "(Video Only) MP4[1280x720]",
  27. 247 => "(Video Only) WEBM[1280x720]",
  28. 135 => "(Video Only) MP4[854x480]",
  29. 244 => "(Video Only) WEBM[854x480]",
  30. 134 => "(Video Only) MP4[640x360]",
  31. 243 => "(Video Only) WEBM[640x360]",
  32. 133 => "(Video Only) MP4[320x240]",
  33. 242 => "(Video Only) WEBM[320x240]",
  34. 160 => "(Video Only) MP4[176x144]",
  35. 278 => "(Video Only) WEBM[176x144]",
  36. // Dash Audios
  37. 140 => "(Audio Only) M4A[128Kbps]",
  38. 171 => "(Audio Only) WEBM[128Kbps]",
  39. 249 => "(Audio Only) WEBM[50Kbps]",
  40. 250 => "(Audio Only) WEBM[70Kbps]",
  41. 251 => "(Audio Only) WEBM[160Kbps]"
  42. );
  43. private $itag_ext = array(
  44. // Full Video
  45. 5 => ".flv",
  46. 6 => ".flv",
  47. 17 => ".3gp",
  48. 18 => ".mp4",
  49. 22 => ".mp4",
  50. 34 => ".flv",
  51. 35 => ".flv",
  52. 36 => ".3gp",
  53. 37 => ".mp4",
  54. 38 => ".mp4",
  55. 43 => ".webm",
  56. 44 => ".webm",
  57. 45 => ".webm",
  58. 46 => ".webm",
  59. 59 => ".mp4",
  60. 78 => ".mp4",
  61. // DASH videos
  62. 137 => ".mp4",
  63. 248 => ".webm",
  64. 136 => ".mp4",
  65. 247 => ".webm",
  66. 135 => ".mp4",
  67. 244 => ".webm",
  68. 134 => ".mp4",
  69. 243 => ".webm",
  70. 133 => ".mp4",
  71. 242 => ".webm",
  72. 160 => ".mp4",
  73. 278 => ".webm",
  74. // Dash Audios
  75. 140 => ".mp4",
  76. 171 => ".webm",
  77. 249 => ".webm",
  78. 250 => ".webm",
  79. 251 => ".webm"
  80. );
  81. function __construct(){
  82. $this->cache_dir = dirname(__FILE__).'/.cache';
  83. $this->cookie_dir = sys_get_temp_dir();
  84. if(!file_exists($this->cache_dir) && is_writeable(dirname(__FILE__))) {
  85. mkdir($this->cache_dir,0755);
  86. }
  87. }
  88. public function getDownloadLinks($id) {
  89. $returnData = FALSE;
  90. $videoID = $this->extractId($id);
  91. $webPage = $this->curlGet('https://www.youtube.com/watch?v='.$videoID);
  92. $sts = null;
  93. if(preg_match('|"sts":([0-9]{4,}),"|i', $webPage, $matches)) {
  94. $sts = $matches[1];
  95. }
  96. foreach(array('vevo', 'embedded', 'detailpage') as $elKey) {
  97. $query = http_build_query(array(
  98. 'c' => 'web',
  99. 'el' => $elKey,
  100. 'hl' => 'en_US',
  101. 'sts' => $sts,
  102. 'cver' => 'html5',
  103. 'eurl' => "https://youtube.googleapis.com/v/{$videoID}",
  104. 'html5' => '1',
  105. 'iframe' => '1',
  106. 'authuser' => '1',
  107. 'video_id' => $videoID,
  108. ));
  109. if($this->is_Ok($videoData = $this->curlGet("http://www.youtube.com/get_video_info?{$query}"))) {
  110. parse_str($videoData, $videoData);
  111. break;
  112. }
  113. }
  114. if(isset($videoData['status']) && $videoData['status'] !== 'fail') {
  115. $playerData = json_decode($videoData["player_response"]);
  116. $captions = array();
  117. for($i=0;$i<count($playerData->captions->playerCaptionsTracklistRenderer->captionTracks);$i++) {
  118. $caption = array();
  119. $caption["title"] = $playerData->captions->playerCaptionsTracklistRenderer->captionTracks[$i]->name->simpleText;
  120. $caption["lang"] = $playerData->captions->playerCaptionsTracklistRenderer->captionTracks[$i]->languageCode;
  121. $caption["url"] = $playerData->captions->playerCaptionsTracklistRenderer->captionTracks[$i]->baseUrl;
  122. array_push($captions,$caption);
  123. }
  124. $thumbinfo = $playerData->storyboards->playerStoryboardSpecRenderer->spec;
  125. $thumbparts = explode("|",$thumbinfo);
  126. $thumbnum = count($thumbparts)-1;
  127. $thumbdata = explode("#",$thumbparts[$thumbnum]);
  128. $vInfo['Title'] = $videoData['title'];
  129. $vInfo['ChannelName'] = $videoData['author'];
  130. $vInfo['ChannelId'] = $videoData['ucid'];
  131. $vInfo['Thumbnail'] = str_replace('default', 'maxresdefault', $videoData['thumbnail_url']);
  132. $vInfo['Duration'] = $videoData['length_seconds'];
  133. $vInfo['Rating'] = $playerData->videoDetails->averageRating;
  134. $vInfo['Captions'] = $captions;
  135. $vInfo['Thumbs'] = array();
  136. $vInfo['Thumbs']["src"] = "ytthumbs.php?data=".urlencode($thumbinfo);
  137. $vInfo['Thumbs']["width"] = $thumbdata[0]*$thumbdata[3];
  138. $vInfo['Thumbs']["height"] = $thumbdata[1]*ceil($thumbdata[2]/$thumbdata[3]);
  139. $vInfo['Thumbs']["fwidth"] = $thumbdata[0];
  140. $vInfo['Thumbs']["fheight"] = $thumbdata[1];
  141. $vInfo['Thumbs']["fcount"] = $thumbdata[2];
  142. $vInfo['Thumbs']["row"] = $thumbdata[3];
  143. }
  144. if (isset($videoData['url_encoded_fmt_stream_map']) && isset($videoData['adaptive_fmts'])) {
  145. $draft1 = explode(',',$videoData['url_encoded_fmt_stream_map']);
  146. $draft2 = explode(',',$videoData['adaptive_fmts']);
  147. foreach ($draft1 as $key) {
  148. $draftLink[] = $key;
  149. }
  150. foreach ($draft2 as $key) {
  151. $draftLink[] = $key;
  152. }
  153. foreach($draftLink as $dlink) {
  154. parse_str($dlink,$mLink[]);
  155. }
  156. if (isset($mLink[0]['s'])) {
  157. $instructions = $this->get_instructions($webPage);
  158. }
  159. foreach($mLink as $linker) {
  160. if(isset($linker['s'])) {
  161. $linkData[] = array(
  162. 'url' => preg_replace('@(https\:\/\/)[^\.]+(\.googlevideo\.com)@', 'https://redirector$2', $linker['url']).'&signature='.$this->sig_decipher($linker['s'], $instructions).'&title='.$this->clean_name($videoData['title']),
  163. 'itag' => $linker['itag'],
  164. 'type' => isset($this->itag_info[$linker['itag']]) ? $this->itag_info[$linker['itag']] : 'Unknown'
  165. );
  166. } else {
  167. $linkData[] = array(
  168. 'url' => preg_replace('@(https\:\/\/)[^\.]+(\.googlevideo\.com)@', 'https://redirector$2', $linker['url']).'&title='.$this->clean_name($videoData['title']),
  169. 'itag' => $linker['itag'],
  170. 'type' => isset($this->itag_info[$linker['itag']]) ? $this->itag_info[$linker['itag']] : 'Unknown'
  171. );
  172. }
  173. }
  174. }
  175. if (!empty($vInfo)) {
  176. $returnData['info'] = $vInfo;
  177. }if (!empty($linkData)) {
  178. $returnData['dl'] = $linkData;
  179. }
  180. return $returnData;
  181. }
  182. protected function curlGet($url) {
  183. if(in_array('curl', get_loaded_extensions())){
  184. $appSettings = parse_ini_file('../config/config.ini',true);
  185. $ch = curl_init($url);
  186. curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (X11; Linux x86_64; rv:62.0) Gecko/20100101 Firefox/62.0');
  187. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  188. curl_setopt($ch, CURLOPT_HEADER, 0);
  189. //curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
  190. if($appSettings["Proxy"]["type"]) {
  191. curl_setopt($ch, CURLOPT_PROXY, $appSettings["Proxy"]["type"]."://".$appSettings["Proxy"]["domain"].":".$appSettings["Proxy"]["port"]);
  192. curl_setopt($ch, CURLOPT_PROXYUSERPWD, $appSettings["Proxy"]["username"].":".$appSettings["Proxy"]["password"]);
  193. }
  194. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  195. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  196. $result = curl_exec($ch);
  197. curl_close($ch);
  198. return $result;
  199. }
  200. return FALSE;
  201. }
  202. private function is_Ok($var) {
  203. if(!preg_match('|status=fail|i',$var)) {
  204. return true;
  205. }
  206. }
  207. private function extractId($str) {
  208. if(preg_match('/[a-z0-9_-]{11}/i', $str, $matches)){
  209. return $matches[0];
  210. }
  211. return FALSE;
  212. }
  213. private function get_instructions($html) {
  214. $playerPattern = '/"assets":.+?"js":\s*("[^"]+")/';
  215. if(preg_match($playerPattern, $html, $matches) && is_string($_player = json_decode($matches[1])) && strlen($_player) >= 1) {
  216. $playerLink = substr($_player, 0, 2) == '//' ? "https:{$_player}" : "https://www.youtube.com{$_player}";
  217. $cache_player = $this->cache_dir.'/.ht-'.md5($_player);
  218. if(file_exists($cache_player)) {
  219. return unserialize(file_get_contents($cache_player));
  220. } else {
  221. $js_code = $this->curlGet($playerLink);
  222. $instructions = $this->sig_js_decode($js_code);
  223. if($instructions){
  224. if(file_exists($this->cache_dir) && is_writeable($this->cache_dir))
  225. file_put_contents($cache_player, serialize($instructions));
  226. return $instructions;
  227. }
  228. }
  229. }
  230. return false;
  231. }
  232. private function clean_name($name) {
  233. $special_chars = array(".","?", "[", "]", "/", "\\", "=", "<", ">", ":", ";", ",", "'", "\"", "&", "$", "#", "*", "(", ")", "|", "~", "`", "!", "{", "}", "%", "+", chr(0));
  234. $filename = str_replace($special_chars,' ',$name);
  235. $filename = preg_replace( "#\x{00a0}#siu", ' ', $filename );
  236. $filename = str_replace( array( '%20', '+', ' '), '-', $filename );
  237. $filename = preg_replace( '/[\r\n\t -]+/', '-', $filename );
  238. $filename = trim( $filename, '.-_' );
  239. return $filename;
  240. }
  241. private function sig_decipher($signature, $instructions) {
  242. foreach($instructions as $opt){
  243. $command = $opt[0];
  244. $value = $opt[1];
  245. if($command == 'swap'){
  246. $temp = $signature[0];
  247. $signature[0] = $signature[$value % strlen($signature)];
  248. $signature[$value] = $temp;
  249. } elseif($command == 'splice'){
  250. $signature = substr($signature, $value);
  251. } elseif($command == 'reverse'){
  252. $signature = strrev($signature);
  253. }
  254. }
  255. return trim($signature);
  256. }
  257. private function sig_js_decode($file){
  258. $script = $this->getBetween($file, 'a=a.split("");', ';return a.join("")');
  259. $script = str_replace(array("a,","\n"), array(',',''), $script);
  260. $script2= $this->getBetween($file, 'var ' . substr($script, 0, 2).'={', '};');
  261. $script2= str_replace('a,b', 'a', $script2);
  262. $script = str_replace(substr($script, 0, 2).'.', '', $script);
  263. $script = str_replace('(', '', $script);
  264. $script = str_replace(')', '', $script);
  265. $script_ex= explode(";", $script);
  266. $script2_ex = explode("\n", $script2);
  267. for($i = 0; $i < count($script2_ex); $i++) {
  268. $tmp = isset($script2_ex[$i]) ? explode(':', $script2_ex[$i]) : [];
  269. $n = isset($tmp[0]) ? $tmp[0] : '';
  270. $m = isset($tmp[1]) ? $tmp[1] : '';
  271. $tempS[$n] = $m;
  272. }
  273. for($i = 0; $i < count($script_ex); $i++) {
  274. $tmp = isset($script_ex[$i]) ? explode(',', $script_ex[$i]) : [];
  275. $a = isset($tmp[0]) ? $tmp[0] : '';
  276. $b = isset($tmp[1]) ? $tmp[1] : '';
  277. $deKey[] = $this->createCommad($a, $b, $tempS);
  278. }
  279. return $deKey;
  280. }
  281. private function createCommad($value, $num, $source) {
  282. $result = '';
  283. if (isset($source[$value]) && mb_strpos($source[$value], 'reverse')) {
  284. $result = array('reverse', '');
  285. } elseif (isset($source[$value]) && mb_strpos($source[$value], 'a.splice')) {
  286. $result = array('splice', $num);
  287. } else {
  288. $result = array('swap', $num);
  289. }
  290. return $result;
  291. }
  292. private function getBetween($content, $start, $end) {
  293. $r = explode($start, $content);
  294. if (isset($r[1])) {
  295. $r = explode($end, $r[1]);
  296. return $r[0];
  297. }
  298. return '';
  299. }
  300. }