Quantcast
Channel: Active questions tagged youtube-api - Stack Overflow
Viewing all articles
Browse latest Browse all 3831

How do I get the duration of videos from a YouTube playlist using Google API?

$
0
0

I am trying to make a playlist length calculator. However, the API is not returning the duration of the videos from the playlist when I make a call. Below is my code. However, the result is always 0, which is not possible since the first video itself is of 2 hours. When I manually check the API, it did not show duration for any video. How do I solve this?

const response = await fetch(`https://www.googleapis.com/youtube/v3/playlistItems?part=contentDetails&maxResults=50&playlistId=${url}&key=${key}`);const data = await response.json();const videos = data.items[0].contentDetails.itemCount;let totalDuration = 0;for (let i = 0; i < videos; i++) {  const videoDuration = data.items[i+1].contentDetails.duration;  const durationInSeconds = moment.duration(videoDuration).asSeconds();  totalDuration += durationInSeconds;}console.log(`Playlist length: ${totalDuration} minutes`);

Viewing all articles
Browse latest Browse all 3831

Trending Articles