所以我试图通过JSON格式从单个YouTube视频中获取信息。像标题描述类别一样,除了评论之外我还能得到什么。我试图在Javascript中这样做。我注意到下面的链接,但他们的所有示例都是如何从Feed中获取视频信息。我想从一个视频中获取信息,假设我知道它的ID。
https://developers.google.com/youtube/2.0/developers_guide_json
我也在看这个Stackoverflow Question,但我说它的get请求有问题 “test.js(第10行) 获取http://gdata.youtube.com/feeds/api/videos/VA770w...v=2&alt=json-in-script&callback=listInfo
200 OK 9ms的“
简而言之,如果我有一个像VA770wpLX-Q这样的YouTube视频ID,那么网址会以什么方式获取JSON中的视频信息?
谢谢
答案 0 :(得分:58)
2018年更新
不推荐使用API v2。新的youtube api v3仅适用于开发人员令牌,并且对免费连接有限制。
您可以在没有API的情况下获得JSON:
http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=ojCkgU5XGdg&format=json
或xml
http://www.youtube.com/oembed?url=http://www.youtube.com/watch?v=ojCkgU5XGdg&format=xml
{
"html": "<iframe width=\"480\" height=\"270\" src=\"https://www.youtube.com/embed/ojCkgU5XGdg?feature=oembed\" frameborder=\"0\" allow=\"autoplay; encrypted-media\" allowfullscreen></iframe>",
"title": "Creativity and Drugs (Eng Sub)",
"thumbnail_height": 360,
"provider_name": "YouTube",
"author_url": "https://www.youtube.com/user/serebniti",
"thumbnail_width": 480,
"height": 270,
"provider_url": "https://www.youtube.com/",
"type": "video",
"width": 480,
"version": "1.0",
"author_name": "serebniti",
"thumbnail_url": "https://i.ytimg.com/vi/ojCkgU5XGdg/hqdefault.jpg"
}
<强>大拇指:强>
hqdefault.jpg
质量较差,但始终存在。
http://img.youtube.com/vi/ojCkgU5XGdg/hqdefault.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/sddefault.jpg
最大尺寸
https://i.ytimg.com/vi/ojCkgU5XGdg/maxresdefault.jpg
迷你拇指:
http://img.youtube.com/vi/ojCkgU5XGdg/0.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/1.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/2.jpg
http://img.youtube.com/vi/ojCkgU5XGdg/3.jpg
<强>注解强>
http://www.youtube.com/annotations_invideo?cap_hist=1&video_id=ojCkgU5XGdg
解析移动页面16kb
https://m.youtube.com/watch?v=ojCkgU5XGdg
不要忘记将用户代理更改为iOS / Safari 7
同时强>
http://www.youtube.com/get_video_info?html5=1&video_id=ojCkgU5XGdg
如何嵌入youtube live
https://www.youtube.com/embed/live_stream?channel=UCkA21M22vGK9GtAvq3DvSlA
其中UCkA21M22vGK9GtAvq3DvSlA是您的频道ID。 您可以在“我的频道”链接的youtube帐户中找到它。
直播
https://i.ytimg.com/vi/W-fSCPrYSL8/hqdefault_live.jpg
答案 1 :(得分:39)
2015年5月更新:
此解决方案无法正常运行,YouTube API v2正在制作中,很快就会停止使用。
更多信息: https://www.youtube.com/devicesupport
尝试这样的事情:
var video_id='VA770wpLX-Q';
$.getJSON('http://gdata.youtube.com/feeds/api/videos/'+video_id+'?v=2&alt=jsonc',function(data,status,xhr){
alert(data.data.title);
// data contains the JSON-Object below
});
返回的JSON如下所示:
{
"apiVersion": "2.1",
"data": {
"id": "VA770wpLX-Q",
"uploaded": "2011-02-24T22:31:02.000Z",
"updated": "2012-04-08T21:37:06.000Z",
"uploader": "drdrevevo",
"category": "Music",
"title": "Dr. Dre - I Need A Doctor (Explicit) ft. Eminem, Skylar Grey",
"description": "Music video by Dr. Dre performing I Need A Doctor featuring Eminem and Skylar Grey (Explicit). © 2011 Aftermath Records",
"tags": ["Dr", "Dre", "Eminem", "New", "Song", "Skylar", "Grey", "GRAMMYs", "Dr.", "Need", "Doctor", "video", "Eazy", "N.W.A.", "NWA", "easy", "drdre", "and", "em"],
"thumbnail": {
"sqDefault": "http://i.ytimg.com/vi/VA770wpLX-Q/default.jpg",
"hqDefault": "http://i.ytimg.com/vi/VA770wpLX-Q/hqdefault.jpg"
},
"player": {
"default": "http://www.youtube.com/watch?v=VA770wpLX-Q&feature=youtube_gdata_player"
},
"content": {
"5": "http://www.youtube.com/v/VA770wpLX-Q?version=3&f=videos&app=youtube_gdata"
},
"duration": 457,
"aspectRatio": "widescreen",
"rating": 4.902695,
"likeCount": "430519",
"ratingCount": 441253,
"viewCount": 88270796,
"favoriteCount": 306556,
"commentCount": 270597,
"status": {
"value": "restricted",
"reason": "requesterRegion"
},
"restrictions": [{
"type": "country",
"relationship": "deny",
"countries": "DE"
}],
"accessControl": {
"comment": "allowed",
"commentVote": "allowed",
"videoRespond": "allowed",
"rate": "allowed",
"embed": "allowed",
"list": "allowed",
"autoPlay": "denied",
"syndicate": "allowed"
}
}
}
答案 2 :(得分:17)
YouTube API并不支持JSONP - 请参阅Issue 4329: oEmbed callback for JSONP。 另外,YouTube Data API v2 is deprecated。
您可以使用Noembed服务通过oEmbed获取YouTube视频的JSONP数据。
对于您的VA770wpLX-Q
视频,您可以尝试以下链接:
https://noembed.com/embed?url=http://www.youtube.com/watch?v=VA770wpLX-Q
或者对于JSONP:
https://noembed.com/embed?callback=example&url=http://www.youtube.com/watch?v=VA770wpLX-Q
这些链接具有作为url
参数传递的YouTube视频的标准网址。它不仅适用于YouTube,也适用于Vimeo和other sites,其网址如下:
https://noembed.com/embed?url=https://vimeo.com/45196609
这是一个使用jQuery的简单示例:
var id = 'VA770wpLX-Q';
var url = 'https://www.youtube.com/watch?v=' + id;
$.getJSON('https://noembed.com/embed',
{format: 'json', url: url}, function (data) {
alert(data.title);
});
请参阅:JS Bin上的DEMO。
另见这些问题:
答案 3 :(得分:10)
in v3:
$.getJSON('https://www.googleapis.com/youtube/v3/videos?id={videoId}&key={myApiKey}&part=snippet&callback=?',function(data){
if (typeof(data.items[0]) != "undefined") {
console.log('video exists ' + data.items[0].snippet.title);
} else {
console.log('video not exists');
}
});
使用PHP和CURL回复@Jonathan服务器端:
$url = "https://www.googleapis.com/youtube/v3/videos?id=".$videoId."&key=".$miApikey."&part=snippet";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL,$url);
$output=curl_exec($ch);
$response = json_decode($output, TRUE);
print_r($response);