contentDetails或持续时间不会使用Youtube v3 api

时间:2014-12-10 05:24:03

标签: youtube google-api youtube-api gdata youtube-javascript-api

看看这个link,给出了一个例子

https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
 &part=snippet,contentDetails,statistics,status

部分回复是

"contentDetails": {
    "duration": "PT15M51S",
    "aspectRatio": "RATIO_16_9"
   },

现在我要检索contentDetails或主要是持续时间。所以我打电话给

https://www.googleapis.com/youtube/v3/search?part=snippet,contentDetails&key=[API_KEY]&q=something&maxResults=15&&fields=items,nextPageToken,prevPageToken,tokenPagination

显示

{
error: {
errors: [
{
domain: "youtube.part",
reason: "unknownPart",
message: "contentDetails",
locationType: "parameter",
location: "part"
}
],
code: 400,
message: "contentDetails"
}
}

为什么呢?我错过了什么?如何检索视频的持续时间?

3 个答案:

答案 0 :(得分:21)

正如您已经发现的那样,Search:list调用不支持part参数的contentDetails。

您可以在Search:列表的参数值中包含的部件名称是id和snippet,而那些返回的数据非常少。如果我们想要获取有关视频或视频的更具体数据,我们应该使用搜索中的极少数据。

因此,要在搜索时获得视频时长,您必须拨打电话

GET https://www.googleapis.com/youtube/v3/search?part=id&q=anything&key={YOUR_API_KEY}

并从响应项

中提取videoId
"id": {
"kind": "youtube#video",
"videoId": "5hzgS9s-tE8"
}

并使用它来进行视频:列表调用以获取更具体的数据

https://www.googleapis.com/youtube/v3/videos?id=5hzgS9s-tE8&key=YOUR_API_KEY&part=snippet,contentDetails,statistics,status

并从响应数据中提取持续时间

 "contentDetails": {
 "duration": "PT15M51S",
 "aspectRatio": "RATIO_16_9"
 },

答案 1 :(得分:6)

第1步:您使用Search: list来获得列表视频ID 例如,您收到3个youtube视频ID,如:
{zOYW7FO9rzA,zOYW7FO9rzA,-vH2eZAM30s}
第2步:您必须为第二次调用添加YouTube视频ID列表。

https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=zOYW7FO9rzA,zOYW7FO9rzA,-vH2eZAM30s&key={Your API KEY} 

因此,您不必为每个视频打电话 结果将是:



{
 "kind": "youtube#videoListResponse",
 "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/p3KyUGr7ZRowLgKTqVFixrx7-mQ\"",
 "pageInfo": {
  "totalResults": 3,
  "resultsPerPage": 3
 },
 "items": [
  {
   "kind": "youtube#video",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/psAhg0bxv1n1IfKwXhrPMV223YE\"",
   "id": "zOYW7FO9rzA",
   "contentDetails": {
    "duration": "PT1M21S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": false
   }
  },
  {
   "kind": "youtube#video",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/YCi772AbPZizPuAFci702rE55tU\"",
   "id": "T3Ysb9O3EWI",
   "contentDetails": {
    "duration": "PT1H28M47S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": false
   }
  },
  {
   "kind": "youtube#video",
   "etag": "\"iDqJ1j7zKs4x3o3ZsFlBOwgWAHU/2BnWErqkysQERsaRNyd1ffGgJes\"",
   "id": "-vH2eZAM30s",
   "contentDetails": {
    "duration": "PT12M57S",
    "dimension": "2d",
    "definition": "hd",
    "caption": "false",
    "licensedContent": false
   }
  }
 ]
}




格式持续时间:1H1M1S = 1小时& 1分钟& 1秒

答案 2 :(得分:3)

使用上面的链接,这是一个快速的php示例,说明如何只为这两个调用进行每次调用最多50个结果

$JSON = file_get_contents('https://www.googleapis.com/youtube/v3/search?part=snippet&q=cats&fields=items%2CnextPageToken%2CprevPageToken%2CtokenPagination&maxResults=50&key={YOUR_API_KEY});

以上链接将搜索猫(q =猫)并将获取maxResults=50

之后,我们将每个id存储在以逗号分隔的字符串中

$get_duration="";

foreach ($JSON_Data->items as $ids) {

        $get_duration .=$ids->id->videoId.",";
}

$get_duration = rtrim($get_duration, ",");

最后,我们使用$get_duration中包含的批量ID进行第二次调用,并显示每个视频的标题和持续时间

$JSON= file_get_contents('https://www.googleapis.com/youtube/v3/videos?part=snippet%2CcontentDetails%2Cstatistics%2Cstatus&id='.$get_duration.'&key={YOUR_API_KEY}');

$JSON_Data = json_decode($JSON);

foreach ($JSON_Data->items as $ids) {

        $date = new DateTime('1970-01-01');
        $date->add(new DateInterval($ids->contentDetails->duration));
        echo "Title: ".$ids->snippet->title."\nDuration: {$date->format('H:i:s')}\n\n";
}

结果将是这样的

> Title: Cats Being Jerks Video Compilation || FailArmy 
> Duration: 00:08:33
> 
> Title: Ultimate cat vines compilation - Best cat vines 2014 / 2015
> Duration: 00:14:58
> 
> Title: Funny cats annoying owners - Cute cat compilation 
> Duration: 00:05:58
> 
> Title: Funny Cats Compilation 60 min - NEW in HD 2014 
> Duration: 00:57:51