Youtube Video Downloader Script

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
smstromb
Forum Newbie
Posts: 6
Joined: Thu May 21, 2009 1:42 pm

Youtube Video Downloader Script

Post by smstromb »

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:

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;
    
}
 
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.
User avatar
iankent
Forum Contributor
Posts: 333
Joined: Mon Nov 16, 2009 4:23 pm
Location: Wales, United Kingdom

Re: Youtube Video Downloader Script

Post by iankent »

Why are you using file_get_contents? Surely you just need to extract the video id from the original URL and insert it into the new URL?

e.g. given URL http://www.youtube.com/watch?v=abcdef

Code: Select all

 
$pos = strrpos($url, '=');
if($pos) {
    $video_id = substr($url, $pos + 1);
}
$link = "http://www.youtube.com/get_video?video_id=$video_id";
 
edit: ignore me sorry, just realised you need to get the hash etc. :banghead:
smstromb
Forum Newbie
Posts: 6
Joined: Thu May 21, 2009 1:42 pm

Re: Youtube Video Downloader Script

Post by smstromb »

I've discovered the issue and it appears to be youtube related. Apparently the video hash corresponds to the IP address that requests it, so using the link on another machine with a different IP won't work. This is why it only works on my localhost machine and not my servers, and why the url generated by the script on my localhost will not work for anyone else that tried it.
Post Reply