How to extract Youtube direct links.
It works today, but may be broken tomorrow, there is no official API,
links are hidden inside JS structures. It should be the reason why it is
difficult to find any details on how to do that.
Few online resources exist for the same. All have there own specifics,
that is why I have found reasonable to get it in one more way.
Youtube video pages contain a few <script> blocks. In one of them a specially
crafted structure can be found that has an entry «url_encoded_fmt_stream_map».
Decoding the value of this entry gives a list of direct URLs associated with
the video page.
Here is a utility to extract the list of direct video links, that works like this:
$ parse-youtube 'https://www.youtube.com/watch?v=YE7VzlLtp-4'
type: video/mp4;+codecs="avc1.64001F,+mp4a.40.2"
qlty: hd720
url: http://r1---sn-1uxaxjvh-axqe.googlevideo.com/videoplayback?itag=22&mm=31&mime=video%2Fmp4&ip=5.19.241.250&expire=1420738590&key=yt5&signature=DB43337D5B7E07B87A078981A1BDDB0A8DD67315.CA1FC0C7C9026057BFB2D3EBA29612FAF7C6AE20&ms=au&ipbits=0&initcwndbps=3356250&mv=m&mt=1420716889&id=o-AGbOfROQYll24IOkksEs_RRk2pa7TsRnEWCnB74nstmd&upn=13Qhd3usVqw&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cratebypass%2Csource%2Cupn%2Cexpire&sver=3&dur=596.567&source=youtube&ratebypass=yes&fexp=900504%2C900718%2C927622%2C930821%2C931024%2C932404%2C936931%2C9405619%2C941004%2C943917%2C947209%2C947218%2C948124%2C952302%2C952605%2C952901%2C955301%2C957103%2C957105%2C957201%2C958614%2C959701
type: video/webm;+codecs="vp8.0,+vorbis"
qlty: medium
url: http://r1---sn-1uxaxjvh-axqe.googlevideo.com/videoplayback?itag=43&mm=31&mime=video%2Fwebm&ip=5.19.241.250&expire=1420738590&key=yt5&signature=8AF71D47C2B663A7293A72CC7F61CD44D3185492.6BA2132AC05CB01355F0A92DAD02DAE2D1FFE5E1&ms=au&ipbits=0&initcwndbps=3356250&mv=m&mt=1420716889&id=o-AGbOfROQYll24IOkksEs_RRk2pa7TsRnEWCnB74nstmd&upn=13Qhd3usVqw&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cratebypass%2Csource%2Cupn%2Cexpire&sver=3&dur=0.000&source=youtube&ratebypass=yes&fexp=900504%2C900718%2C927622%2C930821%2C931024%2C932404%2C936931%2C9405619%2C941004%2C943917%2C947209%2C947218%2C948124%2C952302%2C952605%2C952901%2C955301%2C957103%2C957105%2C957201%2C958614%2C959701
Note that the page given with HTTPS, but the output links are HTTP.
Both HTTP and HTTPS work, «hd720» URL for example:
$ curl -sLI 'https://r1---sn-1uxaxjvh-axqe.googlevideo.com/videoplayback?itag=22&mm=31&mime=video%2Fmp4&ip=5.19.241.250&expire=1420738590&key=yt5&signature=DB43337D5B7E07B87A078981A1BDDB0A8DD67315.CA1FC0C7C9026057BFB2D3EBA29612FAF7C6AE20&ms=au&ipbits=0&initcwndbps=3356250&mv=m&mt=1420716889&id=o-AGbOfROQYll24IOkksEs_RRk2pa7TsRnEWCnB74nstmd&upn=13Qhd3usVqw&sparams=dur%2Cid%2Cinitcwndbps%2Cip%2Cipbits%2Citag%2Cmime%2Cmm%2Cms%2Cmv%2Cratebypass%2Csource%2Cupn%2Cexpire&sver=3&dur=596.567&source=youtube&ratebypass=yes&fexp=900504%2C900718%2C927622%2C930821%2C931024%2C932404%2C936931%2C9405619%2C941004%2C943917%2C947209%2C947218%2C948124%2C952302%2C952605%2C952901%2C955301%2C957103%2C957105%2C957201%2C958614%2C959701' | grep -w 200
HTTP/1.1 200 OK
Content streamed in RTMPE is not eligible for direct downloading.
The utility:
(HTM) HTML
(TXT) Plain-text