youtube-data-mcp
Version:
Extract YouTube transcripts and comments for analysis
37 lines (36 loc) • 1.35 kB
JavaScript
import { YoutubeTranscript } from 'youtube-transcript';
import { getVideoId } from '../utils/helpers.js';
import { getVideoInfo } from '../services/youtubeService.js';
export async function getTranscript({ url, lang = 'en' }) {
try {
// YouTube 동영상 ID 추출
const videoId = getVideoId(url);
if (!videoId) {
throw new Error('Invalid YouTube URL or video ID');
}
// 동영상 기본 정보 가져오기
const videoInfo = await getVideoInfo(videoId);
// 트랜스크립트 가져오기
const transcript = await YoutubeTranscript.fetchTranscript(videoId, { lang });
// 전체 텍스트 구성
const fullText = transcript.map(segment => segment.text).join(' ');
return {
videoInfo: {
id: videoId,
title: videoInfo.title,
channelName: videoInfo.channelName,
publishedAt: videoInfo.publishedAt,
viewCount: videoInfo.viewCount
},
transcript: transcript,
fullText: fullText,
language: lang
};
}
catch (error) {
console.error('Error fetching transcript:', error);
throw error instanceof Error
? error
: new Error('Failed to fetch transcript');
}
}