Using the YouTube Data API to Get Playlist Duration (Developer Guide)
PlaylistCalc Team
Video Analytics Experts
Prerequisites
Step 1: Get the Playlist Items
Use the `playlistItems.list` endpoint to fetch all video IDs in a playlist.
```
GET https://www.googleapis.com/youtube/v3/playlistItems
?part=contentDetails
&playlistId=YOUR_PLAYLIST_ID
&maxResults=50
&key=YOUR_API_KEY
```
Step 2: Batch Fetch Video Durations
Pass up to 50 video IDs at a time to the `videos.list` endpoint:
```
GET https://www.googleapis.com/youtube/v3/videos
?part=contentDetails
&id=VIDEO_ID_1,VIDEO_ID_2,...
&key=YOUR_API_KEY
```
Step 3: Parse ISO 8601 Duration
YouTube returns durations in ISO 8601 format (e.g., `PT1H23M45S`). Parse this with a regex:
PT1H23M45S = 1 hour, 23 minutes, 45 seconds = 5025 seconds
Step 4: Handle Pagination
Playlists with more than 50 videos require multiple API calls using the `nextPageToken` field.
Step 5: Sum and Format
Sum all durations in seconds, then format as HH:MM:SS.
API Quota
Each playlist item fetch costs 1 unit. Each video detail fetch costs 1 unit. Free tier gives 10,000 units/day — enough for ~100 large playlists.