Skip to content

Instantly share code, notes, and snippets.

@angel333
Last active August 15, 2021 19:33
Show Gist options
  • Save angel333/3dbfcb678938163e74dd5ec47043a7a0 to your computer and use it in GitHub Desktop.
Save angel333/3dbfcb678938163e74dd5ec47043a7a0 to your computer and use it in GitHub Desktop.
Youtube playlist duration
// Just copy this into console while on a playlist page (https://www.youtube.com/playlist?list=.....)
alert(((document) => {
let seconds = Array.from(document.querySelectorAll('.pl-video-time .timestamp span'))
.map((span) => {
let time = span.textContent.split(':');
return (Number(time[0]) * 60) + Number(time[1]);
})
.reduce((a,b) => {
return a + b;
});
let total = {
hours: Math.floor(seconds/(3600)),
minutes: Math.floor((seconds%3600)/60),
seconds: (seconds%60)
};
return 'Playlist duration: ' + total.hours + ' hours, ' + total.minutes + ' minutes, ' + total.seconds + ' seconds';
})(document));
@stek29
Copy link

stek29 commented Aug 2, 2018

Add &disable_polymer=1 to switch back to old YT design where this works

Fix for videos longer than one hour:

  .map((span) => {
    let time = span.textContent.split(':');
    return time.reduce((a, b) => a*60 + Number(b));
  })

To sort all videos by length:

document.querySelector('#pl-video-list tbody').innerHTML =
Array.from(document.querySelectorAll('.pl-video'))
.map((plv) => {
	let span = plv.querySelector('.pl-video-time .timestamp span');
	let time = span.textContent.split(':');
    let secs = 0;
    while (time.length) secs = secs * 60 + Number(time.shift());
	return {html: plv.outerHTML, len: secs};
})
.sort((xv, yv) => xv.len - yv.len)
.map(v => v.html)
.join('')

@H-G-Hristov
Copy link

function main(){
 const durationContainers = Array.from(document.getElementById("items").getElementsByClassName("style-scope ytd-thumbnail-overlay-time-status-renderer"));
 var durations = durationContainers.map(__span => __span.innerText).map(duration => {
   if (duration) {
     return duration.split(":").map(duration => parseInt(duration)).reverse().reduce((acc, e, i) => {
       return acc + e * Math.pow(60, i);
     });
   }
   else {
     return 0;
   }
 });
 durations = durations.filter(element => 0 !== element);
 console.log(durations);
 const totalSeconds = durations.reduce((acc, e, _) => {
   return acc + e;
 });
 console.log(totalSeconds);
 

 const seconds = totalSeconds % 60;
 const minutes = Math.floor(totalSeconds / 60) % 60;
 const hours = Math.floor(totalSeconds / 3600);
 const totalTime = "Play Time: " + hours + ":" + minutes + ":" + seconds;

 console.log(totalTime);

 const list = document.getElementById("start-actions");
 const div = document.createElement("div");
 div.class = "style-scope style-grey-text";
 const text = document.createElement("p");
 text.innerText = totalTime;
 div.appendChild(text);
 
 list.insertBefore(div, list.childNodes[1]);
};
main();

@sharatsachin
Copy link

I made an app to do that, hosted on Heroku. It can find the total length of a playlist that has up to 500 videos. If it has more than 500 videos, it shows the total length of the first 500 videos.
YouTube Playlist Length (https://ytplaylist-len.herokuapp.com/)

@H-G-Hristov
Copy link

You should make the 'edit box' much longer and also it would be nice if it is possible to just put the full URL not just the ID.

@emsi
Copy link

emsi commented Jan 19, 2020

YouTube Playlist Length (https://ytplaylist-len.herokuapp.com/)

Awesome!
Could you share app repo?

@sgadola
Copy link

sgadola commented Mar 19, 2020

Yes, please.

@sharatsachin
Copy link

sharatsachin commented Mar 26, 2020

You should make the 'edit box' much longer and also it would be nice if it is possible to just put the full URL not just the ID.

@H-G-Hristov
I've made the changes you suggested!

@emsi @sgadola
I've made the app repo! It's here.

@emsi
Copy link

emsi commented Mar 26, 2020

@emsi @sgadola
I've made the app repo! It's here.

Awesome! You are great! I love the new way to check the playlist length either by ID or URL.
Big thanks!

@sgadola
Copy link

sgadola commented Mar 26, 2020

Thank you.

@abhishekgoyal-a11y
Copy link

Get YouTube playlist length visit:- YouTube playlist length

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