Tales of a Software Craftsman

PhpTube: A PHP Class to get download links from Youtube Watch URLs

THE API HAS BEEN BROKEN FOR A LONG TIME, AND I DON’T WANT TO FIX IT SINCE THERE ARE BETTER ALTERNATIVES LIKE “youtube-dl”.

Let me get to it straight: I wrote a php class that takes a youtube watch url (the typical url to watch a video, the url has a “watch” GET parameter in it) and returns the download links for different available formats of the video. Please note that these links will be valid for the requesting IP address. That is you can not download the videos from other IP addresses except the only one IP adress that actually originated the download request. So if you put it on your server and send these links to your users, they’ll certainly not be able to download from these links since their IP addresses will be different from your server IP address. In that case, you should first download it to your server and then link to those downloaded videos.

I think if you add the video to a playlist on your youtube account, the video remains cached in youtube servers for your account. So if you can generate the links from the IP you logged in, it may not expire. I’m not sure, but I think I read something like this before.

cURL is more acceptable to me than file_get_contents() because many hosts have fopen() disabled for urls. In such cases, file_get_contents() shall not work. But I like your solution, but put that file_get_contents() in another if…else loop to to check for data, if it fails, throw an exception.

The videos on that channel might have something different that is breaking the entire thing. I am sorry, I can’t really afford to look into the issue right now. May be when I get some time. I of course welcome anyone interested in sniffing the youtube html and fixing the issue. Proper credits will be given 🙂

Woah! I’m really loving the template/theme of this site. It’s simple,
yet effective. A lot of times it’s very difficult to get that “perfect balance” between superb usability and appearance. I must say that you’ve done a great job with this.
In addition, the blog loads very fast for me on Chrome.

Great post. I was checking constantly this blog and I’m inspired! Very helpful information particularly the remaining phase 🙂 I take care of such information a lot. I used to be seeking this particular information for a long time. Thank you and best of luck.

I tried to ran the sample script but it didn’t work. I tried to print the array to analyze the output. However, the array is empty.

Array ( [0] => Array ( ) [1] => Array ( ) )

I also tried to write my own script too. Below is a sample code:

$url = ‘http://youtube.com/get_video_info?video_id=’.$id;

$content = file_get_contents($url);

Then I parse the contents to get the direct link. However, for some videos (such as video ID: a1Nmb7OEpQA) this method also does not work. I mean, it produces the link but that link not downloadable or playable. Do you know what could be the reason?