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).
I got it!
There it returns the time for YouTube API version 3 (the key is made up by the way ;). I used
$vId
that I had gotten off of the returned list of the videos from the channel I am showing the videos from...It works... :) Google REALLY needs to include the duration in the snippet so you can get it all with one call instead of two... sigh....... it's on their 'wontfix' list.
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:
Gives this result:
The time is formatted as an ISO 8601 string. PT stands for Time Duration, 4M is 4 minutes, and 13S is 13 seconds.
Duration in seconds using Python 2.7 and the YouTube API v3:
I wrote the following class to get YouTube video duration using the YouTube API v3 (it returns thumbnails as well):
Please note that you'll need API_KEY to use the YouTube API:
This code extracts the YouTube video duration using the YouTube API v3 by passing a video ID. It worked for me.
You can get your domain's own YouTube API key on https://console.developers.google.com and generate credentials for your own requirement.