Sunday, March 3, 2013

How to grab YouTube playback video files

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.

  1. You can download any video from YouTube without plugins or server-side scripts. You just need a browser.
  2. You can use YouTube as an encoder. Let's say you have to convert a FLV video to MP4 or
    WebM.
  3. You can set a cron job to run once a week to download all available video files from any channel.
  4. You can play any YouTube video out of the YouTube player (not recommend).
  5. You can play YouTube videos on HTML5 canvas and go crazy! Check this out at: http://videopuzzle.possum-cms.com

How to do it



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.

 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>');  
    });  
   }  

21 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. OMG It's awesome.It's working fine.Thanks
    I 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?

    ReplyDelete
    Replies
    1. Hi Erman,

      Firstly, 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

      Delete
    2. By the way, you can't do it via URL only right now. You should fill the URL input.

      Delete
    3. This comment has been removed by the author.

      Delete
    4. Thanks for your interest.
      I 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.

      Delete
  3. Hi Emerson;
    Thanks 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.

    ReplyDelete
    Replies
    1. Hey Erman, it's done.

      http://keepvideos.appspot.com/?url=http://www.youtube.com/watch?v=XsrYCLY6D3I

      Have fun!

      Delete
    2. OMG.It's working fine.
      It's have another small bug.The system is not working with Internet Explorer.
      Best Regards.

      Delete
    3. Sorry bro! I have no interest in supporting IE browsers. To be frank with you I hate IE!

      But I can help you if you post here the IE error messages.

      Best regards,

      Emerson Estrella

      Delete
    4. Dear Emerson;
      Also, I hate from IE :)
      It's doesn't gives error message.
      İt's stucks at "loading".
      Download links are does't shows.
      Thanks & Regards.

      Delete
  4. Nice app man! Congrats!

    BTW, 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,

    ReplyDelete
    Replies
    1. Hey Gabriel, thanks for you comment. I really appreciate that.

      About 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

      Delete
    2. Hi Emerson;
      It's not working now :(
      Could you fix it?

      Delete
  5. What's going on here? Half your file is in Javascript the other half is in PHP? parse_str is not a Javascript function...

    ReplyDelete
  6. Hey Michel,

    I 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

    ReplyDelete
  7. http://keepvideos.possum-cms.com

    I'm sorry, But Not Working
    :(

    ReplyDelete
  8. it's a nice work
    But not working now..

    ReplyDelete
  9. Coding


    Holiday 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/

    ReplyDelete
  10. This comment has been removed by the author.

    ReplyDelete
  11. Nice post, very helpful. Do you mind checking out my website earlycode.net as an expert that you are and give me feedback

    ReplyDelete