首页 > 解决方案 > YouTube 的 get_video_info 端点不再工作

问题描述

我正在使用 YouTube 的get_video_info非官方端点来获取视频的分辨率。最近的某个时候,这已经停止工作并给了我:

We're sorry...... but your computer or network may be sending automated queries. To protect our users, we can't process your request right now.

是否有另一个公共端点,我可以通过它获取视频的大小/纵横比?我需要无需使用某些登录名或开发人员密钥即可访问端点。

标签: htmlapiyoutube

解决方案


我花了几个小时调试youtube-dl,我发​​现对于所有视频,您都可以简单地获取网页的内容,即

(async () => {
  // From the back-end
  const urlVideo = "https://www.youtube.com/watch?v=aiSla-5xq3w";
  const html = await (await fetch(urlVideo)).text();
  console.log(html);
})();

然后将内容与以下 RegExp 进行匹配:
/ytInitialPlayerResponse\s*=\s*({.+?})\s*;\s*(?:var\s+meta|<\/script|\n)/

示例 HTML:

(async () => {
  const html = await (await fetch("https://gist.githubusercontent.com/avi12/0255ab161b560c3cb7e341bfb5933c2f/raw/3203f6e4c56fff693e929880f6b63f74929cfb04/YouTube-example.html")).text();
  const matches = html.match(/ytInitialPlayerResponse\s*=\s*({.+?})\s*;\s*(?:var\s+meta|<\/script|\n)/);
  const json = JSON.parse(matches[1]);

  const [format] = json.streamingData.adaptiveFormats;
  console.log(`${format.width}x${format.height}`);
})();


推荐阅读