YouTube is the most popular video sharing platform and videos is watched online. However, sometimes we have a need to get the video for offline uses or you don’t want the watermark of YouTube on the video or even you want to use YouTube as a free video host and YouTube didn’t provide any ways to get the raw video. So in this post, I’m teach you how to do it in PHP (so you can embed the video easily in your website).
By reverse engineering the YouTube embed player, we know that the player will issue a HTTP request to http://youtube.com/get_video_id?video_id=THE_VIDEO_ID
to get information about a specific video. The result of the request contains a URL-encoded string that has the video’s location. So, we need to get that part first.
<?php $id = $_GET['id']; //the youtube video ID $format = $_GET['fmt']; //the MIME type of the video. e.g. video/mp4, video/webm, etc. parse_str(file_get_contents("http://youtube.com/get_video_info?video_id=".$id),$info); //decode the data $streams = $info['url_encoded_fmt_stream_map']; //the video's location info
The stream is also different sets of URL-encoded data separated by a comma. In order to retrieve all the streams, we need to turn it into an array by using explode(',',$streams)
.
$streams = explode(',',$streams);
After that, we need to loop through all the streams. Now it goes to the main part: select an appropriate stream and get its data.
foreach($streams as $stream){ parse_str($stream,$data); //decode the stream if(stripos($data['type'],$format) !== false){ //We've found the right stream with the correct format $video = fopen($data['url'].'&signature='.$data['sig'],'r'); //the video $file = fopen('video.'.str_replace($format,'video/',''),'w'); stream_copy_to_stream($video,$file); //copy it to the file fclose($video); fclose($file); echo 'Download finished! Check the file.'; break; } }
The script is finished. Now you can call it like this after you’ve put it to the server:
http://localhost/THE SCRIPT NAME.php?id=THE YOUTUBE VIDEO ID&fmt=THE MIME TYPE OF THE VIDEO
It can be turned into a streaming script that streams YouTube videos to the client also. Happy downloading YouTube videos!
Hey, thanks for your help, I created http://download-youtubevideo.com/ with help of your code, thanks a lot and please review if you get a chance.
I am interest look at you site it simple,maybe i will try to update my site http://soundclips.info with this one from licson lee it look great thanks.
Hi Suyash,
I’ve visited your site http://download-youtubevideo.com/
I need this site with complete code if you don’t mind. So plz reply me.
I really need the same type of site and searching for some days for this.
Thanks.
If Possible can you give a source code for your downloading audio + video project
I suppose Suyash site use a python script not and php only for wrapper , cmiiw.
I had a similar thing but not installed on my web infomusic.org
i have a website too. but, some videos dont work example:(https://www.youtube.com/watch?v=mzJj5-lubeM). how you resolve it?
hi everson vansoski so many posible reason
1.wmg video or like vevo video use cipher signature that change every day so your script must decipher this cipher first.
2. this content is not available on your country (youtube check your ip to request to this video).
hey am trying to create a downloader like yours but the problem i get is videos are being downloaded to the server instead of client machine
hello can you send me a copy of your download script to musisifred7@gmail.com
thanx a lot
pls let me know.. if ur get any update
Hello,are you from Uganda ?,please call me on +256702501820.
hi suyash please tell me how to upload this coding to my site.???
Hi Suyash Kumar, great creation of the website. I want to create like this but I dont have knowledge in coding. Can you please send me the code so I can embed to my wordpress site. Or is it okay to just iFrame your website? Thats way more easier. Please let me know your opinion.
Thank you
can you please let me know the logic . or code
Suyash(Nitin Kumar) your work is awesome.I have a simple technique that can benefit you much more.
I watched your alexa ranking (34 lakhs 🙁 ) which is to slow to rank high
Good Luck.
Hi,
It’s Work! Thanks!
but, how can I get a list of new uploaded videos in YouTube? or search in YouTube videos!
Are there any api for those?
Thank you for article!
The only small type I see: get_video_id => get_video_info
how to download video mp4 360p. plz help me
thank you for share this trick.
many many thank you
Hello Licson,
is there a way to download the mp3 Version of a video?
And how can I directly download the video to the users browser instead of saving it in the server ( I sometimes have over 1k videos. That would be way too much to save in the server )?
Greetings,
Cr4fTeXe
thanks. thats the easiest solution that i found
How would I add an input box and a button to this? I would lile for my users to be able to imput the URL and click the button, starting the process.
Thanks! Your tutorial helped me mak the Ruby Version of this script.
See the Gist on Github
Yeah it is not working anymore, as of YouTube updated their security measures. Any way great written script yet.
Thank you for that nice script Licson!
But it only downloads 720p mp4…
How to download lower quality?
Hi Licson,
Great downloader, it works for me almost 90% of the time, but on certain videos it fails.
An example is this video – https://www.youtube.com/watch?v=5uGg6iHnKMs
Does it succeed for you?
Hi,
After this line: $file = fopen(‘video.’.str_replace($format,’video/’,”),’w’);
I recive this error:
PHP Warning: failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
in /home/public_html/index.php on line 23
what is this?
Does your site have a contact page? I’m having
trouble locating it but, I’d like to shoot you an e-mail.
I’ve got some recommendations for your blog you might be interested in hearing.
Either way, great site and I look forward to seeing it develop over time.
You may want to check out this: the contect page is here.
I need a youtube Downloader widget for wordpress , can anyone give me ?
This script is not working any more, but it did help. I guess YouTube updated their security measures. Now they seem to be checking your IP address, so a signature is valid only for one IP address, and you need to specify more parameters. Here is what I did :
I actually wanted to host the script on my server. The problem with YouTube checking the IP address is that in my case I must download the videos on my server, then download them from my server to my laptop. I can’t directly access the YouTube servers with the generated URL, but that’s not a big issue.
Oh! I’ll update my script as soon as possible.
By the way, I’ve another youtube video downloader.
https://github.com/licson0729/node-YoutubeStreamer
Doesn’t work :'(
Can you give me more information such as the youtube video ID and the contents of the result video file so that I can fix it? Also you need to wait for the script finished as that’s mean the video is fully downloaded.
Something wrong: in the third pre tag
“parse_str($stream,$data); //decode the stream”, not “prase_str($stream,$data); //decode the stream”
Thanks, it’s fixed now.
請問這樣有沒有違反Youtube的機制?
很多YouTube downloader 都是用這招,所以沒有甚麼大問題。
It’s a good post. But the institution might be changed, right?
According to what I’ve tested, it works pretty fine. If there’re any changes to the API, I’ll update my post as soon as possible.
OK! Thank you in advanced. I’ll keep follow you, I think.
It’s nice to hear that you’re going to follow me. I’m sure that I’ll write more good posts!