Search code examples
youtube-apijava-18

YouTube Api v3: calculate youtube playlist length


How can i get a youtube playlist duration (the sum of all it's videos durations).

I used this endpoint to get the playlist videos :

https://youtube.googleapis.com/youtube/v3/videos?playlistId

and it's return the videos object with all videos ids, in this case i need to make an api call to get each video length with separate api call to :

https://youtube.googleapis.com/youtube/v3/videos

I searched for a solution for this and found that we can make a call to previous api with all ids each id with a query param ex. id=aaaaa&id=bbbbb, but i need to be the owner of these videos to make the request.

Is there any way to get all videos with one call and without being the owner of the videos ?


Solution

  • Yes you can calculate length of any youtube playlist.

    1. There is two API
    2. First API will give you id of each video
    3. Using this id you can make a request to get duration of each video Here is both api which you are wandering around

    'https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=500&fields=items/contentDetails/videoId,nextPageToken,pageInfo&key=${API_KEY}&playlistId=${link}&pageToken='

    https://www.googleapis.com/youtube/v3/videos?&part=contentDetails&id=${id}&key=${API_KEY}&fields=items/contentDetails/duration
    

    create an api key in https://console.cloud.google.com/apis/dashboard and get API_KEY. Use it in first url and make a get request you will get all the id's. video id of playlist

    Use id in second url make a request you will get duration

    enter image description here