Youtube Video Downloader Script
Posted: Thu Nov 19, 2009 5:52 pm
I'm writing a php function that when given a url to a youtube video, returns a new url to download the video in mp4 format. For those of you who don't know how youtube videos work you can access the mp4 version of a youtube video with the following URL format:
Given the url: http://www.youtube.com/watch?v=(VIDEO_ID)
MP4 Found at: http://www.youtube.com/get_video?video_ ... SH)&fmt=18
The VIDEO_HASH is embedded in the html of the youtube video page and I created a php script that grabs the VIDEO_ID and VIDEO_HASH from the youtube page.
Here is my code:
Here's the output:
From localhost: http://www.youtube.com/get_video?video_ ... %3D&fmt=18
From server: http://www.youtube.com/get_video?video_ ... %3D&fmt=18
The VIDEO_HASH changes every time I run the script on both localhost and the server, which is normal. However, the url given by the localhost gives me a proper download link while my server doesn't. I can't for the life of my figure out why it won't give me a correct download link. I've tried the script on 3 separate web servers and they all generate incorrect download urls so for some reason it only works on my localhost.
Can anyone help me to think of what's going wrong here?
Also, I've tried CURL with the same results, works on local host but not on a web server.
Given the url: http://www.youtube.com/watch?v=(VIDEO_ID)
MP4 Found at: http://www.youtube.com/get_video?video_ ... SH)&fmt=18
The VIDEO_HASH is embedded in the html of the youtube video page and I created a php script that grabs the VIDEO_ID and VIDEO_HASH from the youtube page.
Here is my code:
Code: Select all
function getDownloadLink($url){
$html = file_get_contents($url);
if(!preg_match('/"video_id": "(.*?)"/', $html, $match) || !preg_match('/"t": "(.*?)"/', $html, $match1))
{
return false;
}
$var_id = $match[1];
$var_t = $match1[1];
$link = "http://www.youtube.com/get_video?video_id=".$var_id."&t=".$var_t."&fmt=18";
return $link;
}
From localhost: http://www.youtube.com/get_video?video_ ... %3D&fmt=18
From server: http://www.youtube.com/get_video?video_ ... %3D&fmt=18
The VIDEO_HASH changes every time I run the script on both localhost and the server, which is normal. However, the url given by the localhost gives me a proper download link while my server doesn't. I can't for the life of my figure out why it won't give me a correct download link. I've tried the script on 3 separate web servers and they all generate incorrect download urls so for some reason it only works on my localhost.
Can anyone help me to think of what's going wrong here?
Also, I've tried CURL with the same results, works on local host but not on a web server.