YouTube's get_video_info endpoint no longer working

Viewed 677

I was using the YouTube's get_video_info unofficial endpoint to get the resolution of a video. Sometime very recently, this has stopped working and gives me:

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.

Is there another public endpoint via which I can get the size/aspect ratio of the video? I need the endpoint to be accessible without having to use some login or developer key.

1 Answers

I spent a couple of hours debugging youtube-dl, and I found out that for all videos, you can simply get the content of the web page, i.e.

(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);
})();

And then you match the content against the following RegExp:
/ytInitialPlayerResponse\s*=\s*({.+?})\s*;\s*(?:var\s+meta|<\/script|\n)/

Sample 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}`);
})();

Related