I am trying to get the caption of the youtube video which is unlisted. The video have auto Generated captions and I want that captions programmatically using nodejs.
the Things that i tried are Youtube data API – > the problems is always throws the error for the not founding the video. However when I try to list it it says the video is there, so ver skeptical about it.
async function getCaptions(videoId) {
try {
// List caption tracks for the given video ID
const response = await youtube.captions.list({
part: 'snippet',
videoId: videoId,
});
const captions = response.data.items;
if (!captions.length) {
console.log('No captions found for this video.');
return;
}
// Download the caption track (e.g., the first one)
const captionId = captions[0].id;
const captionTrack = await youtube.captions.download({
id: captionId,
tfmt: 'srt', // Use 'vtt' for WebVTT format or 'srt' for SubRip
});
console.log('Caption track:', captionTrack.data);
} catch (error) {
console.error('Error fetching captions:', error);
}
}
getCaptions('video_id');
//when I run this i get the error 404 vide not found
async function checkVideoAccess(videoId) {
try {
const response = await youtube.videos.list({
part: 'snippet',
id: videoId,
});
if (response.data.items.length === 0) {
console.log('Video not found or inaccessible.');
} else {
console.log('Video is accessible:', response.data.items[0].snippet.title);
}
} catch (error) {
console.error('Error accessing video:', error);
}
}
checkVideoAccess('video_id');
//when I run this i get video is accessible
I also tried scrapers-> the problem with scrapers is that the i do get the captions in my local env. but the moment i deploy it the aws ec2 instance, it stops working. I guess youtube is blocking the IP for my aws instance.
I have tried Youtube data api and scrapers and nothing is working for me. I would really appreciate any help. Also thoreo AI (website to summarize the youtube videos) is able to do generate the transcript for the unlisted youtube video. I dont know how they did that.