Look at this link , there is an example given
https://www.googleapis.com/youtube/v3/videos?id=7lCDEYXw3mM&key=YOUR_API_KEY
&part=snippet,contentDeta
Using your links above, here is a quick php example on how to make only those 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 fetch maxResults=50
.
Onwards, we store each id in a string divided by commas
$get_duration="";
foreach ($JSON_Data->items as $ids) {
$get_duration .=$ids->id->videoId.",";
}
$get_duration = rtrim($get_duration, ",");
Finally, we make the second call using the batch 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
Step 1: You have a list video id by using Search: list
For example you receive 3 youtube video id like:
{ zOYW7FO9rzA, zOYW7FO9rzA, -vH2eZAM30s}
Step 2: You have to put the list of youtube video id for the second call.
https://www.googleapis.com/youtube/v3/videos?part=contentDetails&id=zOYW7FO9rzA,zOYW7FO9rzA,-vH2eZAM30s&key={Your API KEY}
Therefore, you won't have to make a call for each 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
}
}
]
}
The format duration: 1H1M1S = 1 hour & 1 minute & 1 second
As you've already found out, the Search:list call does not support contentDetails for the part parameter.
The part names that you can include in the parameter value for Search:list are id and snippet, and those return very little data. We're supposed to use that very little data from a search if we want to get more specific data on a video or videos.
So, to get a video duration when doing a search, you'll have to make a call like
GET https://www.googleapis.com/youtube/v3/search?part=id&q=anything&key={YOUR_API_KEY}
and extract the videoId from the response items
"id": {
"kind": "youtube#video",
"videoId": "5hzgS9s-tE8"
}
and use that to make the Videos:list call 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 the duration from the response data
"contentDetails": {
"duration": "PT15M51S",
"aspectRatio": "RATIO_16_9"
},