Forked from liaohuqiu/gist:4ee77b9b03afcdecc80252252378d367
Last active
December 30, 2019 02:09
-
-
Save half-duplex/f5b3bc5054bd112b90b73e4cd7fa09ca to your computer and use it in GitHub Desktop.
Find duration of YouTube playlist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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