Download Youtube Videos using PHP Code
Posted on 29. May, 2008 by Sajith M.R in php, php source 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:



Michael Kamleitner
May 29th, 2008
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/
mohan
Jun 4th, 2008
Nice Info ! I am looking for it! thanks For the download !!
Regards.,
http://itbuddy.blogspot.com
Kumar
Jun 4th, 2008
Wow nice Techie Blog ! See my techie blog http://itbuddy.blogspot.com if you are interested ! Nice Site !
Jime
Jun 11th, 2008
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
Mustafa Jalil
Jun 22nd, 2008
I got the same error! any solution ?
Thanks and Regards,
MJ
Sajith M.R
Jun 22nd, 2008
Download again, and Try. The Error is Corrected
Jime
Jun 22nd, 2008
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
lalit
Jun 30th, 2008
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
ni
Jul 5th, 2008
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
Zak
Jul 9th, 2008
Jime,
You will get that error if you try to use php4. You must use php5.
Zak
Christian
Aug 26th, 2008
thanks very helpful, i was just about to pull my hair off
toby
Aug 26th, 2008
let me try this script.. thanks and more power to you..
Sayli
Oct 6th, 2008
This code really helped me to download youtube videos. Thanks a lot.
aditya nair
Oct 13th, 2008
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
Sajith M.R
Oct 13th, 2008
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..
Navjot Singh
Nov 7th, 2008
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?
Navjot Singh
Nov 7th, 2008
No problem…found another script…also code was messed up in my last comment.
srinivasan
Dec 13th, 2008
Hi , for this same functionality does any one have samples in Java?? Quick help req!!
domowy budżet
Jan 7th, 2009
Thanks for the code. I will use it on my site
Ekla
Jan 8th, 2009
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?
Youtube Downloader
Jan 16th, 2009
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.
sintu
Mar 2nd, 2009
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
ghprod
Mar 15th, 2009
thnx for this script
u r rock man
Spidey
Mar 22nd, 2009
wweeeewww amazing……
PHP Code for Downloading Youtube Videos |
May 4th, 2009
[...] reading here: PHP Code for Downloading Youtube Videos | Share and [...]
munawer
May 7th, 2009
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
Saravanan.K
May 14th, 2009
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
Munawer
Jun 1st, 2009
YouTube Kills Our Video Download Tool
see here.
http://cli.110mb.com/k/
Akshay
Jul 29th, 2009
You can check out the script for free php youtube downloader over here
http://softloads.in/scripts/ultimate-youtube-grabber-script/
pixy
Sep 4th, 2009
Thanks for the code! Here is a website where you can easily download YouTube videos in different formats.
Neo Cambell
Dec 16th, 2009
Thank you very much for sharing this one.
cigarette
Jan 21st, 2010
Exactly i want! Thank you!
Komplette Youtube Playlist downloaden - IO – Zone
Jan 23rd, 2010
[...] Videofiles bietet die API nicht an. Da habe ich mir die paar Codezeilen von Michael Kamleitner und Sajith M.R zu Nutze [...]
Rara
Feb 8th, 2010
thank you, i will use phpcodedownloading fla youtube
ambitos
Mar 5th, 2010
Thank you man. I will use your php code for find youtube flv files. Thanks (c# – php
)