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


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/
Nice Info ! I am looking for it! thanks For the download !!
Regards.,
http://itbuddy.blogspot.com
Wow nice Techie Blog ! See my techie blog http://itbuddy.blogspot.com if you are interested ! Nice Site !
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
I got the same error! any solution ?
Thanks and Regards,
MJ
Download again, and Try. The Error is Corrected
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
i find arror
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
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
Jime,
You will get that error if you try to use php4. You must use php5.
Zak
thanks very helpful, i was just about to pull my hair off
let me try this script.. thanks and more power to you..
This code really helped me to download youtube videos. Thanks a lot.
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
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..
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?
No problem…found another script…also code was messed up in my last comment.
Hi , for this same functionality does any one have samples in Java?? Quick help req!!
Thanks for the code. I will use it on my site
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?
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.
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
thnx for this script
u r rock man
wweeeewww amazing……
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
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
YouTube Kills Our Video Download Tool
see here.
http://cli.110mb.com/k/
You can check out the script for free php youtube downloader over here
http://softloads.in/scripts/ultimate-youtube-grabber-script/
Thanks for the code! Here is a website where you can easily download YouTube videos in different formats.
Thank you very much for sharing this one.
Exactly i want! Thank you!
thank you, i will use phpcodedownloading fla youtube
Thank you man. I will use your php code for find youtube flv files. Thanks (c# – php
)
LOL that was my idea too
Your code does not work anymore!
not working youtube downloader php script plz fix this script
You have a really excellent site! Keep up the good work, I look forward to seeing much more of your posts.
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/
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
The code which is in the download link doesn’t works. Please provide me the code for displaying youtube videos in my site.
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
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.
how it works i need some help in my web site if you like to do help please send me message
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
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
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
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.
$url = $_POST['url'];
$pattern = getPatternFromUrl($url);
$flv_path = GrabFlvFromYoutube( $pattern );
echo $flv_path.”";
?>
You tube url:
tnkyo
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
It does not work for me. Also I saw that this does not work on the site you’ve given…
Sorry! It never work……
Hi, is the code working now?
youtube changed their site, this won’t work anymore,
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
please i want to transfer from server to server my site http://www.yadady.com/news
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.
Heh, this shit is so funny. I am going to share this.
@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
everyone is addicted to youtube, and everyonle like to upload videos on youtube.;`
any update of this script?
thanks
does this still works
Can you create an interface for this?
download link not working
On your website it did not work!
on my too
Used this script a while. Shame, but it stopped working. Seems Youtube changed something?!
Regards.
Your Link for downloading script’s zip file is not working.
yeah zip file that @andrew Gee Provided is not working. now in 2011..
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.
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.
Chris, it is much appreciated that you are maintaining your script. Thank you
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?
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
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.
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.
Get New Video Downloader Script…
Check Demo–
http://www.pcyoutube.com
Just add pc before Youtube
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
Yes….. Its Working … Still working fine….. I Changed the code little bit, the script will send the video file directly to the browser
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.
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.
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.