Skip to content

Instantly share code, notes, and snippets.

@mkpolkowski
Forked from RafalJDev/CountYoutubeVideosTime.js
Last active April 2, 2024 14:51
Show Gist options
  • Save mkpolkowski/5003e758158186c737e4f51ce3647a53 to your computer and use it in GitHub Desktop.
Save mkpolkowski/5003e758158186c737e4f51ce3647a53 to your computer and use it in GitHub Desktop.
Count all videos time duration on youtube channel
//You need to run this in javascript console inside chrome
//Assumptions:
//1. Will count only "expanded" videos on page, you may first need to run script to scroll to last video or do it manually
//2. Tested on chrome, ubuntu, 2019
//3. Time format: hh:mm:ss
// Assuming you have your domlists as an array-like object
const domlists = document.querySelectorAll("ytd-rich-item-renderer");
var allHours = 0;
var allMinutes = 0;
var allSeconds = 0;
// Loop through each element in domlists
domlists.forEach(element => {
// Find the child element with <span> tag and the specified className
const childElement = element.querySelector("span.style-scope.ytd-thumbnail-overlay-time-status-renderer");
var splitedTime = childElement.textContent.split(":");
if (splitedTime.length == 1) {
allSeconds += +splitedTime[0];
} else if (splitedTime.length == 2) {
allMinutes += +splitedTime[0];
allSeconds += +splitedTime[1];
} else if (splitedTime.length == 3) {
allHours += +splitedTime[0];
allMinutes += +splitedTime[1];
allSeconds += +splitedTime[2];
} else {
console.log("WTF error, current content:", content);
}
});
var totalTimeSeconds = allHours * 3600 + allMinutes * 60 + allSeconds;
var seconds = totalTimeSeconds % 60;
var minutes = ~~(totalTimeSeconds / 60) % 60;
var hours = ~~(totalTimeSeconds / 3600);
// console.log("allHours:", allHours);
// console.log("allMinutes:", allMinutes);
// console.log("allSeconds:", allSeconds);
console.log("Hours:", hours);
console.log("Minutes:", minutes);
console.log("Seconds:", seconds);
//comments leaved for future fast debugging in case of errors, I know, bad practice
//Example page: https://www.youtube.com/user/DNewsChannel/videos
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment