Skip to content

Instantly share code, notes, and snippets.

@magician11
Created August 14, 2018 17:12
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save magician11/f669cee2974c89d42602cb039719f7e6 to your computer and use it in GitHub Desktop.
Save magician11/f669cee2974c89d42602cb039719f7e6 to your computer and use it in GitHub Desktop.
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