Skip to content

Instantly share code, notes, and snippets.

@abonzer
Last active February 24, 2021 15:51
Show Gist options
  • Save abonzer/74273193b1b0ef36833b9d918ab61519 to your computer and use it in GitHub Desktop.
Save abonzer/74273193b1b0ef36833b9d918ab61519 to your computer and use it in GitHub Desktop.
Find the total length of a youtube playlist with javascript console.
// remove all comments lines
var countUdTime = 0;
var ytt = "desktop";
// for Desktop / laptop / youtube.com
// ===================================
// on YouTube Videos Page - https://www.youtube.com/channel/UCapQ8VzljfmUW5OM-ynsyCQ/videos
var ytp = document.querySelectorAll("ytd-grid-video-renderer");
if (ytp.length < 1) {
// on Playlist Page - https://www.youtube.com/playlist?list=PLv7XeeXKl8EKbJ-McS0ZUi1iP9ja12yaa
ytp = document.querySelectorAll("ytd-playlist-video-list-renderer > #contents > ytd-playlist-video-renderer");
}
if (ytp.length < 1) {
// on Playlist Watch Page - https://www.youtube.com/watch?v=MlBXz9gv08E&list=PLv7XeeXKl8EKbJ-McS0ZUi1iP9ja12yaa&index=1
ytp = document.querySelectorAll("ytd-playlist-panel-video-renderer");
}
// for Mobile / m.youtube.com
// ==========================
if (ytp.length < 1) {
ytt = "mobile";
// on YouTube Videos Page - https://m.youtube.com/c/UCapQ8VzljfmUW5OM-ynsyCQ/videos/
ytp = document.querySelectorAll("ytm-compact-video-renderer");
if (ytp.length < 1) {
// on Playlist Page
ytp = document.querySelectorAll("ytm-playlist-video-list-renderer > ytm-playlist-video-renderer");
}
if (ytp.length < 1) {
// on Playlist Watch Page
ytp = document.querySelectorAll("ytm-playlist-panel-video-renderer");
}
}
var time = 0;
for (var i = 0; i < ytp.length; i++) {
try {
if( ytt == "mobile"){
var a = ytp[i].getElementsByTagName("ytm-thumbnail-overlay-time-status-renderer")[0].innerText;
}else{
var a = ytp[i].getElementsByTagName("ytd-thumbnail-overlay-time-status-renderer")[0].innerText;
}
var tx = a.split(":");
if (tx.length < 3) {
time = time + Number(tx[0]) * 60 + Number(tx[1]);
} else if ((tx.length = 3)) {
time = time + Number(tx[0]) * 60 * 60 + Number(tx[1]) * 60 + Number(tx[2]);
}
} catch (e) {
countUdTime = countUdTime + 1;
}
}
var ytpT = convertS(time);
var show = "YouTube Playlist \n --------------- \n Total Videos : " + ytp.length + "\n Total Duration : " + ytpT + "\n Avg. Duration : " + convertS(time / ytp.length);
alert(show);
alert("Error to Get Time : " + countUdTime + " videos");
function convertS(sec) {
var hrs = Math.floor(sec / 3600);
var min = Math.floor((sec - hrs * 3600) / 60);
var seconds = sec - hrs * 3600 - min * 60;
seconds = Math.round(seconds * 100) / 100;
var result = (hrs < 10 ? "0" + hrs : hrs) + " Hours ";
result += (min < 10 ? "0" + min : min) + " Min ";
result += (seconds < 10 ? "0" + seconds : seconds) + " Sec ";
return result;
}
@abonzer
Copy link
Author

abonzer commented Apr 23, 2020

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment