extract.php 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552
  1. <?php
  2. $id = $video_id;
  3. // Delete sig file if is old 60 sec
  4. if (is_file(TOOLS_PATH.'sig.php')) {
  5. $filelastmodified = filemtime(TOOLS_PATH.'sig.php');
  6. if((time() - $filelastmodified) > 60) {
  7. unlink(TOOLS_PATH.'sig.php');
  8. }
  9. }
  10. function getc($url) {
  11. $ch = curl_init($url);
  12. curl_setopt($ch, CURLOPT_ENCODING, 'gzip');
  13. curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
  14. curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
  15. curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  16. curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  17. curl_setopt($ch, CURLOPT_TIMEOUT, 10);
  18. $data = curl_exec($ch);
  19. curl_close($ch);
  20. return $data;
  21. }
  22. function cut_yt($ganti) {
  23. $arayganti = array('Youtube','youtube','YouTube');
  24. $ganti = str_replace($arayganti,'',$ganti);
  25. return $ganti;
  26. }
  27. function exformat($itag) {
  28. switch ($itag) {
  29. case "5":
  30. return "flv, video, 240p, audio";
  31. break;
  32. case "6":
  33. return "flv, video, 270p, audio";
  34. break;
  35. case "13":
  36. return "3gp, video, audio";
  37. break;
  38. case "17":
  39. return "3gp, video, 144p, audio";
  40. break;
  41. case "18":
  42. return "mp4, video, 360p, audio";
  43. break;
  44. case "22":
  45. return "mp4, video, 720p, audio";
  46. break;
  47. case "34":
  48. return "flv, video, 360p, audio";
  49. break;
  50. case "35":
  51. return "flv, video, 480p, audio";
  52. break;
  53. case "36":
  54. return "3gp, video, audio";
  55. break;
  56. case "37":
  57. return "mp4, video, 1080p, audio";
  58. break;
  59. case "38":
  60. return "mp4, video, 3072p, audio";
  61. break;
  62. case "43":
  63. return "webm, video, 360p, audio";
  64. break;
  65. case "44":
  66. return "webm, video, 480p, audio";
  67. break;
  68. case "45":
  69. return "webm, video, 720p, audio";
  70. break;
  71. case "46":
  72. return "webm, video, 1080p, audio";
  73. break;
  74. case "59":
  75. return "mp4, video, 480p, audio";
  76. break;
  77. case "78":
  78. return "mp4, video, 480p, audio";
  79. break;
  80. case "82":
  81. return "mp4, video, 360p, audio";
  82. break;
  83. case "83":
  84. return "mp4, video, 480p, audio";
  85. break;
  86. case "84":
  87. return "mp4, video, 720p, audio";
  88. break;
  89. case "85":
  90. return "mp4, video, 1080p, audio";
  91. break;
  92. case "100":
  93. return "webm, video, 360p, audio";
  94. break;
  95. case "101":
  96. return "webm, video, 480p, audio";
  97. break;
  98. case "102":
  99. return "webm, video, 720p, audio";
  100. break;
  101. case "91":
  102. return "mp4, video, 144p, audio";
  103. break;
  104. case "92":
  105. return "mp4, video, 240p, audio";
  106. break;
  107. case "93":
  108. return "mp4, video, 360p, audio";
  109. break;
  110. case "94":
  111. return "mp4, video, 480p, audio";
  112. break;
  113. case "95":
  114. return "mp4, video, 720p, audio";
  115. break;
  116. case "96":
  117. return "mp4, video, 1080p, audio";
  118. break;
  119. case "132":
  120. return "mp4, video, 240p, audio";
  121. break;
  122. case "151":
  123. return "mp4, video, 72p, audio";
  124. break;
  125. case "133":
  126. return "mp4, video, 240p";
  127. break;
  128. case "134":
  129. return "mp4, video, 360p";
  130. break;
  131. case "135":
  132. return "mp4, video, 480p";
  133. break;
  134. case "136":
  135. return "mp4, video, 720p";
  136. break;
  137. case "137":
  138. return "mp4, video, 1080p";
  139. break;
  140. case "138":
  141. return "mp4, video";
  142. break;
  143. case "160":
  144. return "mp4, video, 144p";
  145. break;
  146. case "212":
  147. return "mp4, video, 480p";
  148. break;
  149. case "264":
  150. return "mp4, video, 1440p";
  151. break;
  152. case "298":
  153. return "mp4, video, 720p";
  154. break;
  155. case "299":
  156. return "mp4, video, 1080p";
  157. break;
  158. case "266":
  159. return "mp4, video, 2160p";
  160. break;
  161. case "139":
  162. return "m4a, audio";
  163. break;
  164. case "140":
  165. return "m4a, audio";
  166. break;
  167. case "141":
  168. return "m4a, audio";
  169. break;
  170. case "256":
  171. return "m4a, audio";
  172. break;
  173. case "258":
  174. return "m4a, audio";
  175. break;
  176. case "325":
  177. return "m4a, audio";
  178. break;
  179. case "328":
  180. return "m4a, audio";
  181. break;
  182. case "167":
  183. return "webm, video, 360p";
  184. break;
  185. case "168":
  186. return "webm, video, 480p";
  187. break;
  188. case "169":
  189. return "webm, video, 720p";
  190. break;
  191. case "170":
  192. return "webm, video, 1080p";
  193. break;
  194. case "218":
  195. return "webm, video, 480p";
  196. break;
  197. case "219":
  198. return "webm, video, 480p";
  199. break;
  200. case "278":
  201. return "webm, video, 144p";
  202. break;
  203. case "242":
  204. return "webm, video, 240p";
  205. break;
  206. case "243":
  207. return "webm, video, 360p";
  208. break;
  209. case "244":
  210. return "webm, video, 480p";
  211. break;
  212. case "245":
  213. return "webm, video, 480p";
  214. break;
  215. case "246":
  216. return "webm, video, 480p";
  217. break;
  218. case "247":
  219. return "webm, video, 720p";
  220. break;
  221. case "248":
  222. return "webm, video, 1080p";
  223. break;
  224. case "271":
  225. return "webm, video, 1440p";
  226. break;
  227. case "272":
  228. return "webm, video, 2160p";
  229. break;
  230. case "302":
  231. return "webm, video, 720p";
  232. break;
  233. case "303":
  234. return "webm, video, 1080p";
  235. break;
  236. case "308":
  237. return "webm, video, 1440p";
  238. break;
  239. case "313":
  240. return "webm, video, 2160p";
  241. break;
  242. case "315":
  243. return "webm, video, 2160p";
  244. break;
  245. case "171":
  246. return "webm, audio";
  247. break;
  248. case "249":
  249. return "webm, audio";
  250. break;
  251. case "250":
  252. return "webm, audio";
  253. break;
  254. case "251":
  255. return "webm, audio";
  256. break;
  257. case "394":
  258. return "video";
  259. break;
  260. case "395":
  261. return "video";
  262. break;
  263. case "396":
  264. return "video";
  265. break;
  266. case "397":
  267. return "video";
  268. break;
  269. case "398":
  270. return "av01, video, 720p";
  271. break;
  272. case "399":
  273. return "av01, video, 1080p";
  274. break;
  275. default:
  276. return $itag;
  277. break;
  278. }
  279. }
  280. $a = getc("https://www.youtube.com/embed/".$id);
  281. $ccc=explode('s/player/', $a);
  282. $ddd=explode('/www-player.css', $ccc[1]);
  283. $gsts = file_get_contents('https://www.youtube.com/s/player/'.$ddd[0].'/player_ias.vflset/en_US/base.js');
  284. function getchiper($decipherScript) {
  285. $decipherPatterns = explode('.split("")', $decipherScript);
  286. unset($decipherPatterns[0]);
  287. foreach ($decipherPatterns as $value) {
  288. $value = explode('.join("")', explode('}', $value)[0]);
  289. if (count($value) === 2) {
  290. $value = explode(';', $value[0]);
  291. array_pop($value);
  292. unset($value[0]);
  293. $decipherPatterns = implode(';', $value);
  294. break;
  295. }
  296. }
  297. preg_match_all('/(?<=;).*?(?=\[|\.)/', $decipherPatterns, $deciphers);
  298. if ($deciphers && count($deciphers[0]) >= 2) {
  299. $deciphers = $deciphers[0][0];
  300. $deciphersObjectVar = $decipherPatterns ;
  301. $decipher = explode($deciphers . '={', $decipherScript)[1];
  302. $decipher = str_replace(["\n", "\r"], '', $decipher);
  303. $decipher = explode('}};', $decipher)[0];
  304. $decipher = explode('},', $decipher);
  305. // Convert deciphers to object
  306. $deciphers = [];
  307. foreach ($decipher as &$function) {
  308. $deciphers[explode(':function', $function)[0]] = explode('){', $function)[1];
  309. }
  310. // Convert pattern to array
  311. $decipherPatterns = str_replace($deciphersObjectVar . '.', '', $decipherPatterns);
  312. $decipherPatterns = str_replace($deciphersObjectVar . '[', '', $decipherPatterns);
  313. $decipherPatterns = str_replace(['](a,', '(a,'], '->(', $decipherPatterns);
  314. $decipherPatterns = explode(';', $decipherPatterns);
  315. $patterns =$decipherPatterns;
  316. $deciphers =$deciphers;
  317. if (!file_exists(TOOLS_PATH.'sig.php')) {
  318. for ($i=0; $i < count($patterns); $i++) {
  319. $executes = explode('->', $patterns[$i]);
  320. $execute=explode('.', $executes[0]);
  321. $number = intval(str_replace(['(', ')'], '', $executes[1]));
  322. $execute = $deciphers[$execute[1]];
  323. switch ($execute) {
  324. case 'a.reverse()':
  325. $processSignature = '$reverse';
  326. break;
  327. case 'var c=a[0];a[0]=a[b%a.length];a[b]=c':
  328. $processSignature= '$length';
  329. break;
  330. case 'var c=a[0];a[0]=a[b%a.length];a[b%a.length]=c':
  331. $processSignature= '$lengtha';
  332. break;
  333. case 'a.splice(0,b)':
  334. $processSignature= '$splice';
  335. break;
  336. default:
  337. die("\n==== Decipher dictionary was not found ====");
  338. break;
  339. }
  340. $myfile = fopen(TOOLS_PATH.'sig.php', "a+") or die("Unable to open file!");
  341. if ($i==0) {
  342. fwrite($myfile, '<?php $a = str_split($s);');
  343. }
  344. fwrite($myfile, $processSignature.'($a,'.$number.');');
  345. fclose($myfile);
  346. }
  347. }
  348. }
  349. }
  350. function sig($s){
  351. $reverse=function(&$a){
  352. $a = array_reverse($a);
  353. };
  354. $splice=function(&$a, $b){
  355. $a = array_slice($a, $b);
  356. };
  357. $length = function(&$a, $b){
  358. $c = $a[0];
  359. $a[0] = $a[$b % count($a)];
  360. $a[$b] = $c;
  361. };
  362. $lengtha = function(&$a, $b){
  363. $c = $a[0];
  364. $a[0] = $a[$b%count($a)];
  365. $a[$b%count($a)] = $c;
  366. };
  367. include(TOOLS_PATH.'sig.php');
  368. return join('', $a);
  369. }
  370. getchiper($gsts);
  371. $data = getc("https://www.youtube.com/get_video_info?video_id=".$id."&asv=3&el=detailpage&hl=en_US");
  372. parse_str($data,$info);
  373. $streams = $info['player_response'];
  374. $jsn_str = str_replace("\u0026","&", $streams);
  375. $streamin_data_json = json_decode($jsn_str, true);
  376. // Microdata
  377. $vidtitle = $streamin_data_json["videoDetails"]["title"];
  378. $vidId = $streamin_data_json["videoDetails"]["videoId"];
  379. if (isset( $streamin_data_json["videoDetails"]["shortDescription"] )) {
  380. $viddes = $streamin_data_json["videoDetails"]["shortDescription"];
  381. } else {
  382. $viddes = null;
  383. }
  384. $vidpub = $streamin_data_json["microformat"]["playerMicroformatRenderer"]["publishDate"];
  385. $vidviewCount = $streamin_data_json["videoDetails"]["viewCount"];
  386. $vidauthor = $streamin_data_json["videoDetails"]["author"];
  387. $vidauthorid = $streamin_data_json["videoDetails"]["channelId"];
  388. $vidauthourl = "channel/{$vidauthorid}";
  389. $vidtime = $streamin_data_json["videoDetails"]["lengthSeconds"];
  390. $vidisLiveContent = $streamin_data_json["videoDetails"]["isLiveContent"];
  391. // thumbs
  392. $vidthumb = array();
  393. foreach ($streamin_data_json["videoDetails"]["thumbnail"]["thumbnails"] as $data) {
  394. $url = $data['url'];
  395. $values = array(
  396. 'url' => $url,
  397. 'width' => $data['width'],
  398. 'height' => $data['height']
  399. );
  400. array_push($vidthumb, $values);
  401. }
  402. // keywords
  403. $vidkeywords = array();
  404. if ( isset($streamin_data_json["videoDetails"]["keywords"]) ) {
  405. foreach ($streamin_data_json["videoDetails"]["keywords"] as $data) {
  406. array_push($vidkeywords, $data);
  407. }
  408. }
  409. // streams
  410. if (isset($streamin_data_json["streamingData"])) {
  411. if (isset($streamin_data_json["streamingData"]["formats"])) {
  412. $strF=$streamin_data_json["streamingData"]["formats"];
  413. }
  414. if (isset($streamin_data_json["streamingData"]["adaptiveFormats"])) {
  415. $strA=$streamin_data_json["streamingData"]["adaptiveFormats"];
  416. }
  417. $vdef = array();
  418. if (isset($strF)) {
  419. foreach ($strF as $stream) {
  420. if (isset($stream["signatureCipher"])) {
  421. parse_str($stream["signatureCipher"],$dturl);
  422. $values = array(
  423. 'url' => $dturl['url'].'&sig='.sig($dturl['s']),
  424. 'itag' => $stream['itag'],
  425. 'type' => $stream['mimeType'],
  426. 'quality' => $stream['quality'],
  427. 'fps' => $stream['fps'],
  428. 'resolution' => $stream['qualityLabel'],
  429. 'qualityLabel' => $stream['qualityLabel'],
  430. 'format' => exformat($stream['itag'])
  431. );
  432. array_push($vdef, $values);
  433. } else {
  434. if (!empty($strF['url'])) {
  435. $values = array(
  436. 'url' => $strF['url'],
  437. );
  438. array_push($vdef, $values);
  439. }
  440. }
  441. }
  442. }
  443. $aud = array();
  444. $vid = array();
  445. if (isset($strF)) {
  446. foreach ($strF as $stream) {
  447. if (isset( $stream['url'] )) {
  448. if (preg_match('/video/', $stream['mimeType'])) {
  449. $url = $stream['url'];
  450. $values = array(
  451. 'url' => $url,
  452. 'itag' => $stream['itag'],
  453. 'type' => $stream['mimeType'],
  454. 'quality' => $stream['quality'],
  455. 'fps' => $stream['fps'],
  456. 'resolution' => $stream['qualityLabel'],
  457. 'qualityLabel' => $stream['qualityLabel'],
  458. 'format' => exformat($stream['itag']),
  459. );
  460. array_push($vid, $values);
  461. }
  462. }
  463. }
  464. }
  465. if (isset($strA)) {
  466. foreach ($strA as $stream) {
  467. if (isset($stream["signatureCipher"])) {
  468. parse_str($stream["signatureCipher"],$dturl);
  469. $stream['url']=$dturl['url'].'&sig='.sig($dturl['s']);
  470. }
  471. if (preg_match('/audio/', $stream['mimeType'])) {
  472. $url = $stream['url'];
  473. $values = array(
  474. 'url' => $url,
  475. 'itag' => $stream['itag'],
  476. 'type' => $stream['mimeType'],
  477. 'quality' => $stream['quality'],
  478. 'format' => exformat($stream['itag']),
  479. );
  480. array_push($aud, $values);
  481. }
  482. if (preg_match('/video/', $stream['mimeType'])) {
  483. $url = $stream['url'];
  484. $values = array(
  485. 'url' => $url,
  486. 'itag' => $stream['itag'],
  487. 'type' => $stream['mimeType'],
  488. 'quality' => $stream['quality'],
  489. 'fps' => $stream['fps'],
  490. 'resolution' => $stream['qualityLabel'],
  491. 'qualityLabel' => $stream['qualityLabel'],
  492. 'format' => exformat($stream['itag']),
  493. );
  494. array_push($vid, $values);
  495. }
  496. }
  497. }
  498. } else {
  499. $vdef = array();
  500. $aud = array();
  501. $vid = array();
  502. }
  503. // $json['title']=$vidtitle;
  504. // $json['videoId']=$vidId;
  505. // $json['videoThumbnails']=$vidthumb;
  506. // $json['description']=$viddes;
  507. // $json['publishedText']=$vidpub;
  508. // $json['viewCount']=$vidviewCount;
  509. // $json['keywords']=$vidkeywords;
  510. // $json['author']=$vidauthor;
  511. // $json['authorId']=$vidauthorid;
  512. // $json['authorUrl']=$vidauthourl;
  513. // $json['lengthSeconds']=$vidtime;
  514. // $json['liveNow']=$vidisLiveContent;
  515. // $json['default']=$vdef;
  516. // $json['audio']=$aud;
  517. // $json['video']=$vid;