YouTube 的 get_video_info 端点不再有效

YouTube's get_video_info endpoint no longer working

我使用 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.

是否有另一个 public 端点可以让我获得视频的 size/aspect 比例?我需要无需使用某些登录名或开发人员密钥即可访问端点。

我花了几个小时调试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}`);
})();