Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
345 views
in Technique[技术] by (71.8m points)

javascript - 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!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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"
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...