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
|
|
|
|
If you're new here, you may want to subscribe to my RSS feed. Thanks for visiting!


May 29th, 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/
June 4th, 2008 at 9:44 am
Nice Info ! I am looking for it! thanks For the download !!
Regards.,
http://itbuddy.blogspot.com
June 4th, 2008 at 9:46 am
Wow nice Techie Blog ! See my techie blog http://itbuddy.blogspot.com if you are interested ! Nice Site !
June 11th, 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
June 22nd, 2008 at 1:01 am
I got the same error! any solution ?
Thanks and Regards,
MJ
June 22nd, 2008 at 10:12 am
Download again, and Try. The Error is Corrected
June 22nd, 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
June 30th, 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
July 5th, 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
July 9th, 2008 at 8:20 am
Jime,
You will get that error if you try to use php4. You must use php5.
Zak
August 26th, 2008 at 4:23 am
thanks very helpful, i was just about to pull my hair off
August 26th, 2008 at 6:09 am
let me try this script.. thanks and more power to you..