如何获取YouTube视频的源URL?

时间:2018-02-21 16:59:29

标签: url youtube youtube-api blob

我正试图通过此代码获取youtube网址...

<div class="html5-video-container" data-layer="0">
    <video tabindex="-1" class="video-stream html5-main-video" controlslist="nodownload" src="blob:https://www.youtube.com/654261e0-472e-42b8-baac-951c85f8bb18" style="width: 514px; height: 289px; left: 0px; top: 0px;"></video>
</div>

我可以从这个blob获得src:https://www.youtube.com/654261e0-472e-42b8-baac-951c85f8bb18。但我不能通过这个网址呈现视频。问题是如何获得视频源的参考链接?或者可能存在youtube API以获取视频源?

1 个答案:

答案 0 :(得分:0)

我得到了解决方案。如果有人需要它,你可以使用。

    // 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);