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.
You can download the source codes from my github repo: https://github.com/masnun/phptube 🙂
An example:
1 2 3 4 5 |
<?php require_once 'PhpTube.php'; $tube = new PhpTube(); var_dump($tube->getDownloadLink("http://www.youtube.com/watch?v=sesOnXMcaBk&feature=channel")); ?> |
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
array 0 => array 'ext' => string 'flv' (length=3) 'type' => string 'Low Quality' (length=11) 'url' => string 'http://o-o.preferred.btcl-dac1.v15.lscache7.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909702%2C912503&algorithm=throttle-factor&itag=5&ipbits=8&burst=40&sver=3&signature=69B3EB0AC93CA358375F6C56384CFB82314E8CFF.060F0B630A23F3DB1E07C173E1804D8059439D45&expire=1305637200&key=yt1&ip=180.0.0.0&factor=1.25&id=b1eb0e9d731c6819' (length=383) 1 => array 'ext' => string 'flv' (length=3) 'type' => string 'High Quality (320p)' (length=19) 'url' => string 'http://o-o.preferred.btcl-dac1.v13.lscache5.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909702%2C912503&algorithm=throttle-factor&itag=34&ipbits=8&burst=40&sver=3&signature=4F52B91CBD47F3B048FFF72C7C8235CF8A5E2020.540EF6713BFD929EBA8FF7C3645BB9A367D716CF&expire=1305637200&key=yt1&ip=180.0.0.0&factor=1.25&id=b1eb0e9d731c6819' (length=384) 2 => array 'ext' => string 'mp4' (length=3) 'type' => string 'High Quality (480p)' (length=19) 'url' => string 'http://o-o.preferred.btcl-dac1.v13.lscache6.c.youtube.com/videoplayback?sparams=id%2Cexpire%2Cip%2Cipbits%2Citag%2Calgorithm%2Cburst%2Cfactor&fexp=909702%2C912503&algorithm=throttle-factor&itag=18&ipbits=8&burst=40&sver=3&signature=30BCE104E535CEC7543D1CE859785C4927E96156.6B7EFB893086BBBDA499487F0720A744BB3FC5F2&expire=1305637200&key=yt1&ip=180.0.0.0&factor=1.25&id=b1eb0e9d731c6819' (length=384) |
41 replies on “PhpTube: A PHP Class to get download links from Youtube Watch URLs”
Do you think these download link expires in a timely manner? Because the sample download urls are not working at my end.
Yes, the download URLs have an “expiry” part. And also, these links will not work for anyone else. These were generated from my IP address.
Is there any need to implement OOP ? 😛
OOP is good programming practice 🙂
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.
Thanks for the information 🙂
Does it work now ? I’m getting string(46) “An error ocurred: Error Locating Downlod URL’s”
It used to work, can you please send me the URL you’re trying to download so that I can check (& fix)? 🙂
ইউটিউব ডট কম watch?v=8wxOVn99FTE
For some reasons the video doesn’t have any other formats specified. So it has no download candidates. I have added a meaningful error message in such cases. Thanks for the input.
Is there any need to use the curl ?
private function _getHtml($url)
{
if (function_exists("curl_init")) {
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
return curl_exec($ch);
} else {
// throw new Exception("No cURL module available");
return file_get_contents($url);
}
}
How’s that ?
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.
If you make a pull request on github, I shall pull it in 🙂
An error ocurred: Error Locating Download URL’s I think smth were changed on youtube.com((((
when i download the video and changing the name to a.flv and tries to open it with media player or vlc its says occurred file
This works great! For most videos…
I was testing this with multiple videos and it worked great on all of them, until I got to this video:
http://www.youtube.com/watch?v=MCWJUSulnro
It doesn’t work with any video on this channel that I’ve tested:
http://www.youtube.com/user/gearboxsoftware
Is there a fix for that or have they blocked it?
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 🙂
I think I found the problem, they are age restricted.
Thanks for the input, any workarounds?
Not that I know of. :/
You could potentially pass the session ID from the browser through cURL. Once logged in, the age restriction should go away.
I tested to download the videos on that channel using my own PHP class and had no issues at all. Downloaded several videos successfully. You can find my class at GitHub: https://github.com/eyecatchup/php-yt_downloader
Greetz!
[…] have download phptube from http://www.masnun.me/2011/05/17/phptube-a-php-class-to-get-download-links-from-youtube-watch-videos….. It’s working but when I download video from this generate link it said that “I […]
Hello,
How can i use the links?? it’s telling me 403 forbidden even though i’m using the same ip.
How can i fix this please
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.
Excellent Blog!
Thanks! 🙂
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.
Can you please fix the PhpTube class? :[
Your class error , please fix it
Thanks
I’m getting an empty array…
Hello Sir
Can u tell me how they did it?
They fetched youtube source video link
http://www.animefun.com/dl/googDevhd.php?url=/111844977409367164673/Nship381ut
http://www.animefun.com/dl/googDevhd.php?url=/111844977409367164673/Nship380ut
They might have used some other solutions.
I have an error when get link download from this link https://www.youtube.com/watch?v=t7K2kXijROc
Hey bro how are you.. i like to tell you that please write a script that when user click on Download Button then link generate the download link in 720p
Hi bro, I don’t like to write script for other people… Please use “youtube-dl”.
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?
The script is broken for a long time. I didn’t get time to update it.
your script doesnt works dude 🙁
Please read the bold text at the top of the post.
Masnun bro how to access array index 2 for mp4 file to use it in the HTML Player to play video please send me code thanks.
not working can you please help me?
your code is bugs #Output Notice: Undefined offset: 0 in E:\xampp\htdocs\apitest\PhpTube.php on line 23array(0) { }