forked from PRNDA/you2php
-
Notifications
You must be signed in to change notification settings - Fork 1
/
YouTubeDownloader.php
388 lines (277 loc) · 9.49 KB
/
YouTubeDownloader.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
<?php
error_reporting(0);
// utils.php
function sig_js_decode($player_html){
// what javascript function is responsible for signature decryption?
// var l=f.sig||Xn(f.s)
// a.set("signature",Xn(c));return a
if(preg_match('/signature",([a-zA-Z0-9$]+)\(/', $player_html, $matches)){
$func_name = $matches[1];
$func_name = preg_quote($func_name);
// extract code block from that function
// single quote in case function name contains $dollar sign
// xm=function(a){a=a.split("");wm.zO(a,47);wm.vY(a,1);wm.z9(a,68);wm.zO(a,21);wm.z9(a,34);wm.zO(a,16);wm.z9(a,41);return a.join("")};
if(preg_match('/'.$func_name.'=function\([a-z]+\){(.*?)}/', $player_html, $matches)){
$js_code = $matches[1];
// extract all relevant statements within that block
// wm.vY(a,1);
if(preg_match_all('/([a-z0-9]{2})\.([a-z0-9]{2})\([^,]+,(\d+)\)/i', $js_code, $matches) != false){
// must be identical
$obj_list = $matches[1];
//
$func_list = $matches[2];
// extract javascript code for each one of those statement functions
preg_match_all('/('.implode('|', $func_list).'):function(.*?)\}/m', $player_html, $matches2, PREG_SET_ORDER);
$functions = array();
// translate each function according to its use
foreach($matches2 as $m){
if(strpos($m[2], 'splice') !== false){
$functions[$m[1]] = 'splice';
} else if(strpos($m[2], 'a.length') !== false){
$functions[$m[1]] = 'swap';
} else if(strpos($m[2], 'reverse') !== false){
$functions[$m[1]] = 'reverse';
}
}
// FINAL STEP! convert it all to instructions set
$instructions = array();
foreach($matches[2] as $index => $name){
$instructions[] = array($functions[$name], $matches[3][$index]);
}
return $instructions;
}
}
}
return false;
}
// YouTube is capitalized twice because that's how youtube itself does it:
// https://developers.google.com/youtube/v3/code_samples/php
class YouTubeDownloader {
private $storage_dir;
private $cookie_dir;
private $itag_info = array(
18 => "360P",
22 => "720P",
37 => "1080P",
38 => "3072P",
// questionable MP4s
59 => "MP4480P",
78 => "MP4480P",
43 => "WebM360P",
17 => "3GP144P"
);
function __construct(){
$this->storage_dir = sys_get_temp_dir();
$this->cookie_dir = sys_get_temp_dir();
}
function setStorageDir($dir){
$this->storage_dir = $dir;
}
// what identifies each request? user agent, cookies...
public function curl($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.3; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
//curl_setopt($ch, CURLOPT_COOKIEJAR, $tmpfname);
//curl_setopt($ch, CURLOPT_COOKIEFILE, $tmpfname);
//curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
public static function head($url){
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_NOBODY, 1);
$result = curl_exec($ch);
curl_close($ch);
return http_parse_headers($result);
}
// html code of watch?v=aaa
private function getInstructions($html){
// <script src="//s.ytimg.com/yts/jsbin/player-fr_FR-vflHVjlC5/base.js" name="player/base"></script>
// check what player version that video is using
if(preg_match('@<script\s*src="([^"]+player[^"]+js)@', $html, $matches)){
$player_url = $matches[1];
// relative protocol?
if(strpos($player_url, '//') === 0){
$player_url = 'http://'.substr($player_url, 2);
} else if(strpos($player_url, '/') === 0){
// relative path?
$player_url = 'http://www.youtube.com'.$player_url;
}
// try to find instructions list already cached from previous requests...
$file_path = $this->storage_dir.'/'.md5($player_url);
if(file_exists($file_path)){
// unserialize could fail on empty file
$str = file_get_contents($file_path);
return unserialize($str);
} else {
$js_code = $this->curl($player_url);
$instructions = sig_js_decode($js_code);
if($instructions){
file_put_contents($file_path, serialize($instructions));
return $instructions;
}
}
}
return false;
}
// this is in beta mode!!
public function stream($id){
$links = $this->getDownloadLinks($id, "mp4");
if(count($links) == 0){
die("no url found!");
}
// grab first available MP4 link
$url = $links[0]['url'];
// request headers
$headers = array(
'User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; rv:49.0) Gecko/20100101 Firefox/49.0'
);
if(isset($_SERVER['HTTP_RANGE'])){
$headers[] = 'Range: '.$_SERVER['HTTP_RANGE'];
}
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
// we deal with this ourselves
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
// whether request to video success
$headers = '';
$headers_sent = false;
$success = false;
curl_setopt($ch, CURLOPT_HEADERFUNCTION, function($ch, $data) use (&$headers, &$headers_sent){
$headers .= $data;
// this should be first line
if(preg_match('@HTTP\/\d\.\d\s(\d+)@', $data, $matches)){
$status_code = $matches[1];
// status=ok or partial content
if($status_code == 200 || $status_code == 206){
$headers_sent = true;
header(rtrim($data));
}
} else {
// only headers we wish to forward back to the client
$forward = array('content-type', 'content-length', 'accept-ranges', 'content-range');
$parts = explode(':', $data, 2);
if($headers_sent && count($parts) == 2 && in_array(trim(strtolower($parts[0])), $forward)){
header(rtrim($data));
}
}
return strlen($data);
});
// if response is empty - this never gets called
curl_setopt($ch, CURLOPT_WRITEFUNCTION, function($curl, $data) use (&$headers_sent){
if($headers_sent){
echo $data;
flush();
}
return strlen($data);
});
$ret = @curl_exec($ch);
$error = curl_error($ch);
curl_close($ch);
// if we are still here by now, return status_code
return true;
}
// extract youtube video_id from any piece of text
public function extractId($str){
if(preg_match('/[a-z0-9_-]{11}/i', $str, $matches)){
return $matches[0];
}
return false;
}
// selector by format: mp4 360,
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;
}
// options | deep_links | append_redirector
public function getDownloadLinks($id, $selector = false){
$result = array();
$instructions = array();
// you can input HTML of /watch? page directory instead of id
if(strpos($id, '<div id="player') !== false){
$html = $id;
} else {
$video_id = $this->extractId($id);
if(!$video_id){
return false;
}
$html = $this->curl("https://www.youtube.com/watch?v={$video_id}");
}
// age-gate
if(strpos($html, 'player-age-gate-content') !== false){
// nothing you can do folks...
return false;
}
// http://stackoverflow.com/questions/35608686/how-can-i-get-the-actual-video-url-of-a-youtube-live-stream
if(preg_match('@url_encoded_fmt_stream_map["\']:\s*["\']([^"\'\s]*)@', $html, $matches)){
$parts = explode(",", $matches[1]);
foreach($parts as $p){
$query = str_replace('\u0026', '&', $p);
parse_str($query, $arr);
$url = $arr['url'];
if(isset($arr['sig'])){
$url = $url.'&signature='.$arr['sig'];
} else if(isset($arr['signature'])){
$url = $url.'&signature='.$arr['signature'];
} else if(isset($arr['s'])){
// this is probably a VEVO/ads video... signature must be decrypted first! We need instructions for doing that
if(count($instructions) == 0){
$instructions = (array)$this->getInstructions($html);
}
$dec = $this->sig_decipher($arr['s'], $instructions);
$url = $url.'&signature='.$dec;
}
// redirector.googlevideo.com
//$url = preg_replace('@(\/\/)[^\.]+(\.googlevideo\.com)@', '$1redirector$2', $url);
$itag = $arr['itag'];
$format = isset($this->itag_info[$itag]) ? $this->itag_info[$itag] : 'Unknown';
$result[$itag] = array(
'url' => $url,
'format' => $format
);
}
}
// do we want all links or just select few?
if($selector){
return $this->selectFirst($result, $selector);
}
return $result;
}
private function sig_decipher($signature, $instructions){
foreach($instructions as $opt){
$command = $opt[0];
$value = $opt[1];
if($command == 'swap'){
$temp = $signature[0];
$signature[0] = $signature[$value % strlen($signature)];
$signature[$value] = $temp;
} else if($command == 'splice'){
$signature = substr($signature, $value);
} else if($command == 'reverse'){
$signature = strrev($signature);
}
}
return trim($signature);
}
}
?>