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