Feng Huo Feng Huo - 1 year ago 253
Javascript Question

How to get direct url for youtube videos

All the current tutorials on making your own youtube video downloader are outdated. The whole token insertion from video info does not work, and neither does using the javascript method. Does anyone know of a way to do this currently without just downloading someone else's programs? I just want to know the method to go about so I can program my own (for fun :D)

Thanks in advance!

Answer Source

As of today 15/06/2012 is really simple; but you have to be watch for future changes. Here it is (in Javascript)

    var urls = document.body.innerHTML.match(/"url_encoded_fmt_stream_map": "url=([^"]+)/)[1]
    urls = decodeURIComponent(urls).replace(/\\u0026/g,'&')
    urls = urls.replace(/&quality.+?(?=,url)/g,'');;
    urls = urls.split(',url=')
    // urls is an array of all the possible qualities
    // To download one you could use something like:
    // document.location = urls[0]
    // The first one is usually in the highest quality available
} catch(e){
    console.error("Youtube may have changed its API")