Skip to content

Instantly share code, notes, and snippets.

@leolabs
Created September 13, 2018 08:44
Show Gist options
  • Save leolabs/ba7ac1a9419d428d28d4887f6fcb20b9 to your computer and use it in GitHub Desktop.
Save leolabs/ba7ac1a9419d428d28d4887f6fcb20b9 to your computer and use it in GitHub Desktop.
Extracting subtitles from a <video> element in JS

Extracting subtitles from a <video> element in JS

While watching today's Apple Keynote I got the idea to statistically analyze

// Select the <video> element in Chrome's inspector first
// If the subtitles don't change dynamically, use this function
var text = Array.from($0.textTracks[1].cues).map(c => c.text).join('\n')
// If the subtitles change dynamically, like in Apple's Keynotes, use this function
var text = Array.from($0.textTracks[1].cues).reduce((acc, c, index, cues) => {
if(index == 0 || c.text.length >= cues[index - 1].text.length) return acc;
return acc.concat([cues[index - 1].text.split("\n")[0].trim()]);
}, []).join(' ')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment