Youtube Video Downloader in PHP

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.

Simply copy the code below and paste on your website and start downloading youtube videos. Enjoy!

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
<?php
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=M5k_U_e9r9k';
    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;
    }
?>
<center>
<h1>Youtube Url:</h1>
<form method="get" id="form" onSubmit="<?php echo $_SERVER['PHP_SELF']?>">
<input type="text" name="yu" id="yu" style="width:400px" value="<?php echo $yu ?>"/>
<input type="hidden" name="service" id="service" style="width:400px" value="<?php echo substr(__FILE__,strrpos(str_replace("\\","/",__FILE__),"/")+1)?>"/> <input type="submit" name="b" id="b" value="Load" />
</form>
<br />
 
<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" />
<br /><br />
<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>
</center>
<?php
}
?>

Check it out

Share this

The Author

admin - Hello, my name is Simon, I’am 25 and I’m a software and web developer. My interests span all aspects of programming and development, including ASP, JavaScript, AJAX, PHP, .NET, Java, C#, C++, Objectv-C, Flash, Cocoa, SQL, XML and everything in between. www.simra.ch

Leave a Reply

 
© 2010 onYou GmbH. All Rights Reserved