get_video_info YouTube 端点突然返回 404 未找到
get_video_info YouTube endpoint suddenly returning 404 not found
https://www.youtube.com/get_video_info?video_id={videoId} 正在投掷
Response status code does not indicate success: 404 (Not Found).
编辑:我发现这可行。我不明白为什么。但是确实有效
您可以在 url 中添加 &html5=1
来修复它。
function getVideo($url)
{
parse_str(file_get_contents("https://www.youtube.com/get_video_info?video_id=$url&html5=1"), $data);
return $finalData = json_decode($data['player_response'], true);
}
header('Content-Type: application/json');
echo json_encode(getVideo('Zrcg7w67Ots'));
我就是这样使用它的,但有时它会起作用,有时它不起作用,即 returns info 有时它不起作用
你能修好里面的东西吗,非常感谢
出于某种原因,Google 正试图让它变得更难。
编辑 2021 年 8 月:
我刚刚为 C# 开发人员发布了一个新的 NuGet,它带来了 get_video_info 的 URL。享受!
https://www.nuget.org/packages/Youtube.VideoInfo
初始值为
https://www.youtube.com/get_video_info?video_id={videoId}&eurl=https://youtube.googleapis.com/v/{videoId}
然后添加了HTML5标签:
https://www.youtube.com/get_video_info?html5=1&video_id={videoId}&eurl=https://youtube.googleapis.com/v/{videoId}
现在(2021 年 6 月)新的解决方案是:
https://www.youtube.com/get_video_info?video_id={videoId}&eurl=https%3A%2F%2Fyoutube.googleapis.com%2Fv%2Fonz2k4zoLjQ&html5=1&c=TVHTML5&cver=6.20180913
我们需要擦亮眼睛。看起来 google 中有人不喜欢 YouTube 启用此功能这一事实。
我想我找到了替代 YouTube get_video_info 端点的方法。
下面是 CURL 和 PHP (CURL) 的实现
POST 向 youtubei/v1/player 端点请求最小请求 headers 和参数。
我已经从请求中删除了许多请求 headers 和参数,以保持简单和尽可能匿名,就像使用 GET get_video_info端点。例如 cookies、user-agent 等
下面的例子可能仍然包含可选的请求参数,因为它是在紧急情况下完成的。
我在将 user-agent 设置为电视后通过检查 firefox 中的 XHR 网络请求发现了这个新的 API。
firefox-inspect-network-xhr-requests-television-user-agent
以下 CURL 请求中使用的示例视频 link:
https://www.youtube.com/watch?v=UF8uR6Z6KLc
CURL
curl 'https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' -H 'Content-Type: application/json' --data '{ "context": { "client": { "hl": "en", "clientName": "WEB", "clientVersion": "2.20210721.00.00", "clientFormFactor": "UNKNOWN_FORM_FACTOR", "clientScreen": "WATCH", "mainAppWebInfo": { "graftUrl": "/watch?v=UF8uR6Z6KLc", } }, "user": { "lockedSafetyMode": false }, "request": { "useSsl": true, "internalExperimentFlags": [], "consistencyTokenJars": [] } }, "videoId": "UF8uR6Z6KLc", "playbackContext": { "contentPlaybackContext": { "vis": 0, "splay": false, "autoCaptionsDefaultOn": false, "autonavState": "STATE_NONE", "html5Preference": "HTML5_PREF_WANTS", "lactMilliseconds": "-1" } }, "racyCheckOk": false, "contentCheckOk": false}'
PHP (CURL)
function getVideoInfo($video_id){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "context": { "client": { "hl": "en", "clientName": "WEB", "clientVersion": "2.20210721.00.00", "clientFormFactor": "UNKNOWN_FORM_FACTOR", "clientScreen": "WATCH", "mainAppWebInfo": { "graftUrl": "/watch?v='.$video_id.'", } }, "user": { "lockedSafetyMode": false }, "request": { "useSsl": true, "internalExperimentFlags": [], "consistencyTokenJars": [] } }, "videoId": "'.$video_id.'", "playbackContext": { "contentPlaybackContext": { "vis": 0, "splay": false, "autoCaptionsDefaultOn": false, "autonavState": "STATE_NONE", "html5Preference": "HTML5_PREF_WANTS", "lactMilliseconds": "-1" } }, "racyCheckOk": false, "contentCheckOk": false}');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
}
更新(2021 年 7 月)
这个方法我试过了
POST:
https://youtubei.googleapis.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8
正文:
{
"context": {
"client": {
"hl": "en",
"clientName": "WEB",
"clientVersion": "2.20210721.00.00",
"mainAppWebInfo": {
"graftUrl": "/watch?v={VIDEO_ID}"
}
}
},
"videoId": "{VIDEO_ID}"
}
这项工作很有魅力
你可以试试这个插件
new YouTubeToHtml5();
<script src="https://cdn.jsdelivr.net/gh/thelevicole/youtube-to-html5-loader@4.0.1/dist/YouTubeToHtml5.js"></script>
<video data-yt2html5="https://www.youtube.com/watch?v=ScMzIvxBSi4" controls></video>
尝试 1060 修复 - 此问题尚未在 Pytube 包中修复。在 Pytube 中正式可用之前,您可以在系统中安装以下一个以进行下载工作。
https://github.com/Zeecka/pytube/tree/fix_1060
Youtube (撰写本文时 2021 年 8 月 8 日) 看起来已将等效输出移至 watch?v=
终点。您应该在 html 中的 ytInitialPlayerResponse
(json) 变量中找到视频格式 returns。
例如https://youtube.com/watch?v=SA8ZBJWo73E returns html。在那个 html 响应 ytInitialPlayerResponse
变量中包含一个 json 字符串。在该字符串中,您可以在以下位置找到视频格式:[streamingData][formats]
.
我很想把所有的功劳都归功于我,但这个例子 commit 是我找到答案的地方。
这就是我现在获取 YouTube 流媒体数据格式的方式。也许它可以帮助某人。
var videoInfoUrl = $"https://www.youtube.com/watch?v={videoId}";
using (var client = new HttpClient())
{
var videoPageContent = await client.GetStringAsync(videoInfoUrl);
var regex = new Regex(@"ytInitialPlayerResponse\s*=\s*(\{.+?\})\s*;", RegexOptions.Multiline);
var match = regex.Match(videoPageContent);
if (!match.Success)
return "";
var json = match.Result("");
var playerResponseJson = JToken.Parse(json);
var formats = playerResponseJson.SelectToken("streamingData.formats").ToList();
}
到目前为止,对于需要 get_video_info
API 数据的 Python 项目,我找到的最佳解决方案似乎是使用 the InnerTube
API from the pytube
project:
>>> from pytube.innertube import InnerTube
>>> innertube = InnerTube()
>>> video_info = innertube.player("dQw4w9WgXcQ")
>>> video_info.keys()
dict_keys(['responseContext', 'trackingParams', 'adBreakParams', 'playabilityStatus', 'streamingData', 'playbackTracking', 'videoDetails', 'annotations', 'playerConfig', 'storyboards', 'attestation', 'videoQualityPromoSupportedRenderers', 'messages', 'endscreen', 'playerSettingsMenuData'])
>>> video_info["videoDetails"]
{'videoId': 'dQw4w9WgXcQ', 'title': 'Rick Astley - Never Gonna Give You Up (Official Music Video)', 'lengthSeconds': '212', 'keywords': ['rick astley', 'Never Gonna Give You Up', 'nggyu', 'never gonna give you up lyrics', 'rick rolled', 'the boys soundtrack', 'the boys amazon prime', 'Never gonna give you up the boys', 'official', 'Rick Roll', 'music video', 'Rick Astley album', 'rick astley official', 'together forever', 'Whenever You Need Somebody', 'rickrolled', 'WRECK-IT RALPH 2', 'Fortnite song', 'Fortnite event', 'Fortnite dance', 'fortnite never gonna give you up', 'rick astley never gonna give you up', 'rick astley never gonna give you up lyrics'], 'channelId': 'UCuAXFkgsw1L7xaCfnd5JJOw', 'isOwnerViewing': False, 'shortDescription': "Rick Astley's official music video for “Never Gonna Give You Up” \n\nSubscribe to the official Rick Astley YouTube channel: https://RickAstley.lnk.to/YTSubID\n\nFollow Rick Astley:\nFacebook: https://RickAstley.lnk.to/FBFollowID \nTwitter: https://RickAstley.lnk.to/TwitterID \nInstagram: https://RickAstley.lnk.to/InstagramID \nWebsite: https://RickAstley.lnk.to/storeID \nTikTok: https://RickAstley.lnk.to/TikTokID\n\nListen to Rick Astley:\nSpotify: https://RickAstley.lnk.to/SpotifyID \nApple Music: https://RickAstley.lnk.to/AppleMusicID \nAmazon Music: https://RickAstley.lnk.to/AmazonMusicID \nDeezer: https://RickAstley.lnk.to/DeezerID \n\nLyrics:\nWe’re no strangers to love\nYou know the rules and so do I\nA full commitment’s what I’m thinking of\nYou wouldn’t get this from any other guy\n\nI just wanna tell you how I’m feeling\nGotta make you understand\n\nNever gonna give you up\nNever gonna let you down\nNever gonna run around and desert you\nNever gonna make you cry\nNever gonna say goodbye\nNever gonna tell a lie and hurt you\n\nWe’ve known each other for so long\nYour heart’s been aching but you’re too shy to say it\nInside we both know what’s been going on\nWe know the game and we’re gonna play it\n\nAnd if you ask me how I’m feeling\nDon’t tell me you’re too blind to see\n\nNever gonna give you up\nNever gonna let you down\nNever gonna run around and desert you\nNever gonna make you cry\nNever gonna say goodbye\nNever gonna tell a lie and hurt you\n\n#RickAstley #NeverGonnaGiveYouUp #OfficialMusicVideo", 'isCrawlable': True, 'thumbnail': {'thumbnails': [{'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/default.jpg', 'width': 120, 'height': 90}, {'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/mqdefault.jpg', 'width': 320, 'height': 180}, {'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg', 'width': 480, 'height': 360}, {'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/sddefault.jpg', 'width': 640, 'height': 480}]}, 'averageRating': 4.893901, 'allowRatings': True, 'viewCount': '1031179782', 'author': 'Rick Astley', 'isPrivate': False, 'isUnpluggedCorpus': False, 'isLiveContent': False}
https://www.youtube.com/get_video_info?video_id={videoId} 正在投掷
Response status code does not indicate success: 404 (Not Found).
编辑:我发现这可行。我不明白为什么。但是确实有效
您可以在 url 中添加 &html5=1
来修复它。
function getVideo($url)
{
parse_str(file_get_contents("https://www.youtube.com/get_video_info?video_id=$url&html5=1"), $data);
return $finalData = json_decode($data['player_response'], true);
}
header('Content-Type: application/json');
echo json_encode(getVideo('Zrcg7w67Ots'));
我就是这样使用它的,但有时它会起作用,有时它不起作用,即 returns info 有时它不起作用 你能修好里面的东西吗,非常感谢
出于某种原因,Google 正试图让它变得更难。
编辑 2021 年 8 月:
我刚刚为 C# 开发人员发布了一个新的 NuGet,它带来了 get_video_info 的 URL。享受! https://www.nuget.org/packages/Youtube.VideoInfo
初始值为
https://www.youtube.com/get_video_info?video_id={videoId}&eurl=https://youtube.googleapis.com/v/{videoId}
然后添加了HTML5标签:
https://www.youtube.com/get_video_info?html5=1&video_id={videoId}&eurl=https://youtube.googleapis.com/v/{videoId}
现在(2021 年 6 月)新的解决方案是:
https://www.youtube.com/get_video_info?video_id={videoId}&eurl=https%3A%2F%2Fyoutube.googleapis.com%2Fv%2Fonz2k4zoLjQ&html5=1&c=TVHTML5&cver=6.20180913
我们需要擦亮眼睛。看起来 google 中有人不喜欢 YouTube 启用此功能这一事实。
我想我找到了替代 YouTube get_video_info 端点的方法。
下面是 CURL 和 PHP (CURL) 的实现 POST 向 youtubei/v1/player 端点请求最小请求 headers 和参数。
我已经从请求中删除了许多请求 headers 和参数,以保持简单和尽可能匿名,就像使用 GET get_video_info端点。例如 cookies、user-agent 等
下面的例子可能仍然包含可选的请求参数,因为它是在紧急情况下完成的。
我在将 user-agent 设置为电视后通过检查 firefox 中的 XHR 网络请求发现了这个新的 API。
firefox-inspect-network-xhr-requests-television-user-agent
以下 CURL 请求中使用的示例视频 link:
https://www.youtube.com/watch?v=UF8uR6Z6KLc
CURL
curl 'https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8' -H 'Content-Type: application/json' --data '{ "context": { "client": { "hl": "en", "clientName": "WEB", "clientVersion": "2.20210721.00.00", "clientFormFactor": "UNKNOWN_FORM_FACTOR", "clientScreen": "WATCH", "mainAppWebInfo": { "graftUrl": "/watch?v=UF8uR6Z6KLc", } }, "user": { "lockedSafetyMode": false }, "request": { "useSsl": true, "internalExperimentFlags": [], "consistencyTokenJars": [] } }, "videoId": "UF8uR6Z6KLc", "playbackContext": { "contentPlaybackContext": { "vis": 0, "splay": false, "autoCaptionsDefaultOn": false, "autonavState": "STATE_NONE", "html5Preference": "HTML5_PREF_WANTS", "lactMilliseconds": "-1" } }, "racyCheckOk": false, "contentCheckOk": false}'
PHP (CURL)
function getVideoInfo($video_id){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.youtube.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, '{ "context": { "client": { "hl": "en", "clientName": "WEB", "clientVersion": "2.20210721.00.00", "clientFormFactor": "UNKNOWN_FORM_FACTOR", "clientScreen": "WATCH", "mainAppWebInfo": { "graftUrl": "/watch?v='.$video_id.'", } }, "user": { "lockedSafetyMode": false }, "request": { "useSsl": true, "internalExperimentFlags": [], "consistencyTokenJars": [] } }, "videoId": "'.$video_id.'", "playbackContext": { "contentPlaybackContext": { "vis": 0, "splay": false, "autoCaptionsDefaultOn": false, "autonavState": "STATE_NONE", "html5Preference": "HTML5_PREF_WANTS", "lactMilliseconds": "-1" } }, "racyCheckOk": false, "contentCheckOk": false}');
curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');
$headers = array();
$headers[] = 'Content-Type: application/json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
return $result;
}
更新(2021 年 7 月)
这个方法我试过了
POST: https://youtubei.googleapis.com/youtubei/v1/player?key=AIzaSyAO_FJ2SlqU8Q4STEHLGCilw_Y9_11qcW8
正文:
{
"context": {
"client": {
"hl": "en",
"clientName": "WEB",
"clientVersion": "2.20210721.00.00",
"mainAppWebInfo": {
"graftUrl": "/watch?v={VIDEO_ID}"
}
}
},
"videoId": "{VIDEO_ID}"
}
这项工作很有魅力
你可以试试这个插件
new YouTubeToHtml5();
<script src="https://cdn.jsdelivr.net/gh/thelevicole/youtube-to-html5-loader@4.0.1/dist/YouTubeToHtml5.js"></script>
<video data-yt2html5="https://www.youtube.com/watch?v=ScMzIvxBSi4" controls></video>
尝试 1060 修复 - 此问题尚未在 Pytube 包中修复。在 Pytube 中正式可用之前,您可以在系统中安装以下一个以进行下载工作。 https://github.com/Zeecka/pytube/tree/fix_1060
Youtube (撰写本文时 2021 年 8 月 8 日) 看起来已将等效输出移至 watch?v=
终点。您应该在 html 中的 ytInitialPlayerResponse
(json) 变量中找到视频格式 returns。
例如https://youtube.com/watch?v=SA8ZBJWo73E returns html。在那个 html 响应 ytInitialPlayerResponse
变量中包含一个 json 字符串。在该字符串中,您可以在以下位置找到视频格式:[streamingData][formats]
.
我很想把所有的功劳都归功于我,但这个例子 commit 是我找到答案的地方。
这就是我现在获取 YouTube 流媒体数据格式的方式。也许它可以帮助某人。
var videoInfoUrl = $"https://www.youtube.com/watch?v={videoId}";
using (var client = new HttpClient())
{
var videoPageContent = await client.GetStringAsync(videoInfoUrl);
var regex = new Regex(@"ytInitialPlayerResponse\s*=\s*(\{.+?\})\s*;", RegexOptions.Multiline);
var match = regex.Match(videoPageContent);
if (!match.Success)
return "";
var json = match.Result("");
var playerResponseJson = JToken.Parse(json);
var formats = playerResponseJson.SelectToken("streamingData.formats").ToList();
}
到目前为止,对于需要 get_video_info
API 数据的 Python 项目,我找到的最佳解决方案似乎是使用 the InnerTube
API from the pytube
project:
>>> from pytube.innertube import InnerTube
>>> innertube = InnerTube()
>>> video_info = innertube.player("dQw4w9WgXcQ")
>>> video_info.keys()
dict_keys(['responseContext', 'trackingParams', 'adBreakParams', 'playabilityStatus', 'streamingData', 'playbackTracking', 'videoDetails', 'annotations', 'playerConfig', 'storyboards', 'attestation', 'videoQualityPromoSupportedRenderers', 'messages', 'endscreen', 'playerSettingsMenuData'])
>>> video_info["videoDetails"]
{'videoId': 'dQw4w9WgXcQ', 'title': 'Rick Astley - Never Gonna Give You Up (Official Music Video)', 'lengthSeconds': '212', 'keywords': ['rick astley', 'Never Gonna Give You Up', 'nggyu', 'never gonna give you up lyrics', 'rick rolled', 'the boys soundtrack', 'the boys amazon prime', 'Never gonna give you up the boys', 'official', 'Rick Roll', 'music video', 'Rick Astley album', 'rick astley official', 'together forever', 'Whenever You Need Somebody', 'rickrolled', 'WRECK-IT RALPH 2', 'Fortnite song', 'Fortnite event', 'Fortnite dance', 'fortnite never gonna give you up', 'rick astley never gonna give you up', 'rick astley never gonna give you up lyrics'], 'channelId': 'UCuAXFkgsw1L7xaCfnd5JJOw', 'isOwnerViewing': False, 'shortDescription': "Rick Astley's official music video for “Never Gonna Give You Up” \n\nSubscribe to the official Rick Astley YouTube channel: https://RickAstley.lnk.to/YTSubID\n\nFollow Rick Astley:\nFacebook: https://RickAstley.lnk.to/FBFollowID \nTwitter: https://RickAstley.lnk.to/TwitterID \nInstagram: https://RickAstley.lnk.to/InstagramID \nWebsite: https://RickAstley.lnk.to/storeID \nTikTok: https://RickAstley.lnk.to/TikTokID\n\nListen to Rick Astley:\nSpotify: https://RickAstley.lnk.to/SpotifyID \nApple Music: https://RickAstley.lnk.to/AppleMusicID \nAmazon Music: https://RickAstley.lnk.to/AmazonMusicID \nDeezer: https://RickAstley.lnk.to/DeezerID \n\nLyrics:\nWe’re no strangers to love\nYou know the rules and so do I\nA full commitment’s what I’m thinking of\nYou wouldn’t get this from any other guy\n\nI just wanna tell you how I’m feeling\nGotta make you understand\n\nNever gonna give you up\nNever gonna let you down\nNever gonna run around and desert you\nNever gonna make you cry\nNever gonna say goodbye\nNever gonna tell a lie and hurt you\n\nWe’ve known each other for so long\nYour heart’s been aching but you’re too shy to say it\nInside we both know what’s been going on\nWe know the game and we’re gonna play it\n\nAnd if you ask me how I’m feeling\nDon’t tell me you’re too blind to see\n\nNever gonna give you up\nNever gonna let you down\nNever gonna run around and desert you\nNever gonna make you cry\nNever gonna say goodbye\nNever gonna tell a lie and hurt you\n\n#RickAstley #NeverGonnaGiveYouUp #OfficialMusicVideo", 'isCrawlable': True, 'thumbnail': {'thumbnails': [{'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/default.jpg', 'width': 120, 'height': 90}, {'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/mqdefault.jpg', 'width': 320, 'height': 180}, {'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/hqdefault.jpg', 'width': 480, 'height': 360}, {'url': 'https://i.ytimg.com/vi/dQw4w9WgXcQ/sddefault.jpg', 'width': 640, 'height': 480}]}, 'averageRating': 4.893901, 'allowRatings': True, 'viewCount': '1031179782', 'author': 'Rick Astley', 'isPrivate': False, 'isUnpluggedCorpus': False, 'isLiveContent': False}