The following Php-Script is able to download videos from youtube in three different formats (MP4 for hight, FLV for medium and 3GP for low quality). In addition the script provides a preview of the current video.
Here’s the source:
if(isset($_GET['u'])){
$v = urldecode($_GET['u']);
if($_GET['fmt'] == 17){ // 3gp
header("Content-Type: video/3gp");
header("Content-Disposition: attachment; filename=\"video.3gp\"");
$v.="&fmt=17";
}
elseif($_GET['fmt'] == 18){ // mp4
header("Content-Type: video/mp4");
header("Content-Disposition: attachment; filename=\"video.mp4\"");
$v.="&fmt=18";
}
else{ // flv
header("Content-Type: video/x-flv");
header("Content-Disposition: attachment; filename=\"video.flv\"");
}
readfile($v);
flush();
exit();
}else{
$yu = 'http://www.youtube.com/watch?v=qGyfaoUAWtM';
if(isset($_GET["yu"])) $yu = $_GET["yu"];
$c = file_get_contents($yu);
if(preg_match('/"video_id": "(.*?)"/', $c, $m) && preg_match('/"t": "(.*?)"/', $c, $m1)){
$id = $m[1];
$t = $m1[1];
$u = "http://www.youtube.com/get_video?video_id=".$id."&t=".$t;
}
?>
<h1>Youtube Url:</h1>
<form method="get" id="form" onSubmit="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="yu" id="yu" size="80" value="<?php echo $yu ?>"/> <input type="submit" name="b" id="b" value="Load" />
</form>
<h1>Prewiew:</h1>
<img src="http://img.youtube.com/vi/<?php echo $id?>/1.jpg" alt="Preview 1" />
<img src="http://img.youtube.com/vi/<?php echo $id?>/2.jpg" alt="Preview 2" />
<img src="http://img.youtube.com/vi/<?php echo $id?>/3.jpg" alt="Preview 3" />
<h1>Downloads:</h1>
<a href="<?php echo $_SERVER['PHP_SELF'].'?u='.urlencode($u)?>">Download FLV</a><br />
<a href="<?php echo $_SERVER['PHP_SELF'].'?fmt=18&u='.urlencode($u)?>">Download MP4</a><br />
<a href="<?php echo $_SERVER['PHP_SELF'].'?fmt=17&u='.urlencode($u)?>">Download 3GP</a>
<?php
}
?>
Please check out my Youtube Video Downloader in Python
Posted in Code Snippets,Php | Trackback Url








9 Responses to “Youtube Video Downloader in PHP”
1
yoenk
December 19th, 2009
Permalink this comment
hi,i upload your file to my site http://boe-yoenk.co.cc/Utube.php i don't change any code.but the Mp4 & 3gp doesn't work. the all download result is flv player..
2
admin
December 19th, 2009
Permalink this comment
I updated the script above. Now it should work for you!!
3
ijul
December 28th, 2009
Permalink this comment
helo I am a beginner, please explain in more detail again. file in the save in what format, index.php?? why I try does not work, and in trying to have yoenk picture does not appear, it just sounds .. help me, http://cinemo.info/utube
4
Thomas
January 15th, 2010
Permalink this comment
I have been looking for a script like this for quite some time.
5
Daniel
March 20th, 2010
Permalink this comment
Thanks a lot , youtube changes Api again, but you script works wonderfull, I´ve been wasting my time in internet for 3 days searching how to solve this issue, you Roksss
6
Kooldude
March 20th, 2010
Permalink this comment
Hi mate,Your demo is working fine.But the source code doesnt work on server.Can you post the working one.Thanks.
7
Fasttrack
March 25th, 2010
Permalink this comment
On my site i don't change any code.but the Mp4 & 3gp doesn't work. the all download result is flv
8
admin
March 26th, 2010
Permalink this comment
I updated the code. Now it works for mp4 and 3gp!!
1 Trackbacks/Pingbacks
Leave a reply