Created
August 14, 2018 17:12
-
-
Save magician11/f669cee2974c89d42602cb039719f7e6 to your computer and use it in GitHub Desktop.
How to extract plain text subtitles from YouTube
This file contains 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
const { getSubtitles } = require('youtube-captions-scraper'); | |
const getYouTubeID = require('get-youtube-id'); | |
const getYouTubeSubtitles = async youtubeUrl => { | |
try { | |
const videoID = getYouTubeID(youtubeUrl); | |
const subtitles = await getSubtitles({ videoID }); | |
return subtitles.reduce( | |
(accumulator, currentSubtitle) => | |
`${accumulator} ${currentSubtitle.text}`, | |
'' | |
); | |
} catch (error) { | |
console.log(`Error getting captions: ${error.message}`); | |
} | |
}; | |
(async () => { | |
const consoleArguments = process.argv; | |
if (consoleArguments.length !== 3) { | |
console.log( | |
'usage example: node get-youtube-subtitles.js https://www.youtube.com/watch?v=gypAjPp6eps' | |
); | |
return; | |
} | |
const subtitles = await getYouTubeSubtitles(consoleArguments[2]); | |
console.log(subtitles); | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment