While watching today's Apple Keynote I got the idea to statistically analyze
Created
September 13, 2018 08:44
-
-
Save leolabs/ba7ac1a9419d428d28d4887f6fcb20b9 to your computer and use it in GitHub Desktop.
Extracting subtitles from a <video> element in JS
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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