Youtube video downloader

Started on the old forum in January 2010 · view the original in the archive · old links redirect here

3 replies · 1,364 views · started 16 years ago · 1 min read

3 participants
riderz Lead · 975 posts #1
<?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);
}
?>

It: • 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/download.php?file=yt.php


Jacques jacques@gw-designs.co.za http://coding.biz.tm Come join and lets make it a place to learn all the noobies how to code


NEVER FORGET TO CLICK THE TANX BUTTON IF U LIKE WHAT IM SHARING OR HELPING WITH

warfrek Junior · 44 posts #2

this is a good sample

i hope this works fine, ill try to make use of this yt.php file. Hoping that it really download the real one.

frostymarvelous Senior · 266 posts #3

I've got a class that can retrieve youtube files based on the identifier. You will have to integrate it into an app yourself though.

Perfection comes at a cost

I accept liberty!

Write your reply now; next you create a free account (or sign in) and it is posted right away. Sign in to reply.

Similar topics

Keyboard shortcuts

g f
Go to forums
g h
Go to your feed
g m
Go to messages
r
Reply (in a topic)
/
Search
j
Next post
k
Previous post
?
This list
Ctrl/⌘ + Enter
Send what you are writing
Ctrl/⌘ + B / I
Bold / italic while writing
Esc
Close