Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const fetch = require("node-fetch")
- async function getYouTubeCaptions(videoUrl) {
- const response = await fetch('https://website-tools-dot-maestro-218920.uk.r.appspot.com/getYoutubeCaptions', {
- method: 'POST',
- headers: {
- 'Content-Type': 'application/json',
- 'User-Agent': 'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/141.0.0.0 Mobile Safari/537.36',
- 'Referer': 'https://maestra.ai/tools/video-to-text/youtube-transcript-generator?v=RY1zQHjUG_Q'
- },
- body: JSON.stringify({
- videoUrl: videoUrl
- })
- });
- const result = await response.json();
- const transcript = extractTranscript(result.selectedCaptions);
- return {
- videoID: result.videoID,
- defaultLanguage: result.defaultLanguage,
- transcript: transcript
- };
- }
- function extractTranscript(webvttText) {
- let lines = webvttText.split('\n').filter(line =>
- !line.startsWith('WEBVTT') &&
- !line.startsWith('Kind:') &&
- !line.startsWith('Language:') &&
- line.trim() !== ''
- );
- let transcript = [];
- let currentText = '';
- for (let line of lines) {
- if (line.includes('-->') || line.includes('align:') || line.includes('position:')) {
- continue;
- }
- if (line.trim() === '') {
- if (currentText.trim() !== '') {
- transcript.push(currentText.trim());
- currentText = '';
- }
- continue;
- }
- let cleanLine = line.replace(/<[^>]+>/g, '').trim();
- if (cleanLine !== '') {
- if (currentText !== '') {
- currentText += ' ';
- }
- currentText += cleanLine;
- }
- }
- if (currentText.trim() !== '') {
- transcript.push(currentText.trim());
- }
- return transcript.filter(text => text !== '[Musik]' && text !== '[Music]');
- }
- //use
- const captions = await getYouTubeCaptions("https://youtu.be/FtHaMWRn8Mc?si=G503YW_gdLl4NWL_");
- console.log(JSON.stringify(captions, null, 2));
Advertisement
Add Comment
Please, Sign In to add comment