ContentDetails or duration not coming using Youtube v3 api

Look at the link, here's an example

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

      

Part of the answer

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

      

Now I want to get contentDetails or mostly duration. So I called with

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

      

He shows

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

      

Why? What am I missing? How do I get the duration of a video?

+3


source to share


3 answers


As you already figured out, the Search: list call does not support contentDetails for the detail parameter.

The part names you can include in the parameter value for the Search list are id and snippet, and they return very little data. We have to use very small data from the search if we want to get more specific data about a video or video.

So, to get the duration of the video while doing a search, you need to make a call like

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

      

and extract video from response items



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

      

and use that to make a video: call a list to get more specific data

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

      

and extract duration from response data

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

      

+21


source


Step 1 . You have a video ID using Search: list
For example, you get 3 YouTube video IDs: {zOYW7FO9rzA, zOYW7FO9rzA, -vH2eZAM30s}
Step 2 . You have to put a list of YouTube video IDs for the second call.

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

      

Therefore, you will not need to make a call for every video The result will be:



{
 "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
   }
  }
 ]
}
      

Run codeHide result


Format duration: 1H1M1S = 1 hour and 1 minute and 1 second

+6


source


Using the links above, there is a quick php example of how to make only these two calls for a maximum of 50 results per call

$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});

      

The above link will search for cats (q = cats) and will receive maxResults=50

.

Further we store each id in a comma separated string

$get_duration="";

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

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

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

      

Finally, we make a second call using the package IDs contained in $get_duration

and display the title and duration of each video

$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";
}

      

The result will be something like this

> 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

      

+3


source







All Articles