And again i searched the internet for some “easy” script to grab/download youtube movies to my own server.
I failed! So wrote my own.
Let me explain the steps or jump over to the VERY short working code:
First step we need to do 3 things:
*1: make a request to youtube for getting the required data
*2: make a second request to really get the .FLV file
*3: save the data to a file
*1: make a request to the url, in this case it is:
(just grabbed a url dont look at the content of it
)
*2: Grab the source and strip it because we need a hash and a video id
(the video id could also be catched from the above url)
video_id = 22bS3WLWPpE (speak for itself)
t hash = OEgsToPDskK7nRQvAd4WaR2IXy0TY1mr ( different wich each request)
Basicly i didnt found out about t hash… some others did it.
Some guys say that this hash selects the right server to get the movie from.
Some others say that it is just for security reasons…
*3: just save the data to a file
Well this is what i came up with (COULDNT BE EASIER!).
<?php
//REMORSE.NL! goz the world should be free!
//this script creates a file called test.flv,
//base on a youtube url,
//in the directory where this script is
/*
* set parameters
*/
$url = "http://www.youtube.com/watch?v=22bS3WLWPpE";
//video_id now hardcoded... but should here be grabbed from above url
$video_id = '22bS3WLWPpE';
//this will be the name of the file that will be created
$video_filename = 'test.flv';
/*
* first call
*/
$curl = new Curl($url);
$page = $curl->curl_get();
/*
* get the stuff we need from the source
*/
//look for video id... could also be grabbed from above video id..
preg_match('/"video_id": "(.*?)"/', $page, $match);
$var_id = $match[1];
//look for the t hash in the source...
preg_match('/"t": "(.*?)"/', $page, $match);
$var_t = $match[1];
//build the new url
$url = "";
$url .= $var_id;
$url .= "&t=";
$url .= $var_t;
$url = "http://www.youtube.com/get_video?video_id=".$url;
/*
* make the new request...
*/
$curl = new Curl($url);
$fileContent = $curl->curl_get();
//write to the file
$curl->writeToFile($fileContent,$video_filename);
class Curl{
// Vars, basic:
var $curlInstance = NULL;
var $url = NULL;
// Constructor:
function Curl($url)
{
//set the url
$this->url = $url;
//initialize
$this->curlInstance = curl_init();
//set some options
curl_setopt($this->curlInstance, CURLOPT_URL, $this->url);
curl_setopt($this->curlInstance, CURLOPT_TIMEOUT, 25);
curl_setopt($this->curlInstance, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($this->curlInstance, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($this->curlInstance, CURLOPT_HTTPHEADER, array("Content-type: app/xml"));
curl_setopt($this->curlInstance, CURLOPT_FOLLOWLOCATION, TRUE);
}
function curl_get()
{
//execute the curl
$returningData = curl_exec($this->curlInstance);
//close the curl
curl_close($this->curlInstance);
//need to say more?
unset($this->curlInstance);
return $returningData;
}
//just write the file
function writeToFile($data,$video_filename){
$video_filename = dirname(__FILE__).DIRECTORY_SEPARATOR.$video_filename;
if ($o = fopen ($video_filename, "w")) {
fwrite($o,$data);
fclose ($o);
print "Done writing to: ".$video_filename;
}else{
print 'ERROR';
}
}
}
?>
Soon I will release a nice php class that can do things u havent seen yet….
If you like the code or need help please leave a comment.
January 28th, 2009 on 17:38
Hi,
works fine, thank you very much.
did you released new version?
Regards,
Necat Kutlar
February 11th, 2009 on 18:57
Thanks for leaving a comment!
New release is not yet done..
October 8th, 2009 on 16:42
An easy way to save youtube vids is to prepend ‘pwn’ to ‘youtube.com’ e.g. http://www.pwnyoutube.com/watch?v=22bS3WLWPpE
From there you can save the flv and an mp4.