我正在尝试使用Youtube API检索播放列表视频的持续时间,图片和视图.我注意到我检索到的片段中没有包含这些内容.这是我到目前为止的代码:
gapi.client.setApiKey('xxxx'); gapi.client.load('youtube', 'v3', function() { var request = gapi.client.youtube.playlistItems.list({ part: 'snippet, contentDetails', playlistId: 'PL3C9792F77CB0EE51', maxResults: 50 }); request.execute(function(response) { for (var i = 0; i < response.items.length; i++) { console.log(response.items[i].snippet.title + " published at " + response.items[i].snippet.publishedAt); console.log(response.items[i].contentDetails.videoId); } });
console.log(response.items[i].snippet)
每个视频返回以下内容:
Object { publishedAt: ..., channelId: ..., title: ..., description: ..., thumbnails { ... } }
并console.log(response.items[i].contentDetails)
返回:
Object { videoId: ..., }
我想可能使用videoId然后再发出请求并检索每个给定视频的持续时间,图片和视图,但我不确定如何继续
一步步.
Youtube PlayListItem具有这种结构.
{ "kind": "youtube#playlistItem", "etag": etag, "id": string, "snippet": { "publishedAt": datetime, "channelId": string, "title": string, "description": string, "thumbnails": { (key): { "url": string, "width": unsigned integer, "height": unsigned integer } }, "channelTitle": string, "playlistId": string, "position": unsigned integer, "resourceId": { "kind": string, "videoId": string, } }, "contentDetails": { "videoId": string, "startAt": string, "endAt": string, "note": string }, "status": { "privacyStatus": string } }
如果要检索视频图像,请在thumbnails
属性上获得图像的网址.不过你也可以这样做.
http://img.youtube.com/vi//default.jpg
对于高质量版本的缩略图,请使用与此类似的网址:
http://img.youtube.com/vi//hqdefault.jpg
还有一个中等质量版本的缩略图,使用类似于总部的网址:
http://img.youtube.com/vi//mqdefault.jpg
对于缩略图的标准定义版本,请使用与此类似的URL:
http://img.youtube.com/vi//sddefault.jpg
对于缩略图的最大分辨率版本,请使用与此类似的URL:
http://img.youtube.com/vi//maxresdefault.jpg
编辑(获取视频信息)
要获得剩余的信息,您必须通过PlayListItem检索到的Id来获取videoObjet.演示
$.ajax({ async: false, type: 'GET', url: "https://www.googleapis.com/youtube/v3/videos?id={{videoId}}&key={{yourKey}}&part=snippet,contentDetails", success: function(data) { ...Do what you want with data... } }
返回的JSON如下所示:
{ kind: "youtube#videoListResponse", etag: ""oqbvhYxBE6fAbRk6m7aLlHf5s1I/jg_aG2jmpbZL5qs3yae4JnZbDs0"", pageInfo: { totalResults: 1, resultsPerPage: 1 }, items: [{ kind: "youtube#video", etag: ""oqbvhYxBE6fAbRk6m7aLlHf5s1I/YQ-QRwoxkXL4UBFIFCyCwIdmtzg"", id: "ojCkgU5XGdg", contentDetails: { duration: "PT23M14S", dimension: "2d", definition: "hd", caption: "true", licensedContent: false }, statistics: { viewCount: "25587", likeCount: "168", dislikeCount: "17", favoriteCount: "0", commentCount: "45" } }] }
如果您想要检索有关视频的更多信息.你必须对查询添加part
PARAM任何的下一个选择:snippet
,contentDetails
,fileDetails
,player
,processingDetails
,recordingDetails
,statistics
,status
,suggestions
ÿ topicDetails
.
我希望它有所帮助