How to grab YouTube playback video files
Step-by-step tutorial showing how to get all available video source files from a video on YouTube using only Javascript. No plugins and/or server side scripts are required.
First of all I want to list here a few benefits from being able to do that.
- You can download any video from YouTube without plugins or server-side scripts. You just need a browser.
- You can use YouTube as an encoder. Let's say you have to convert a FLV video to MP4 or
WebM. - You can set a cron job to run once a week to download all available video files from any channel.
- You can play any YouTube video out of the YouTube player (not recommend).
- You can play YouTube videos on HTML5 canvas and go crazy! Check this out at: http://videopuzzle.possum-cms.com
How to do it
Source code: https://github.com/edse/keepvideos
Step 1: Extract YouTube video ID from URL
var url = 'http://www.youtube.com/watch?v=yntfUjenls0';
var video_id = '';
var regExp = /.*(?:youtu.be\/|v\/|u\/\w\/|embed\/|watch\?v=)([^#\&\?]*).*/;
var match = url.match(regExp);
if(match){
video_id = match[1];
}
Step 2: Request YouTube playback files information
Easy as pie. Just make an Ajax request to: http://www.youtube.com/get_video_info sending the desired video ID...
$.ajax({
url: "http://www.youtube.com/get_video_info?video_id="+video_id,
dataType: "text"
}).done(function(data) {
...
});
Step 3: Parse the response data information from YouTube
The information that we are looking for is "url_encoded_fmt_stream_map". This variable contains all streams available for the requested movie.
Step 4: Trick. Parse again each one of the streams
YouTube encode the information twice, so you have to parse it again.
There is other trick here, You also have to add a signature value on each stream url, otherwise it won't work.
Step 5: Have fun!
By now you should have a variable "results" with all YouTube video files available for the requested movie.
You can do whatever you want with the files. For example: display a link, play the video, grab snap shots of the video... Have fun!
To display the a link for each video file available...
function(data) {
var info = {};
parse_str(data, info);
var streams = explode(',', info['url_encoded_fmt_stream_map']);
...
Step 4: Trick. Parse again each one of the streams
YouTube encode the information twice, so you have to parse it again.
There is other trick here, You also have to add a signature value on each stream url, otherwise it won't work.
var results = [];
for(var i=0; i<streams.length; i++){
var real_stream = {};
parse_str(streams[i], real_stream);
real_stream['url'] += '&signature=' + real_stream['sig'];
results.push(real_stream);
}
Step 5: Have fun!
By now you should have a variable "results" with all YouTube video files available for the requested movie.
You can do whatever you want with the files. For example: display a link, play the video, grab snap shots of the video... Have fun!
To display the a link for each video file available...
if(results.length > 0){
$.each(results, function(index, value) {
$('#results').append('<a href="'+value.url+'">'+value.quality+' - '+value.type</a>');
});
}
This comment has been removed by the author.
ReplyDeleteOMG It's awesome.It's working fine.Thanks
ReplyDeleteI have a small question.
How I can grab youtube video url on web browser directly.
I tried eg:
http://http://keepvideos.possum-cms.com/#url=http://www.youtube.com/watch?v=XsrYCLY6D3I
But, its doesn't work.Could you help me?
Hi Erman,
DeleteFirstly, thank you for your feedback.
Secondly, I'm Sorry, but the version you have tried is out date. This is an old version and it's not working properly. I have already edit my post.
Please try it at: http://keepvideos.appspot.com ... and let me know how it goes.
Best Regards,
Emerson Estrella
By the way, you can't do it via URL only right now. You should fill the URL input.
DeleteThis comment has been removed by the author.
DeleteThanks for your interest.
DeleteI found a small bug.
When I use Internet Explorer 9, İt's stucks at loading.
Download links are does't shows.
I were tested it with Opera, Google Chrome and Firefox.It's working fine.But Internet Explorer not shows the download links.
I will follow your works.
Thanks & Best Regards.
Hi Emerson;
ReplyDeleteThanks for your reply.
YouR system is awesome.But I want that modification.
Could you make it?
When I call the url on browser directly, it's shows download links.
For example url:
http://keepvideos.appspot.com/index.php?url=http://www.youtube.com/watch?v=XsrYCLY6D3I
Thank you for your interest.You are number one.
Best Regards.
Hey Erman, it's done.
Deletehttp://keepvideos.appspot.com/?url=http://www.youtube.com/watch?v=XsrYCLY6D3I
Have fun!
OMG.It's working fine.
DeleteIt's have another small bug.The system is not working with Internet Explorer.
Best Regards.
Sorry bro! I have no interest in supporting IE browsers. To be frank with you I hate IE!
DeleteBut I can help you if you post here the IE error messages.
Best regards,
Emerson Estrella
Dear Emerson;
DeleteAlso, I hate from IE :)
It's doesn't gives error message.
İt's stucks at "loading".
Download links are does't shows.
Thanks & Regards.
Nice app man! Congrats!
ReplyDeleteBTW, is that possible to get the name of the video and put as the name of the file to download?!
Maybe parse from the page title, or something similar. Like the Easy Youtube Downloader for the firefox does with the file name (https://addons.mozilla.org/en-US/firefox/addon/easy-youtube-video-downl-10137/?src=userprofile).
Sincerely,
Hey Gabriel, thanks for you comment. I really appreciate that.
DeleteAbout the download file name, I'm not sure if I can do that, because it is not passing through a server, it is javascript only.
Maybe using the File API... I'll try it and let you know.
Regards,
-Emerson Estrella
Hi Emerson;
DeleteIt's not working now :(
Could you fix it?
What's going on here? Half your file is in Javascript the other half is in PHP? parse_str is not a Javascript function...
ReplyDeleteHey Michel,
ReplyDeleteI did translate de PHP parse_str function from PHP to JS.
You can take a look in the repository: https://github.com/edse/keepvideos
Regards,
Emerson Estrella
http://keepvideos.possum-cms.com
ReplyDeleteI'm sorry, But Not Working
:(
it's a nice work
ReplyDeleteBut not working now..
Coding
ReplyDeleteHoliday Camps - We provide the best online learning coaching for Stem Education Coding in Singapore. Get the best online Stem Education Coding for your kids.
https://www.bricks4kidz.com.sg/
This comment has been removed by the author.
ReplyDeleteNice post, very helpful. Do you mind checking out my website earlycode.net as an expert that you are and give me feedback
ReplyDelete