Skip to content

Instantly share code, notes, and snippets.

@hazycora
Created November 2, 2022 20:06
Show Gist options
  • Save hazycora/9e7349c60df47cf494296c336b952f4a to your computer and use it in GitHub Desktop.
Save hazycora/9e7349c60df47cf494296c336b952f4a to your computer and use it in GitHub Desktop.
get the duration of a youtube playlist
// scroll down so the entire playlists videos have been fetched, and run this
(() => {
const sumArray = (arr) => {
let sum = 0
for (let seconds of arr) {
sum+=seconds
}
return sum
}
const timestampElems = [...document.querySelectorAll('ytd-playlist-video-list-renderer ytd-thumbnail-overlay-time-status-renderer span.style-scope.ytd-thumbnail-overlay-time-status-renderer')]
const durations = timestampElems.map(e => {
let parts = e.innerText.split(':')
let parsed = {}
if (parts.length===3) {
parsed.hours = parseInt(parts[0])
parsed.minutes = parseInt(parts[1])
parsed.seconds = parseInt(parts[2])
} else {
parsed.minutes = parseInt(parts[0])
parsed.seconds = parseInt(parts[1])
}
return ((parsed.hours??0)*60 + parsed.minutes)*60 + parsed.seconds
})
let sumInSeconds = sumArray(durations)
console.log(`${sumInSeconds} seconds, or approximately ${Math.floor(sumInSeconds/(60*60))} hours and ${(Math.ceil(sumInSeconds%(60*60))/60).toFixed(3)} minutes`)
})()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment