Code:
<?php
/*
* Simple Youtube video downloader
* By xMopx (Dave)
* http://xmop.org/
* http://www.110mb.com/forum/how-to-tutorials-on-various-script-installationstips-tricks/script-youtube-video-downloader-t42837.0.html;new
*/
function fetch($url) {
$c=curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
return curl_exec($c);
}
function get_http_header($url){
$uh = curl_init();
curl_setopt($uh, CURLOPT_URL, $url);
curl_setopt($uh, CURLOPT_HEADER, 1);
curl_setopt($uh, CURLOPT_NOBODY, 1);
curl_setopt($uh, CURLOPT_RETURNTRANSFER, 1);
$res = curl_exec($uh);
curl_close($uh);
return $res;
}
if($_POST['video']=="") {
echo <<<FORM
<form action="yt.php" method="post">
<label>Youtube video address: <input type="text" name="video"></label><br>
<label>High quality: <input type="checkbox" name="high" value="true" checked></label><br>
<label>Rename video (Slow): <input type="checkbox" name="rename" value="true"></label><br>
<input type="submit" value="Go">
</form>
FORM;
exit;
}
$url = $_POST['video'];
$high = $_POST['high']=="true";
preg_match_all("/v=(([.]*[^&])*)/", $url, $matches);
$video = $matches[1][0];
$info=fetch("http://youtube.com/get_video_info?&video_id={$video}");
$info=explode("&", $info);
foreach($info as $key=>$value) {
$info[$key]=explode("=", $value, 2);
}
foreach($info as $key=>$value) {
if($value[0]=="token") {
$token = $value[1];
break;
}
}
$URL="";
$URL.="/get_video?video_id=";
$URL.=$video;
$URL.="&t=";
$URL.=$token;
if($high==true) {
$URL.="&fmt=18";
}
$continue = true;
$realLoc = "http://www.youtube.com".$URL;
do {
$found = false;
$headers = get_http_header($realLoc);
$headers=explode("\n", $headers);
foreach($headers as $key=>$value) {
$headers[$key]=explode(": ", $value, 2);
}
foreach($headers as $key=>$value) {
if($value[0]=="Location") {
$realLoc = $value[1];
$found=true;
}
}
if($found == false) {
$continue = false;
}
} while($continue);
$realinfo =  explode("/", $realLoc, 4);
$host = $realinfo[2];
$file = $realinfo[3];
/* GET TITLE SECTION */
$c=curl_init($url);
curl_setopt($c, CURLOPT_RETURNTRANSFER, true);
curl_setopt($c, CURLOPT_FOLLOWLOCATION, true);
$pageContent=curl_exec($c);
$pageContent=explode("<title>", $pageContent,2);
$pageContent=explode("</title>", $pageContent[1]);
$TITLE=$pageContent[0];
/* END GET TITLE SECTION */
$f=@fsockopen($host, 80);
if(!$f) {
die("Invalid URL!");
}
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: private"); #, false);
header("Content-Type: application/force-download");
if($_POST['rename']!="") {
header("Content-Disposition: attachment; filename=$TITLE.".($high ? "mp4" : "flv") );
fputs($f, "GET /{$file} HTTP/1.0\r\nHost: $host\r\n\r\n");
$echo = false;
while(!feof($f)) {
if($echo == false) {
$line=fgets($f);
} else if($echo == true) {
echo fgets($f);
}
if($echo == false) {
if ($line=="\r\n") {
$echo = true;
}
}
}
fclose($f);
} else {
$rawURL="http://www.youtube.com/get_video?video_id=".$video."&t=".$token.($high ? "&fmt=18" : "");
header("Location: ".$rawURL);
}
?>
• Provides a box to enter a video URL
• High quality MP4 is default, low quality FLV is optional
• Renaming the video to the title is optional, but activating it means the data has to travel through the server - slower and more expensive, but hey it's a feature baby.
Download: http://downloads.xmop.org/scripts/do...hp?file=yt.php
Comment