Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 32 You must be signed in to star a gist
  • Fork 16 You must be signed in to fork a gist
  • Save liaohuqiu/4ee77b9b03afcdecc80252252378d367 to your computer and use it in GitHub Desktop.
Save liaohuqiu/4ee77b9b03afcdecc80252252378d367 to your computer and use it in GitHub Desktop.
Find out the full duration time of a YouTube playlist
var list = 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 toHMS(totalSec) {
var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;
var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
return result;
}
function add(item) {
item = item.getElementsByClassName('timestamp')[0];
item = item.getElementsByTagName('SPAN')[0];
var timeString = (item.innerText || item.textContent);
time += toS(timeString);
}
for (var i = 0; i < list.length; i++) {
var item = list[i];
add(item);
}
console.log("The full duration time is: " + toHMS(time));
@davet1957
Copy link

aussies fun

@mohamedaboelmagd
Copy link

//To add days count
var list = 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 toHMS(totalSec) {
var days = parseInt( totalSec / 86400 ) % 24;
var hours = parseInt( totalSec / 3600 ) % 24;
var minutes = parseInt( totalSec / 60 ) % 60;
var seconds = totalSec % 60;
var result =(days < 10 ? "0" + days : days) + ":"+ (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds < 10 ? "0" + seconds : seconds);
return result;
}

function add(item) {
item = item.getElementsByClassName('timestamp')[0];
item = item.getElementsByTagName('SPAN')[0];
var timeString = (item.innerText || item.textContent);
time += toS(timeString);
}

for (var i = 0; i < list.length; i++) {
var item = list[i];
add(item);
}

console.log("The full duration time is: " + toHMS(time));

@krmax44
Copy link

krmax44 commented May 25, 2017

does not work on the new YouTube with Polymer anymore.

@Alvai
Copy link

Alvai commented Feb 6, 2018

it does work ! thanks

https://i.imgur.com/Vwlo2To.png

@krozendaal
Copy link

krozendaal commented Mar 23, 2018

To make it work again, use the code below:

var list = document.getElementsByClassName('ytd-thumbnail-overlay-time-status-renderer');
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 toHMS(totalSec) {
  var hours = parseInt( totalSec / 3600 ) % 24;
  var minutes = parseInt( totalSec / 60 ) % 60;
  var seconds = totalSec % 60;
  var result = (hours < 10 ? "0" + hours : hours) + ":" + (minutes < 10 ? "0" + minutes : minutes) + ":" + (seconds  < 10 ? "0" + seconds : seconds);
  return result;
}

function add(item) {
   /*item = item.getElementsByClassName('timestamp')[0];
   item = item.getElementsByTagName('SPAN')[0]; */
   var timeString = (item.innerText || item.textContent);
   time += toS(timeString);
}

for (var i = 0; i < list.length; i++) {
  var item = list[i];
  add(item);
}

console.log("The full duration time is: " + toHMS(time));

@Fabian42
Copy link

Fabian42 commented Jul 1, 2018

I don't know why you added %24, it makes the script return wrong results. Removing it fixes that. Alternatively, you could add something for days.

@EnzoMolion
Copy link

Thanks for the correction @kreekhoorn, it works well 😄

@omargoda
Copy link

omargoda commented Sep 8, 2021

I have added to days feature to the code as it prints only hours, minutes, and seconds, but ignores the days.
https://github.com/omargoda/Get-full-length-of-YouTube-playlist/blob/master/README.md

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