YouTube 的 get_video_info 端点不再工作

Pra*_*y C 5 html youtube api

我正在使用 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.
Run Code Online (Sandbox Code Playgroud)

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

avi*_*i12 3

我花了几个小时调试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);
})();
Run Code Online (Sandbox Code Playgroud)

然后将内容与以下正则表达式进行匹配:
/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}`);
})();
Run Code Online (Sandbox Code Playgroud)