How to get direct url for youtube videos

As of 1/1/2018, the old technique described below doesn’t seem to work reliably. Youtube’s in-page variable structure seems to have changed and I haven’t been able to figure out the updated version.

Here’s an updated (June, 2017) version that works directly from the dev tools console available in any browser. Currently should work for any Youtube media type, including up to 4k if its available.

https://gist.github.com/geuis/8b1b2ea57d7f9a9ae22f80d4fbf5b97f

// ES6 version
const videoUrls = ytplayer.config.args.adaptive_fmts
  .split(',')
  .map(item => item
    .split('&')
    .reduce((prev, curr) => (curr = curr.split('='),
      Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
    ), {})
  )
  .reduce((prev, curr) => Object.assign(prev, {
    [curr.quality_label || curr.type]: curr
  }), {});

console.log(videoUrls);

// ES5 version
var videoUrls = ytplayer.config.args.adaptive_fmts
  .split(',')
  .map(function (item) {
    return item
      .split('&')
      .reduce(function (prev, curr) {
        curr = curr.split('=');
        return Object.assign(prev, {[curr[0]]: decodeURIComponent(curr[1])})
      }, {});
  })
  .reduce(function (prev, curr) {
    return Object.assign(prev, {
      [curr.quality_label || curr.type]: curr
    });
  }, {});

console.log(videoUrls);

Sample output for https://www.youtube.com/watch?v=9bZkp7q19f0

{
  "1080p": {
    "itag": "248",
    "xtags": "",
    "lmt": "1440215955569849",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=248&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "video/webm;+codecs=\"vp9\"",
    "bitrate": "1733724",
    "index": "243-1977",
    "size": "1920x1080",
    "projection_type": "1",
    "fps": "30",
    "clen": "31192903",
    "init": "0-242",
    "quality_label": "1080p"
  },
  "720p": {
    "itag": "247",
    "xtags": "",
    "lmt": "1440215905109639",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=247&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "video/webm;+codecs=\"vp9\"",
    "bitrate": "726076",
    "index": "243-1933",
    "size": "1280x720",
    "projection_type": "1",
    "fps": "30",
    "clen": "15801933",
    "init": "0-242",
    "quality_label": "720p"
  },
  "480p": {
    "itag": "244",
    "xtags": "",
    "lmt": "1440215890236689",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=244&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "video/webm;+codecs=\"vp9\"",
    "bitrate": "396541",
    "index": "243-1933",
    "size": "854x480",
    "projection_type": "1",
    "fps": "30",
    "clen": "7928237",
    "init": "0-242",
    "quality_label": "480p"
  },
  "360p": {
    "itag": "243",
    "xtags": "",
    "lmt": "1440215888783441",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=243&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "video/webm;+codecs=\"vp9\"",
    "bitrate": "223695",
    "index": "243-1933",
    "size": "640x360",
    "projection_type": "1",
    "fps": "30",
    "clen": "5127362",
    "init": "0-242",
    "quality_label": "360p"
  },
  "240p": {
    "itag": "242",
    "xtags": "",
    "lmt": "1440215900971640",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=242&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "video/webm;+codecs=\"vp9\"",
    "bitrate": "113952",
    "index": "242-1931",
    "size": "426x240",
    "projection_type": "1",
    "fps": "30",
    "clen": "2597162",
    "init": "0-241",
    "quality_label": "240p"
  },
  "144p": {
    "itag": "278",
    "xtags": "",
    "lmt": "1440215900119192",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=278&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "video/webm;+codecs=\"vp9\"",
    "bitrate": "60303",
    "index": "242-1930",
    "size": "256x144",
    "projection_type": "1",
    "fps": "15",
    "clen": "1798744",
    "init": "0-241",
    "quality_label": "144p"
  },
  "audio/mp4;+codecs=\"mp4a.40.2\"": {
    "bitrate": "128266",
    "itag": "140",
    "xtags": "",
    "lmt": "1440578358539132",
    "index": "592-1271",
    "clen": "8482615",
    "projection_type": "1",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=140&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "audio/mp4;+codecs=\"mp4a.40.2\"",
    "init": "0-591"
  },
  "audio/webm;+codecs=\"vorbis\"": {
    "bitrate": "118499",
    "itag": "171",
    "xtags": "",
    "lmt": "1440215938192462",
    "index": "4452-5366",
    "clen": "6383456",
    "projection_type": "1",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=171&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "audio/webm;+codecs=\"vorbis\"",
    "init": "0-4451"
  },
  "audio/webm;+codecs=\"opus\"": {
    "bitrate": "154966",
    "itag": "251",
    "xtags": "",
    "lmt": "1440215889283443",
    "index": "272-1186",
    "clen": "9526605",
    "projection_type": "1",
    "url": "https://r16---sn-n4v7kn7r.c.youtube.com/videoplayback?itag=251&keepalive=ye…2Cmm%2Cmn%2Cms%2Cmv%2Cpl%2Crequiressl%2Csc%2Csource%2Cexpire&mt=1498245944",
    "type": "audio/webm;+codecs=\"opus\"",
    "init": "0-271"
  }
}

Leave a Comment