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

An error has occurred. Please try again later.

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

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
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:

PHP
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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 ;
}

Hey, the old script I wrote is not working as youtube constantly changing their way they serve the contents.

I updated googleneedle website with code from here: http://1chris.com/post/39/using-php-to-download-youtube-flvs/

It works now, thank you Chris

 

 

84 Comments , , , , , , , , ,

84 Responses to “Download Youtube Videos using PHP Code”

  1. Michael Kamleitner May 29, 2008 at 4:56 pm #

    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/

  2. mohan June 4, 2008 at 9:44 am #

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

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

  3. Kumar June 4, 2008 at 9:46 am #

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

  4. Jime June 11, 2008 at 8:48 pm #

    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

  5. Mustafa Jalil June 22, 2008 at 1:01 am #

    I got the same error! any solution ?

    Thanks and Regards,
    MJ

  6. Sajith M.R June 22, 2008 at 10:12 am #

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

  7. Jime June 22, 2008 at 11:44 am #

    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

  8. lalit June 30, 2008 at 3:43 pm #

    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

  9. ni July 5, 2008 at 7:56 pm #

    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

  10. Zak July 9, 2008 at 8:20 am #

    Jime,

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

    Zak

  11. Christian August 26, 2008 at 4:23 am #

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

  12. toby August 26, 2008 at 6:09 am #

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

  13. Sayli October 6, 2008 at 12:28 pm #

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

  14. aditya nair October 13, 2008 at 11:49 am #

    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

  15. Sajith M.R October 13, 2008 at 4:50 pm #

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

  16. Navjot Singh November 7, 2008 at 12:56 pm #

    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?

  17. Navjot Singh November 7, 2008 at 1:13 pm #

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

  18. srinivasan December 13, 2008 at 8:31 pm #

    Hi , for this same functionality does any one have samples in Java?? Quick help req!!

  19. domowy budżet January 7, 2009 at 8:44 pm #

    Thanks for the code. I will use it on my site :)

  20. Ekla January 8, 2009 at 12:43 pm #

    Just Now I have downloaded the zip and testing now. First time I was getting some error and warning. But, now I have downloaded 1 flv successfully.

    I tried with the url http://in.youtube.com/watch?v=EBM854BTGL0 .
    But in code default youtube path
    $url = “http://www.youtube.com/watch?v=”.$video_id;
    It works when I changed this to
    $url = “http://in.youtube.com/watch?v=”.$video_id;

    So, I think class “HTTP_Request” have no follow redirection facility. And I shall get same problem if the path become like us. or uk. etc….
    Using libCURL would be better ?
    Any Idea about my opinion?

  21. Youtube Downloader January 16, 2009 at 7:41 am #

    Download YouTube Videos as MP4 Files:

    Just access the website http://www.kucus.com and paste the Youtube RUL then click the “Submit” button to download.

  22. sintu March 2, 2009 at 12:54 pm #

    Hello,

    i want to add a option “Add video” in my project. i am currentally working on symfony frame work with php 5 and my sql data base. so my question is “is this code will work on my web project” if no then plz help me to dounload a video

  23. ghprod March 15, 2009 at 3:28 am #

    thnx for this script

    u r rock man :)

  24. Spidey March 22, 2009 at 5:41 pm #

    wweeeewww amazing……

  25. munawer May 7, 2009 at 1:53 pm #

    HI sajith,

    The youtube downloader you specified here http://www.googleneedle.com is not down loadable link now.Which returns a blank page instead of download.Same situation is happened to me now.i have hosted my code here
    http://munawer.1free.ws/youtube/.
    which is working in local machine perfectly.In server it returns the downloadable Url just as in ur site..

    Any idea!!

    Thanks

  26. Saravanan.K May 14, 2009 at 3:11 pm #

    HI
    Urgent plz help me, thanks in advance

    This youtube download code will be perfectly work on in my local wamp server.But it does not work in my client server.

    I modified the GrabFlvFromYoutube( $pattern ) funaction as

    function GrabFlvFromYoutube( $pattern )
    {
    require_once (“phptube.php”);
    $tube = new PHPTube ();

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

    return $flv_http_path;
    }
    My server link is;

    http://64.94.137.212/eego/SDHK/WIDGET%20API/youtube_download/index.php?url=http://www.youtube.com/watch?v=o3kA7G3hZbg

    i need only the download url. i got it in my local, in server it doesn’t download the file. nothing dispaly if i put url in browser, thanks in advance

  27. Munawer June 1, 2009 at 3:31 pm #

    YouTube Kills Our Video Download Tool
    see here.

    http://cli.110mb.com/k/

  28. Akshay July 29, 2009 at 10:27 pm #

    You can check out the script for free php youtube downloader over here

    http://softloads.in/scripts/ultimate-youtube-grabber-script/

  29. pixy September 4, 2009 at 2:32 pm #

    Thanks for the code! Here is a website where you can easily download YouTube videos in different formats.

  30. Neo Cambell December 16, 2009 at 7:59 pm #

    Thank you very much for sharing this one.

  31. cigarette January 21, 2010 at 11:08 pm #

    Exactly i want! Thank you!

  32. Rara February 8, 2010 at 10:07 am #

    thank you, i will use phpcodedownloading fla youtube

  33. ambitos March 5, 2010 at 12:26 pm #

    Thank you man. I will use your php code for find youtube flv files. Thanks (c# – php :) )

    • AspergerSoft December 31, 2011 at 1:53 am #

      LOL that was my idea too

  34. Fraspi March 18, 2010 at 4:54 pm #

    Your code does not work anymore!

  35. usman March 19, 2010 at 12:19 am #

    not working youtube downloader php script plz fix this script

  36. Tapscott March 19, 2010 at 2:37 am #

    You have a really excellent site! Keep up the good work, I look forward to seeing much more of your posts.

  37. FiFtHeLeMeNt April 7, 2010 at 6:11 pm #

    Seems recently most youtube downloaders have stopped working after youtube has disbaled get_video , I found a site which works , I hope you like it : http://www.youtubeto3gp.com/

  38. keshav April 9, 2010 at 12:38 pm #

    Hi i m using above code but getting error

    Warning: file_get_contents(http://www.youtube.com/get_video?video_id=&t=) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found in E:\xampp\htdocs\vdo\utube\functions.php on line 14

    Warning: file_put_contents(./flvs/3kJ4rJCL5U0.flv) [function.file-put-contents]: failed to open stream: No such file or directory in E:\xampp\htdocs\vdo\utube\functions.php on line 19.

    please tell me how to solve it

  39. Salahudeen April 14, 2010 at 11:53 am #

    The code which is in the download link doesn’t works. Please provide me the code for displaying youtube videos in my site.

  40. chithrareddy April 20, 2010 at 1:17 pm #

    hi,i got this warning i need solution 4 diz plzz… Warning: include(classes/class_company.php) [function.include]: failed to open stream: No such file or directory in /var/www/Invoice_System/user_add.php on line 3

  41. Ram April 21, 2010 at 6:12 pm #

    To Salahudeen,

    http://www.youtube.com/get_video?video_id=&t=…..no longer works. This used to work, but not after google changed it. Try http://www.ngcoders.com . They have updated the scripts.

  42. khalid May 1, 2010 at 12:18 am #

    how it works i need some help in my web site if you like to do help please send me message

  43. Rick May 3, 2010 at 7:16 am #

    I have a domain name “tubethief.com” which I would like to sell. I would be a great domain to use with “php youtube download code” provided here on http://www.sajithmr.me.

    Regards Rick

    Email intrest to rick at tubethief.com

  44. khalid May 13, 2010 at 10:19 am #

    there wrong line code at PHPTube.php
    the line 59 is
    $this->req =& new HTTP_Request($url);
    i don’t know what is mistake but if you use php editor it will show you

    and also i would like some modfication with phpmotion if you can help me please send to my email

    regards

  45. khalid May 16, 2010 at 11:39 pm #

    http://www.youtube.com/get_video?video_id=&t=
    Warning: file_put_contents(./flvs/joelv8HkcIA.flv) [function.file-put-contents]: failed to open stream: No such file or directory in /home/mysite/public_html/addons/massuploader/upload/youtube/functions.php on line 21
    ./flvs/joelv8HkcIA.flv

    i got this error

    cn you help

  46. Salahudeen May 17, 2010 at 2:30 pm #

    To Ram,

    Thanks Ram to give me a way for youtube codes. if u know how to get the thumbnail url of youtube means please help me.

  47. ahmed May 25, 2010 at 2:36 am #

    $url = $_POST['url'];

    $pattern = getPatternFromUrl($url);

    $flv_path = GrabFlvFromYoutube( $pattern );

    echo $flv_path.”";

    ?>

    You tube url:

  48. ahmed May 25, 2010 at 2:36 am #

    tnkyo

  49. abhishek trivedi June 1, 2010 at 11:05 pm #

    Hi,

    I have tried using this code , but i m getting reference errors, i m new to php so please any help will be very appreciated.

    error is like:

    Deprecated: Assigning the return value of new by reference is deprecated in E:\wamp\www\Video_Downloader\phptube.php on line 63

    Thanks,
    Abhishek Trivedi

  50. Fakeheal June 12, 2010 at 7:53 pm #

    It does not work for me. Also I saw that this does not work on the site you’ve given…

  51. ugesh July 6, 2010 at 12:58 pm #

    Sorry! It never work……

  52. Crystal July 30, 2010 at 7:20 am #

    Hi, is the code working now?

  53. Paul August 16, 2010 at 6:30 am #

    youtube changed their site, this won’t work anymore,

  54. Andrew Gee September 13, 2010 at 8:59 pm #

    A new one is here http://www.dumaguetewebdesign.com/youtube.php
    It’s not pretty but it’s working as of 13/9/2010
    It also downloads entire playlists :)

  55. khalid September 23, 2010 at 2:36 pm #

    please i want to transfer from server to server my site http://www.yadady.com/news

  56. Dude October 6, 2010 at 9:57 pm #

    Hi Andrew Gee,

    Thank you very much for the great script. I just tested it today, and I can definitely confirm that it is working. :)

  57. kabelky online October 12, 2010 at 7:47 pm #

    Heh, this shit is so funny. I am going to share this.

  58. fixmer October 13, 2010 at 6:21 am #

    @andrew gee
    witch php version is for your script required? i use 5.* an a lot of errors appear like this one:

    Deprecated: Function ereg_replace() is deprecated in /youtube.php on line 164
    Deprecated: Function ereg_replace() is deprecated in /youtube.php on line 165
    Deprecated: Function eregi() is deprecated in /youtube.php on line 76
    Deprecated: Function eregi() is deprecated in /youtube.php on line 85

  59. Form October 21, 2010 at 9:39 am #

    everyone is addicted to youtube, and everyonle like to upload videos on youtube.;`

  60. Buzzknow November 12, 2010 at 5:06 am #

    any update of this script?

    thanks

  61. neena December 6, 2010 at 1:50 pm #

    does this still works

  62. Free Youtube To Mp3 Converter February 16, 2011 at 5:03 am #

    Can you create an interface for this?

  63. mohsin February 17, 2011 at 3:28 pm #

    download link not working

  64. Hanseman February 20, 2011 at 12:45 am #

    On your website it did not work!
    on my too

  65. undertaker March 4, 2011 at 9:18 pm #

    Used this script a while. Shame, but it stopped working. Seems Youtube changed something?!

    Regards.

  66. Manish June 21, 2011 at 10:19 pm #

    Your Link for downloading script’s zip file is not working.

  67. asim July 20, 2011 at 5:06 am #

    yeah zip file that @andrew Gee Provided is not working. now in 2011..

  68. Chris August 18, 2011 at 8:03 am #

    Hi, I have made my own implementation of this: http://1chris.com/post/39/using-php-to-download-youtube-flvs/

    I hope someone finds it useful.

  69. Chris September 15, 2011 at 6:59 pm #

    The script I linked to in my comment above still works. Youtube did indeed change its code, and because of this many youtube downloader scripts have broken, but I have updated mine to still work.

    • Sajith M.R September 27, 2011 at 9:22 pm #

      Chris, it is much appreciated that you are maintaining your script. Thank you

  70. adnan September 30, 2011 at 1:18 am #

    I have tried phptube class, but its not working for me, the response body is empty, so file isn’t downloading, any idea? .. also when i go to the link generated by phptube, it redirects to 404 / page not found .. any suggestion please?

  71. JD October 27, 2011 at 12:08 am #

    Thanks for this pretty nifty piece of code! Out of curiosity, did you also create www. youtubebuddy. com/? A friend of mine said that it could be yours hehe

  72. smith November 18, 2011 at 10:09 pm #

    hello;
    i am very very amazing with your cod.thank you very much for this.
    but i dont enough learning about php programing.
    can you tell me how to run your cod in my website please.

  73. john December 28, 2011 at 4:14 am #

    Hiya,
    Is your code still working? I’ll tell you what I did. I have this program called Zennoposter, with which I created a step where I went to a youtube page that has a video. I then created another step consisting of own code, where I told the template to go to a page on localhost where I have your php script written in, because in Zennoposter you’re supposed to run code in a safe area, as in point it to a page with the code, I assume, so I thought localhost on Xampp should be ideal for this. Now, I may be doing it wrong with Zennoposter, but maybe the code is broken, so I’m double checking and seeing if everything is OK your end? John.

  74. shan January 2, 2012 at 10:48 pm #

    This code is working in My local webserver
    But when Uploaded to my Live server, Nothing Happens!!

    More over you’ll get 3 Diff types of file @ following index of $flvs
    $flvs[1] Download Flv
    $flvs[3] Download Flv Small Sized
    $flvs[2] Download Mp4

    • shan January 8, 2012 at 2:33 pm #

      Yes….. Its Working … Still working fine….. I Changed the code little bit, the script will send the video file directly to the browser

  75. new programs January 12, 2012 at 12:24 am #

    Hi, Neat post. There is an issue together with your site in web explorer, could test this? IE still is the market leader and a good component of other people will omit your magnificent writing due to this problem.

  76. Shubas February 28, 2012 at 6:21 pm #

    Hey, thanks to you and Chris for this code. It works beautifully. However didn’t see any link to download the “download.php”. Are you going to share that too? Chriss’ code works just like it says “locally” and I dont have a *NIX box with me to test. I have a hosted apache server, where I want to try this. I am able to understand what needs to be w.r.t. to modfying the download.php to prompt to the user for the file location, but not good enough in PHP to write the code. Any help will be apprreciated.

  77. Duke May 13, 2012 at 6:33 pm #

    I have created a fully functional website where Youtube videos are downloaded then converted in mp3 files without all these codes. In php is simple.

Trackbacks/Pingbacks

  1. PHP Code for Downloading Youtube Videos | - May 4, 2009

    [...] reading here: PHP Code for Downloading Youtube Videos | Share and [...]

  2. Komplette Youtube Playlist downloaden - IO – Zone - January 23, 2010

    [...] Videofiles bietet die API nicht an. Da habe ich mir die paar Codezeilen von Michael Kamleitner und Sajith M.R zu Nutze [...]

Leave a Reply

More in php, php source code (62 of 111 articles)


Resize a Photo >> Today we will implement hows to resize photo into a particular width and height (or ...