This repository has been archived by the owner on Feb 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 4
/
getvideo.php
292 lines (261 loc) · 8.71 KB
/
getvideo.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
<?php
// YouTube Downloader PHP
// based on youtube-dl in Python http://rg3.github.com/youtube-dl/
// by Ricardo Garcia Gonzalez and others (details at url above)
//
// Takes a VideoID and outputs a list of formats in which the video can be
// downloaded
// if not, some servers will show this php warning: header is already set in line 46...
include_once('curl.php');
ob_start();
// date_default_timezone_set("Asia/Tehran"); // if default timezone not set php shows a notice
function formatBytes($bytes, $precision = 2) {
$units = array('B', 'kB', 'MB', 'GB', 'TB', 'PB', 'EB', 'ZB', 'YB');
$bytes = max($bytes, 0);
$pow = floor(($bytes ? log($bytes) : 0) / log(1024));
$pow = min($pow, count($units) - 1);
$bytes /= pow(1024, $pow);
return round($bytes, $precision) . '' . $units[$pow];
}
function is_chrome(){
$agent=$_SERVER['HTTP_USER_AGENT'];
if( preg_match("/like\sGecko\)\sChrome\//", $agent) ){ // if user agent is google chrome
if(!strstr($agent, 'Iron')) // but not Iron
return true;
}
return false; // if isn't chrome return false
}
if(isset($_REQUEST['videoid'])) {
$my_id = $_REQUEST['videoid'];
if(strlen($my_id)>11){
$url = parse_url($my_id);
$my_id = NULL;
if( is_array($url) && count($url)>0 && isset($url['query']) && !empty($url['query']) ){
$parts = explode('&',$url['query']);
if( is_array($parts) && count($parts) > 0 ){
foreach( $parts as $p ){
$pattern = '/^v\=/';
if( preg_match($pattern, $p) ){
$my_id = preg_replace($pattern,'',$p);
break;
}
}
}
if( !$my_id ){
echo '<p>No video id passed in</p>';
exit;
}
}else{
echo '<p>Invalid url</p>';
exit;
}
}
} else {
echo '<p>No video id passed in</p>';
exit;
}
if(isset($_REQUEST['type'])) {
$my_type = $_REQUEST['type'];
} else {
$my_type = 'redirect';
}
if(isset($_REQUEST['debug'])) {
$debug = TRUE;
} else {
$debug = FALSE;
}
if ($my_type == 'Download') {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>Youtube Downloader</title>
<meta name="keywords" content="Video downloader, download youtube, video download, youtube video, youtube downloader, download youtube FLV, download youtube MP4, download youtube 3GP, php video downloader" />
<link href="css/bootstrap.min.css" rel="stylesheet" media="screen">
<style type="text/css">
body {
padding-top: 40px;
padding-bottom: 40px;
background-color: #f5f5f5;
}
.download {
max-width: 300px;
padding: 19px 29px 29px;
margin: 0 auto 20px;
background-color: #fff;
border: 1px solid #e5e5e5;
-webkit-border-radius: 5px;
-moz-border-radius: 5px;
border-radius: 5px;
-webkit-box-shadow: 0 1px 2px rgba(0,0,0,.05);
-moz-box-shadow: 0 1px 2px rgba(0,0,0,.05);
box-shadow: 0 1px 2px rgba(0,0,0,.05);
}
.download .download-heading {
text-align:center;
margin-bottom: 10px;
}
.mime, .itag {
width: 75px;
display: inline-block;
}
.itag {
width: 15px;
}
.size {
width: 20px;
}
.userscript {
float: right;
margin-top: 5px
}
#info {
padding: 0 0 0 130px;
position: relative;
height:100px;
}
#info img{
left: 0;
position: absolute;
top: 0;
width:120px;
height:90px
}
</style>
</head>
<body>
<div class="download">
<h1 class="download-heading">Youtube Downloader Results</h1>
<?php
} // end of if for type=Download
/* First get the video info page for this video id */
$my_video_info = 'http://www.youtube.com/get_video_info?&video_id='. $my_id;
$my_video_info = curlGet($my_video_info);
/* TODO: Check return from curl for status code */
$thumbnail_url = $title = $url_encoded_fmt_stream_map = $type = $url = '';
parse_str($my_video_info);
echo '<div id="info"><img src="'. $thumbnail_url .'" border="0" hspace="2" vspace="2"><p>'.$title.'</p></div>';
$my_title = $title;
if(isset($url_encoded_fmt_stream_map)) {
/* Now get the url_encoded_fmt_stream_map, and explode on comma */
$my_formats_array = explode(',',$url_encoded_fmt_stream_map);
if($debug) {
echo '<pre>';
print_r($my_formats_array);
echo '</pre>';
}
} else {
echo '<p>No encoded format stream found.</p>';
echo '<p>Here is what we got from YouTube:</p>';
echo $my_video_info;
}
if (count($my_formats_array) == 0) {
echo '<p>No format stream map found - was the video id correct?</p>';
exit;
}
/* create an array of available download formats */
$avail_formats[] = '';
$i = 0;
$ipbits = $ip = $itag = $sig = $quality = '';
$expire = time();
foreach($my_formats_array as $format) {
parse_str($format);
$avail_formats[$i]['itag'] = $itag;
$avail_formats[$i]['quality'] = $quality;
$type = explode(';',$type);
$avail_formats[$i]['type'] = $type[0];
$avail_formats[$i]['url'] = urldecode($url) . '&signature=' . $sig;
parse_str(urldecode($url));
$avail_formats[$i]['expires'] = date("G:i:s T", $expire);
$avail_formats[$i]['ipbits'] = $ipbits;
$avail_formats[$i]['ip'] = $ip;
$i++;
}
if ($debug) {
echo '<p>These links will expire at '. $avail_formats[0]['expires'] .'</p>';
echo '<p>The server was at IP address '. $avail_formats[0]['ip'] .' which is an '. $avail_formats[0]['ipbits'] .' bit IP address. ';
echo 'Note that when 8 bit IP addresses are used, the download links may fail.</p>';
}
if ($my_type == 'Download') {
echo '<p align="center">List of available formats for download:</p>
<ul>';
/* now that we have the array, print the options */
for ($i = 0; $i < count($avail_formats); $i++) {
echo '<li>' .
'<span class="itag">' . $avail_formats[$i]['itag'] . '</span> '.
'<a href="' . $avail_formats[$i]['url'] . '" class="mime">' . $avail_formats[$i]['type'] . '</a> ' .
'<small>(' . $avail_formats[$i]['quality'] . ' / ' .
'<a href="download.php?mime=' . $avail_formats[$i]['type'] .'&title='. urlencode($my_title) .'&token='
.base64_encode($avail_formats[$i]['url']) . '" class="dl">download</a>' .
')</small> '.
'<small><span class="size">' . formatBytes(get_size($avail_formats[$i]['url'])) . '</span></small>'.
'</li>';
}
echo '</ul><small>Note that you can Right-click and choose "save as" or click "download" to use this server as proxy.</small>';
if(is_chrome()){
echo '<a href="ytdl.user.js" class="userscript btn btn-mini" title="Install chrome extension to view a 'Download' link to this application on Youtube video pages."> Install Chrome Extension</a>';
}
?>
</body>
</html>
<?php
} else {
/* In this else, the request didn't come from a form but from something else
* like an RSS feed.
* As a result, we just want to return the best format, which depends on what
* the user provided in the url.
* If they provided "format=best" we just use the largest.
* If they provided "format=free" we provide the best non-flash version
* If they provided "format=ipad" we pull the best MP4 version
*
* Thanks to the python based youtube-dl for info on the formats
* http://rg3.github.com/youtube-dl/
*/
$format = $_REQUEST['format'];
$target_formats = '';
switch ($format) {
case "best":
/* largest formats first */
$target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
break;
case "free":
/* Here we include WebM but prefer it over FLV */
$target_formats = array('38', '46', '37', '45', '22', '44', '35', '43', '34', '18', '6', '5', '17', '13');
break;
case "ipad":
/* here we leave out WebM video and FLV - looking for MP4 */
$target_formats = array('37','22','18','17');
break;
default:
/* If they passed in a number use it */
if (is_numeric($format)) {
$target_formats[] = $format;
} else {
$target_formats = array('38', '37', '46', '22', '45', '35', '44', '34', '18', '43', '6', '5', '17', '13');
}
break;
}
/* Now we need to find our best format in the list of available formats */
$best_format = '';
for ($i=0; $i < count($target_formats); $i++) {
for ($j=0; $j < count ($avail_formats); $j++) {
if($target_formats[$i] == $avail_formats[$j]['itag']) {
//echo '<p>Target format found, it is '. $avail_formats[$j]['itag'] .'</p>';
$best_format = $j;
break 2;
}
}
}
//echo '<p>Out of loop, best_format is '. $best_format .'</p>';
if( (isset($best_format)) &&
(isset($avail_formats[$best_format]['url'])) &&
(isset($avail_formats[$best_format]['type']))
) {
$redirect_url = $avail_formats[$best_format]['url'];
$content_type = $avail_formats[$best_format]['type'];
}
if(isset($redirect_url)) {
header("Location: $redirect_url");
}
} // end of else for type not being Download
?>