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).
“Get video duration from url in PHP” Code Answerecho("Duration: ". $file['playtime_string'].
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With