html - 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.
是否有另一个公共端点,我可以通过它获取视频的大小/纵横比?我需要无需使用某些登录名或开发人员密钥即可访问端点。
解决方案
我花了几个小时调试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}`);
})();
推荐阅读
- selenium - 此错误是由于 chromdriver 版本错误造成的吗?
- istio - Istio 网关和选择器问题 1.0.2 -> 1.2.3
- java - 作为 Facebook Graph API 对 GET 广告分页的响应的内部服务器错误
- ios - 如何在 Ios swift 中将值写入 BLE 设备中的现有特征
- sql-server - SQL server - 统计和重新编译
- css - 更改所选选项卡的颜色,TabMenu PRIMENG - 样式
- android - firebase 中的数据库页面未显示我的数据库
- sql - 如何将文本列 VARCHAR2(4000 BYTE) 转换为日期格式?
- docker - 如何正确地将我的 Nest-Js/Angular docker-compose 图像连接到我的主机?
- leaflet - 在 Leaflet 地图中编辑图层