Javascript Snippet for YouTube Video Data

Disclaimer: the following content is solely for education purpose and personal use. Please strictly follow the terms of service of YouTube, do not use this technique for commercial or illegal use. Use it at your own risk.

The following Javascript snippet can fetch useful information from the new YouTube video page layout, including video ID, title and “download hash“:

fv=document.getElementById('movie_player').getAttribute('flashvars'); w=fv.split('&'); ret={'t':'','video_id':'','rec_title':''}; for(i=0;i<w.length;i++) {v=w[i].split('=');if(v[0] in ret)ret[v[0]]=unescape(v[1]);} [document.getElementById('watch-headline-title').innerText,ret.video_id,ret.t];

Usage

  1. Open a YouTube video page (try this one: Introducing the Blogger Template Designer)
  2. Fire up debug console
  3. Copy and paste the snippet above to the console and run it
  4. The console should return to you an array of three values:
    • Video title
    • Video ID
    • Video download hash. This is a time-sensitive token that is required to download a video

With these information it is possible to craft a direction download URL of a YouTube video in MP4 format. Format:
http://www.youtube.com/get_video?fmt=18&video_id=(video_id)&t=(download_hash)

Oh yeah, that means it’s possible to make a bookmarklet to download a video from YouTube page with just one click. Well I’ll leave the fun for you to figure this out since it is pretty trivial. In fact I made a URL Service from Automator to conveniently download YouTube mp4 video straight into my iTunes library, which can be later on sync into iPod, iPhone or AppleTV.

Download YouTube to iTunes

right click on a YouTube URL and save to iTunes

The code snippet is tested on the latest updated video layout as of April 2010.

Credit: code snippet above is a modified version from http://unlockforus.blogspot.com/2010/04/downloading-youtube-videos-not-working.html.

Post to Twitter Post to Facebook

About Calvin

Hello there, I’m Calvin Chun-yu Chan. Grew up in Hong Kong, studied and worked in Canada as web engineer+designer, now designing mobile apps in Tokyo. On my blog I would like to share my opinions on design, usability, culture and creativity.

Follow me on:
Twitter @calvincchan