Skip to content

Instantly share code, notes, and snippets.

@ahmed-com
Created December 27, 2020 07:53
Show Gist options
  • Save ahmed-com/8ee062e605ca35e596dc1391c0002f5b to your computer and use it in GitHub Desktop.
Save ahmed-com/8ee062e605ca35e596dc1391c0002f5b to your computer and use it in GitHub Desktop.
a simple script to run in the browser console to get the total length of youtube playlist
const elements = document.getElementsByClassName('style-scope ytd-thumbnail-overlay-time-status-renderer');
let totalDuration = 0;
for(let el of elements){
if(el.localName === 'span') {
const durationString = el.innerText;
const durationArr = durationString.split(':');
let hours;
let mins;
let secs;
if(durationArr.length === 3){
hours = +durationArr[0];
mins = +durationArr[1];
secs= +durationArr[2];
}else if(durationArr.length === 2){
hours = 0;
mins = +durationArr[0];
secs = +durationArr[1];
}
const duration = hours + (mins / 60) + (secs / 3600);
totalDuration += duration;
}
}
const totalHours = Math.floor(totalDuration);
const __totalMins__ = (totalDuration - totalHours) * 60;
const totalMins = Math.floor(__totalMins__);
const __totalSecs__ = (__totalMins__ - totalMins) * 60;
const totalSecs = Math.floor(__totalSecs__);
const totalDurationString = `${totalHours} hours, ${totalMins} minutes and ${totalSecs} seconds`
console.log(`duration of the whole playlist is ${totalDurationString}`);
@mustafaaboelhamd
Copy link

nice work

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