forked from kgfw/youtube-php-mirroring
-
Notifications
You must be signed in to change notification settings - Fork 610
/
Copy pathnglib.php
231 lines (216 loc) · 8.56 KB
/
nglib.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
<?php
/************************************
* 你管索引lib v2
@updated 2012年12月9日1:44:22
@author laobubu
@description 获取youtube视频下载地址
************************************/
//获取原始数据
function getVInfo($id){
$header[0] = "Accept: text/xml,application/xml,application/xhtml+xml,";
$header[0] .= "text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5";
$header[] = "Cache-Control: max-age=0";
$header[] = "Connection: keep-alive";
$header[] = "Keep-Alive: 300";
$header[] = "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7";
$header[] = "Accept-Language: zh-cn,en;q=0.5";
$header[] = "Pragma: ";
$url = 'http://www.youtube.com/get_video_info?video_id='.$id.'&eurl=http://ng.laobubu.com/';
if (!function_exists("curl_init")) {
$f = file_get_contents($url);
} else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.youtube.com/watch?v='.$id);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$f = curl_exec($ch);
curl_close($ch);
}
if (strpos($f,'url_encoded_fmt_stream_map')===FALSE) { //error happened
$url = 'http://www.youtube.com/watch?v='.$id;
if (!function_exists("curl_init")) {
$f = file_get_contents($url);
} else {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 20);
curl_setopt($ch, CURLOPT_REFERER, 'http://www.youtube.com/watch?v='.$id);
@curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows NT 5.1) AppleWebKit/534.30 (KHTML, like Gecko) Chrome/12.0.742.91 Safari/534.30");
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$f = curl_exec($ch);
curl_close($ch);
}
$f = substr($f,strpos($f,'flashvars'));
$f = substr($f,strpos($f,'"')+1);
$f = substr($f,0,strpos($f,'"'));
$f = str_replace('\u0026','&',$f);
$f = str_replace('&','&',$f);
}
return $f;
}
function deleteFromArray(&$array, $deleteIt, $useOldKeys = FALSE){
$key = array_search($deleteIt,$array,TRUE);
if($key === FALSE)
return FALSE;
unset($array[$key]);
if(!$useOldKeys)
$array = array_values($array);
return TRUE;
}
/****************************
按照typelist要求的将指定格式视频列到前面
@author laobubu
@example typelist = "webm,mp4"
****************************/
function formatVListByType($vlist,$typelist,$typename="type",$removeOtherType=false){
$rtn = array();
$f = explode(',',$typelist);
foreach ($f as $t) {
foreach ($vlist as $i) {
$temp= stripos($i[$typename],$t);
if (($temp===0)|($temp>3)) {
$rtn[] = $i;
deleteFromArray($vlist,$i);
}
}
}
if (!$removeOtherType) $rtn=array_merge($rtn,$vlist);
$rtn2 = array();
$f = array("medium","large","hd720","hd1080");
foreach ($f as $t) {
foreach ($rtn as $i) {
if (strpos($i['quality'],$t)!==FALSE) {
$rtn2[] = $i;
deleteFromArray($rtn,$i);
}
}
}
$rtn2=array_merge($rtn2,$rtn);
return $rtn2;
}
/****************************
从原始数据里提取视频地址列表
@author laobubu
@example
array(16) {
[0]=>
array(5) {
["itag"]=> string(2) "46"
["url"]=>string(562) "视频地址"
["type"]=> string(34) "video/webm; codecs="vp8.0, vorbis""
["fallback_host"]=> string(27) "tc.v14.cache5.c.youtube.com"
["quality"]=> string(6) "hd1080"
}
[1]=>
array(5) {
["itag"]=> string(2) "37"
["url"]=> string(562) "视频地址"
["type"]=> string(42) "video/mp4; codecs="avc1.64001F, mp4a.40.2""
["fallback_host"]=> string(27) "tc.v15.cache4.c.youtube.com"
["quality"]=> string(6) "hd1080"
}
[2]=>
array(5) {
["itag"]=> string(2) "45"
["url"]=> string(561) "视频地址"
["type"]=> string(34) "video/webm; codecs="vp8.0, vorbis""
["fallback_host"]=> string(27) "tc.v22.cache2.c.youtube.com"
["quality"]=> string(5) "hd720"
}
...
**************************/
function url_encoded_fmt_stream_map($raw, $remove3D=true, $removeSmall=true) {
parse_str($raw,$a);
$str = explode(',',$a["url_encoded_fmt_stream_map"]);
$rtn = array();
foreach($str as $item) {
parse_str($item,$add);
$q = urldecode($a["quality"]);
$add["url"] = (urldecode($add["url"])."&signature=".$add["sig"]);
$add["type"]= stripslashes( $add["type"]);
unset($add["sig"]);
if (isset($add["stereo3d"]) && $remove3D) continue;
if (($add["quality"] == "small") && $removeSmall) continue;
$rtn[] = $add;
}
$rtn = array_reverse($rtn);
return $rtn;
}
$defaultStreams = array(
"5"=>array("width" =>"320","height" =>"240","container" =>"FLV,acodec:MP3,vcodec:H.263"),
"17"=>array("width" =>"176","height" =>"144","container" =>"3GPP,acodec:AAC,vcodec:MPEG-4"),
"18"=>array("width" =>"640","height" =>"360","container" =>"MP4,acodec:AAC,vcodec:H.264","vprofile" =>"Baseline"),
"22"=>array("width" =>"1280","height" =>"720","container" =>"MP4,acodec:AAC,vcodec:H.264","vprofile" =>"High"),
"34"=>array("width" =>"640","height" =>"360","container" =>"FLV,acodec:AAC,vcodec:H.264","vprofile" =>"Main"),
"35"=>array("width" =>"854","height" =>"480","container" =>"FLV,acodec:AAC,vcodec:H.264","vprofile" =>"Main"),
"36"=>array("width" =>"320","height" =>"240","container" =>"3GPP,acodec:AAC,vcodec:MPEG-4","vprofile" =>"Simple"),
"37"=>array("width" =>"1920","height" =>"1080","container" =>"MP4,acodec:AAC,vcodec:H.264"),
"38"=>array("width" =>"2048","height" =>"1536","container" =>"MP4,acodec:AAC,vcodec:H.264"),
"43"=>array("width" =>"640","height" =>"360","container" =>"WebM,acodec:Vorbis,vcodec:VP8"),
"44"=>array("width" =>"854","height" =>"480","container" =>"WebM,acodec:Vorbis,vcodec:VP8"),
"45"=>array("width" =>"1280","height" =>"720","container" =>"WebM,acodec:Vorbis,vcodec:VP8"),
"46"=>array("width" =>"1920","height" =>"1080","container" =>"WebM,acodec:Vorbis,vcodec:VP8"),
"82"=>array("width" =>"640","height" =>"360","container" =>"MP4,acodec:AAC,vcodec:H.264","stereo3d" =>true),
"83"=>array("width" =>"854","height" =>"480","container" =>"MP4,acodec:AAC,vcodec:H.264","stereo3d" =>true),
"84"=>array("width" =>"1280","height" =>"720","container" =>"MP4,acodec:AAC,vcodec:H.264","stereo3d" =>true),
"85"=>array("width" =>"1920","height" =>"1080","container" =>"MP4,acodec:AAC,vcodec:H.264","stereo3d" =>true),
"100"=>array("width" =>"640","height" =>"360","container" =>"WebM,acodec:Vorbis,vcodec:VP8","stereo3d" =>true),
"101"=>array("width" =>"854","height" =>"480","container" =>"WebM,acodec:Vorbis,vcodec:VP8","stereo3d" =>true),
"102"=>array("width" =>"1280","height" =>"720","container" =>"WebM,acodec:Vorbis,vcodec:VP8","stereo3d" =>true),
);
//VAR AP
if (strpos($_SERVER['SCRIPT_NAME'],'nglib.php')!==FALSE){
$_tempvid='uelHwf8o7_U';
if (isset($_GET['v'])) $_tempvid = $_GET['v'];
$a = getVInfo($_tempvid);
parse_str($a,$b);
unset($b['url_encoded_fmt_stream_map']);
$b['yti_vlist']=url_encoded_fmt_stream_map($a,!isset($_GET['keep3d']),!isset($_GET['keepsmall']));
if (isset($_GET['fvlist'])) $b['yti_vlist'] = formatVListByType($b['yti_vlist'],$_GET['fvlist']);
ob_end_clean();
$rtn = json_encode($b);
if (isset($_GET['encode_magickey'])) {
$a = intval($_GET['encode_magickey']);
function encode_char(&$ch){
global $a;
return chr(ord($ch)+$a);
}
//$rtn = iconv('UTF-8','UTF-16',$rtn);
preg_match_all('/([\d\D])/s',$rtn,$bytes);
$bytes=array_map('encode_char',$bytes[1]) ;
$rtn=implode('',$bytes);
//$str = iconv('UTF-16',$charset,$str);
}
die($rtn);
}
/*********
loadvideo 处理视频地址解析并放入session
*************/
function loadvideo($vid,$type=""){
if (isset($_SESSION['vurl_expire_'.$vid]))
if (time()>$_SESSION['vurl_expire_'.$vid]) {
unset($_SESSION['vurl_'.$vid]);
}
if (!isset($_SESSION['vurl_'.$vid])) {
$vlist = url_encoded_fmt_stream_map(getVInfo($vid));
$_SESSION['vurl_'.$vid] = $vlist;
$_SESSION['vurl_expire_'.$vid] = time()+60*35;
}
}
function getitembytype($vid,$type){
loadvideo($vid);
$vlist = formatVListByType( $_SESSION['vurl_'.$vid] ,$type);
return $vlist[0];
}
function getitembyitag($vid,$itag){
loadvideo($vid);
$vlist = formatVListByType( $_SESSION['vurl_'.$vid] ,$itag,"itag");
return $vlist[0];
}
?>