forked from instance01/YoutubeDownloaderScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathYoutubeDownloader.php
More file actions
96 lines (83 loc) · 3.21 KB
/
YoutubeDownloader.php
File metadata and controls
96 lines (83 loc) · 3.21 KB
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
<?php
if(!isset($_GET['v'])){
echo("The source code of this script can be found <a href='https://github.com/instance01/YoutubeDownloaderScript/blob/master/YoutubeDownloader.php'> here </a>.<br><br>");
echo("Usage: youtubedownloader.php?v=videoid<br><b>Videoid</b> would be the red part: https://www.youtube.com/watch?v=<font color='red'>U8B8RkcF0Wc</font><br>Example: <a href='http://www.instancedev.com/youtubedl/YoutubeDownloader.php?v=U8B8RkcF0W'>http://www.instancedev.com/youtubedl/YoutubeDownloader.php?v=U8B8RkcF0Wc</a><br><br>");
echo("Arguments:<br><b> &mp4=true </b> | Download in mp4 format only <br><b> &debug=true </b> | Print out debug info <br>Example: <a href='http://www.instancedev.com/youtubedl/YoutubeDownloader.php?v=U8B8RkcF0W&mp4=true'>http://www.instancedev.com/youtubedl/YoutubeDownloader.php?v=U8B8RkcF0Wc&mp4=true</a>");
exit;
}
$videoid = $_GET['v'];
function getDownloadLink($videoid){
$invalid_filechars = array('/', '?', '*', '\\', '\n', '\r', '\t', '\0', '\f', '<', '>', '|', '\"', ':', '`');
$ch = curl_init("http://www.youtube.com/get_video_info?video_id=$videoid&fmt=18");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
$data = curl_exec($ch);
parse_str($data, $params);
curl_close($ch);
$urlmap = urldecode(urldecode($params['url_encoded_fmt_stream_map']));
/*$index = strpos($urlmap, 'url=') + 4;
$dlurl = substr($urlmap, $index, strpos($urlmap, ';', $index) - $index);*/
$dlurl = "";
$arr = explode('url=', $urlmap);
$skipped = false;
foreach ($arr as $val){
if(!$skipped){
$skipped = true;
}else{
if(strpos($val, 'mp4') !== false){
$index = 0;
$dlurl = substr($val, $index, strpos($val, ';', $index) - $index);
break;
}
}
}
$headers = get_headers($dlurl, 1);
$type = $headers["Content-Type"];
if(isset($_GET['debug'])){
echo($type."<br>");
}
$valid_type = "video";
if(isset($_GET['mp4'])){
$valid_type = "video/mp4";
}
if(strrpos($type, $valid_type) !== FALSE){
$filename = str_replace($invalid_filechars, '_', $params['title']).'.'.$filename .= substr($type, strpos($type, "/") + 1);;
if(isset($_GET['debug'])){
echo($filename.'<br><br>');
echo($dlurl.'<br><br>');
echo('<a href="'.$dlurl.'">Rightclick -> Download Target Under ..</a><br><br>');
} else {
forceDownload($dlurl, $filename);
}
return false;
} else {
return true;
}
}
function forceDownload($url, $name) {
set_time_limit(0);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$r = curl_exec($ch);
curl_close($ch);
header('Expires: 0');
header('Cache-Control: must-revalidate, post-check=0, pre-check=0');
header('Last-Modified: '.gmdate('D, d M Y H:i:s', time()).' GMT');
header('Cache-Control: private', false);
header('Content-Type: application/force-download');
header('Content-Disposition: attachment; filename="'.$name.'"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: '.strlen($r));
header('Connection: close');
echo $r;
}
$cont = TRUE;
while($cont){
$cont = getDownloadLink($videoid);
//sleep(1);
}
?>