Night | Snow

Youtube Video Downloader in PHP

Posted on: December 4th, 2009

Tags: , ,

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.


View Demo


Here’s the source:

<?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.="&amp;fmt=17";
    }
    elseif($_GET['fmt'] == 18){ // mp4
        header("Content-Type: video/mp4");  
        header("Content-Disposition: attachment; filename=\"video.mp4\"");  
        $v.="&amp;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

News

  • Facebook shares see modest debut

    Facebook shares end a volatile first day of trading at $38.23, barely above the company's initial pricing, having initially jumped more than 10%.

  • Met Police to extract phone data

    Mobile phone data of suspects in police custody is to be extracted and retained, regardless of whether charges are brought, the BBC has learned.

  • Silicon trick for next-gen memory

    Researchers reveal details of a promising way to make a fundamentally different kind of computer memory chip.

Martin Fischer said:

Client and partner of simra.ch

Wir können Simon Schärer als kompetenter und zuverlässiger Programmierer höchst weiterempfehlen. Er ist nicht nur ein guter Programmierer, sondern auch ein ausgezeichneter Designer.
Danke Developer’s Island

Follow us on Twitter

favorite

I get asked a lot - which SDK should we use for web programming? I recommend Netbeans

my toolbar

Simra GameMP3 playersubscribe to RSSMy delicious bookmarksFacebookTwitterE-mail

9 Responses to “Youtube Video Downloader in PHP”

RSS Feed Icon  Subscribe to comments follow-up

yoenk's Avatar

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..

ijul's Avatar

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

Thomas's Avatar

4

Thomas

January 15th, 2010
Permalink this comment

I have been looking for a script like this for quite some time.

Daniel's Avatar

5

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

Kooldude's Avatar

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.

Fasttrack's Avatar

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

Leave a reply

You can use these tags:
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

Latest Tweet

loading...