May 29 2008

Download Youtube Videos using PHP Code

If you want to get the FLV file of any youtube video url using php code, here is the solution.

If you are a PHP Programmer and if you are working with any video website, and if you need to grab videos (FLV files) from youtube and to put it yourown site (not object embedding) , Download the Full Source Code given at the end of this article (ZIP)

I used this php code for the Youtube video download tool http://www.googleneedle.com


Here function getPatternFromUrl is nothing but, get the exact pattern of a particular video from any youtube video url format.

In the above case , it returns pzmP4UvZRa4

The function is below

function getPatternFromUrl($url)

 {

$url = $url.'&';

$pattern = '/v=(.+?)&+/';

preg_match($pattern, $url, $matches);

return ($matches[1]);

}

GetFlvFromYoutube is the main function here, which download the flv file from youtube pattern and saves to your local machine.
The function is below:

function GrabFlvFromYoutube( $pattern )
{

 require_once ("phptube.php");

 $tube = new PHPTube ();

 $flv_http_path = $tube->download($pattern) ;

 echo $flv_http_path;

 set_time_limit(0);

 $data = file_get_contents($flv_http_path);

 $new_flv_path = dirname(_FILE_).'/flvs/'.$pattern.'.flv' ;

 file_put_contents($new_flv_path, $data);

 return $new_flv_path ;

}

Download the fullsource code from this link given:

http://www.sajithmr.com/downloads/youtube-download-php.zip

17 Comments on this post

Trackbacks

  1. Michael Kamleitner said:

    thx for using phptube! a link back to my original work would be appreciated, thx!

    http://nonsmokingarea.com/blog/2007/06/16/phptube-youtube-api-for-video-upload-download/

    May 29th, 2008 at 4:56 pm
  2. mohan said:

    Nice Info ! I am looking for it! thanks For the download !!

    Regards.,
    http://itbuddy.blogspot.com

    June 4th, 2008 at 9:44 am
  3. Kumar said:

    Wow nice Techie Blog ! See my techie blog http://itbuddy.blogspot.com if you are interested ! Nice Site !

    June 4th, 2008 at 9:46 am
  4. Jime said:

    I have tested the code, but i find it can not work?

    Fatal error: Cannot instantiate non-existent class: http_request in XXXX/test/phptube.php on line 59

    June 11th, 2008 at 8:48 pm
  5. Mustafa Jalil said:

    I got the same error! any solution ?

    Thanks and Regards,
    MJ

    June 22nd, 2008 at 1:01 am
  6. Sajith M.R said:

    Download again, and Try. The Error is Corrected :)

    June 22nd, 2008 at 10:12 am
  7. Jime said:

    I have tested the code again, but i find it still not work?
    Parse error: parse error, unexpected T_STRING, expecting T_OLD_FUNCTION or T_FUNCTION or T_VAR or ‘}’ in XXXXXX/test/Request.php on line 49

    June 22nd, 2008 at 11:44 am
  8. lalit said:

    I have tested the code and get the following error, any solution?
    Fatal error: Call to undefined function: file_put_contents() in /XXXXX/youtube-download-php/functions.php on line 19

    June 30th, 2008 at 3:43 pm
  9. ni said:

    I tested the code with neither getting an error nor an usefull download link

    that was an result
    http://www.youtube.com/get_video?video_id=krBXrzk7s-E&t=OEgsToPDskIkst6hx14l1qjVyUt5EIkL./flvs/krBXrzk7s-E.flv

    July 5th, 2008 at 7:56 pm
  10. Zak said:

    Jime,

    You will get that error if you try to use php4. You must use php5.

    Zak

    July 9th, 2008 at 8:20 am
  11. Christian said:

    thanks very helpful, i was just about to pull my hair off

    August 26th, 2008 at 4:23 am
  12. toby said:

    let me try this script.. thanks and more power to you..

    August 26th, 2008 at 6:09 am
  13. Sayli said:

    This code really helped me to download youtube videos. Thanks a lot.

    October 6th, 2008 at 12:28 pm
  14. aditya nair said:

    Hi Sajith ;

    I downloaded the Code and hosted it online.
    But I get the following Errors.

    http://www.youtube.com/get_video?video_id=4ahyHfNqquQ&t=OEgsToPDskKM-mv5HqJ4v5wxk8gyY4sA
    PHP Error Message

    Warning: set_time_limit() has been disabled for security reasons in /home/a3272311/public_html/youtubedownloaderphp/functions.php on line 13

    Free Web Hosting

    PHP Error Message

    Warning: file_put_contents(./flvs/4ahyHfNqquQ.flv) [function.file-put-contents]: failed to open stream: No such file or directory in /home/a3272311/public_html/youtubedownloaderphp/functions.php on line 19

    Free Web Hosting
    ./flvs/4ahyHfNqquQ.flv

    October 13th, 2008 at 11:49 am
  15. Sajith M.R said:

    To solve : set_time_limit() >> Got to your php.ini file and make the safe mode off. SAFE_MODE = Off

    Make the root dir full permission 777

    Thats all..

    October 13th, 2008 at 4:50 pm
  16. Navjot Singh said:

    Now I want to use your plugin to achieve the following:

    I have a blog which shows videos from youtube. Now I want to autogenerate download links for each video beneath the post. This is what I did:

    1. Uploaded the folder in blog’s root directory.
    2. Opened header.php of theme file and put:
    3. Opened single.php and did the following:
    $url = get_post_meta($post->ID, ‘url’, true);
    $pattern = getPatternFromUrl($url);

    and for generating the download link did:

    <a href=”" target=”_blank”>Download Video

    Now where am I wrong since it says undefined function: getPatternFromUrl

    AM I including the wrong file?

    Can you help me out?

    November 7th, 2008 at 12:56 pm
  17. Navjot Singh said:

    No problem…found another script…also code was messed up in my last comment.

    November 7th, 2008 at 1:13 pm

LEAVE A COMMENT

Subscribe Form

Subscribe to Blog

Sponsors

    Advt on sajithmr.com
    Advt on sajithmr.com
    Itslife Online
    Advt on sajithmr.com

Recent Comments

  • Binny V A: Thanks for the post - love the picture ;-)
  • TheAnand: A lot of people are seeing errors with google video chat….is there any other software which has to be...
  • Alex: Wow, interresting analysis you have done! I’m trying to run GoogleVoiceAndVideoSetup on linux, using...
  • Mella Fitriansyah: Nice plugin, I will try to add this plugin in my blog…
  • Mella Fitriansyah: Nice Info, Sir i will reading your another post success for you

Recent Readers

JOIN MY COMMUNITY!

Recent Posts