Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get video durations with YouTube API version 3?

I'm using YouTube API v3 to search YouTube.

https://developers.google.com/youtube/v3/docs/search

As you can see, the response JSON doesn't contains video durations. Is there way to get video durations?

Preferably not calling an API for each element in the result again (unless that's the only way to get durations).

like image 281
Rukshan Avatar asked Mar 24 '13 09:03

Rukshan


People also ask

How can I get YouTube video duration in PHP?

“Get video duration from url in PHP” Code Answerecho("Duration: ". $file['playtime_string'].

What is snippet in YouTube API?

The snippet object contains basic details about the video, such as its title, description, and category. The date and time that the video was published.


1 Answers

You will have to make a call to the YouTube data API's video resource after you make the search call. You can put up to 50 video IDs in a search, so you won't have to call it for each element.

https://developers.google.com/youtube/v3/docs/videos/list

You'll want to set part=contentDetails, because the duration is there.

For example, the following call:

https://www.googleapis.com/youtube/v3/videos?id=9bZkp7q19f0&part=contentDetails&key={YOUR_API_KEY} 

Gives this result:

{  "kind": "youtube#videoListResponse",  "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/ny1S4th-ku477VARrY_U4tIqcTw\"",  "items": [   {     "id": "9bZkp7q19f0",    "kind": "youtube#video",    "etag": "\"XlbeM5oNbUofJuiuGi6IkumnZR8/HN8ILnw-DBXyCcTsc7JG0z51BGg\"",    "contentDetails": {     "duration": "PT4M13S",     "dimension": "2d",     "definition": "hd",     "caption": "false",     "licensedContent": true,     "regionRestriction": {      "blocked": [       "DE"      ]     }    }   }  ] } 

The time is formatted as an ISO 8601 string. PT stands for Time Duration, 4M is 4 minutes, and 13S is 13 seconds.

like image 139
Matt Koskela Avatar answered Oct 07 '22 09:10

Matt Koskela