Skip to content

Instantly share code, notes, and snippets.

@davatron5000
Created January 29, 2023 16:52
Show Gist options
  • Save davatron5000/7560c5067e623d28f215af133e7fa2cd to your computer and use it in GitHub Desktop.
Save davatron5000/7560c5067e623d28f215af133e7fa2cd to your computer and use it in GitHub Desktop.
const arr = $$('.p-media_transcript__scrollable > p').map((line) => {
return {
timestamp: line.querySelector('.p-media_transcript__timestamp').textContent,
text: line.querySelector('.p-media_transcript__text').textContent
}
});
function hhmmssToMillis(hhmmss) {
const time = hhmmss.split(':').reverse();
switch(time.length) {
case 1:
return parseInt(time[0]) * 1000;
case 2:
return (parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
case 3:
return (parseInt(time[2]) * 60 * 60 + parseInt(time[1]) * 60 + parseInt(time[0])) * 1000;
default:
break;
}
return 0;
}
function msToTime(duration) {
var milliseconds = parseInt((duration%1000)/100)
, seconds = parseInt((duration/1000)%60)
, minutes = parseInt((duration/(1000*60))%60)
, hours = parseInt((duration/(1000*60*60))%24);
hours = (hours < 10) ? "0" + hours : hours;
minutes = (minutes < 10) ? "0" + minutes : minutes;
seconds = (seconds < 10) ? "0" + seconds : seconds;
return hours + ":" + minutes + ":" + seconds + "." + milliseconds;
}
function formatTime(hhmmss) {
return msToTime(hhmmssToMillis(hhmmss))
}
const video = document.querySelector('video');
let str = `WEBVTT
`
arr.forEach((line, index) => { str += `
${formatTime(line.timestamp)} --> ${ arr[index + 1] ? formatTime(arr[index + 1].timestamp) : msToTime(video.duration.toFixed()*1000)}
${line.text}
`
})
console.log(str)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment