Download Youtube Videos using PHP Code
- 17 Comment

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:
17 Comments on this post
Trackbacks
-
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 -
mohan said:
Nice Info ! I am looking for it! thanks For the download !!
Regards.,
http://itbuddy.blogspot.comJune 4th, 2008 at 9:44 am -
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 -
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 -
Mustafa Jalil said:
I got the same error! any solution ?
Thanks and Regards,
MJJune 22nd, 2008 at 1:01 am -
Sajith M.R said:
Download again, and Try. The Error is Corrected
June 22nd, 2008 at 10:12 am -
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 49June 22nd, 2008 at 11:44 am -
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 19June 30th, 2008 at 3:43 pm -
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.flvJuly 5th, 2008 at 7:56 pm -
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 -
Christian said:
thanks very helpful, i was just about to pull my hair off
August 26th, 2008 at 4:23 am -
toby said:
let me try this script.. thanks and more power to you..
August 26th, 2008 at 6:09 am -
Sayli said:
This code really helped me to download youtube videos. Thanks a lot.
October 6th, 2008 at 12:28 pm -
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 MessageWarning: 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.flvOctober 13th, 2008 at 11:49 am -
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 -
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 -
Navjot Singh said:
No problem…found another script…also code was messed up in my last comment.
November 7th, 2008 at 1:13 pm




