Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save half-duplex/f5b3bc5054bd112b90b73e4cd7fa09ca to your computer and use it in GitHub Desktop.
Save half-duplex/f5b3bc5054bd112b90b73e4cd7fa09ca to your computer and use it in GitHub Desktop.
Find duration of YouTube playlist
var pl_times = document.getElementsByClassName('pl-video-time');
var time = 0;
function toS(hms) {
var a = hms.split(':');
while (a.length < 3) {
a.unshift(0);
}
var seconds = (+a[0]) * 60 * 60 + (+a[1]) * 60 + (+a[2]);
return seconds;
}
function toDHMS(totalSec) {
var days = parseInt( totalSec / 86400 );
var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;
var result = ((days < 10 ? "0" + days : days) + "d " +
(hours < 10 ? "0" + hours : hours) + "h " +
(minutes < 10 ? "0" + minutes : minutes) + "m " +
(seconds < 10 ? "0" + seconds : seconds) + "s");
return result;
}
for (var i = 0; i < pl_times.length; i++) {
var pl_time = pl_times[i];
var timestamp = pl_time.getElementsByClassName("timestamp");
if (timestamp === undefined) { continue; } // Private video
var vid_time = timestamp[0].children[0].textContent;
time += toS(vid_time);
}
console.log("The full duration time is: " + toDHMS(time));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment