Instantly share code, notes, and snippets.

Embed
What would you like to do?
How to extract plain text subtitles from YouTube
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