YouTube Transcript API (TypeScript)
A TypeScript library to retrieve transcripts/subtitles from YouTube videos. Supports auto-generated subtitles, multiple languages, and formatting options.
Installation
pnpm add youtube-transcript-ts
npm install youtube-transcript-ts
yarn add youtube-transcript-ts
Quick Start
import { YouTubeTranscriptApi } from 'youtube-transcript-ts';
const api = new YouTubeTranscriptApi();
const response = await api.fetchTranscript('dQw4w9WgXcQ');
console.log(`Found ${response.transcript.snippets.length} lines`);
response.transcript.snippets.slice(0, 3).forEach(snippet => {
console.log(`[${snippet.start.toFixed(1)}s]: ${snippet.text}`);
});
console.log(`Title: ${response.metadata.title}`);
console.log(`Author: ${response.metadata.author}`);
Features
API Configuration
const api = new YouTubeTranscriptApi({
cache: {
enabled: true,
maxAge: 3600000,
},
logger: {
enabled: true,
namespace: 'transcript-api',
},
invidious: {
enabled: false,
instanceUrls: 'https://yewtu.be',
timeout: 10000,
},
proxy: {
enabled: false,
http: 'http://localhost:8080',
https: 'http://localhost:8080',
},
});
Invidious Proxy Support
Invidious proxy support allows you to fetch transcripts even when YouTube API access is restricted or blocked. This feature is particularly useful for:
- Bypassing IP blocks or regional restrictions
- Accessing transcripts without YouTube tracking
- Improving reliability when YouTube API changes
const api = new YouTubeTranscriptApi({
invidious: {
enabled: true,
instanceUrls: 'https://yewtu.be',
},
});
const apiWithFallbacks = new YouTubeTranscriptApi({
invidious: {
enabled: true,
instanceUrls: ['https://yewtu.be'],
timeout: 8000,
},
});
const api = new YouTubeTranscriptApi();
api.setInvidiousOptions({
enabled: true,
instanceUrls: 'https://yewtu.be',
});
Note for self-hosting Invidious: If you're running your own Invidious instance for transcript fetching, you must set use_innertube_for_captions: true
in your Invidious configuration file for transcript functionality to work properly.
HTTP/HTTPS Proxy Support
You can configure the library to use HTTP and HTTPS proxies for all outgoing requests. This is useful in environments where direct connections to YouTube are restricted or when you need to route traffic through specific proxy servers.
const api = new YouTubeTranscriptApi({
proxy: {
enabled: true,
http: 'http://http-proxy-server.com:8080',
https: 'http://https-proxy-server.com:8443',
},
});
const apiWithSameProxy = new YouTubeTranscriptApi({
proxy: {
enabled: true,
http: 'http://proxy-server.com:8080',
https: 'http://proxy-server.com:8080',
},
});
const api = new YouTubeTranscriptApi();
api.setProxyOptions({
enabled: true,
http: 'http://username:password@http-proxy.com:8080',
https: 'http://username:password@https-proxy.com:8443',
});
api.setProxyOptions({
enabled: false,
});
Language Selection
const response = await api.fetchTranscript('VIDEO_ID', ['de', 'en']);
console.log(`Language: ${response.transcript.language}`);
Formatting Options
const textResponse = await api.fetchTranscript('VIDEO_ID', ['en'], false, 'text');
console.log(textResponse.formattedText);
Cookie Authentication for Age-Restricted Videos
api.setCookies({
CONSENT: 'YES+cb',
VISITOR_INFO1_LIVE: 'your_visitor_info',
});
Error Handling
The API throws specific error types for different failure cases:
try {
const transcript = await api.fetchTranscript('VIDEO_ID');
} catch (error) {
if (error instanceof VideoUnavailable) {
console.error('Video is not available');
} else if (error instanceof NoTranscriptFound) {
console.error('No transcript found for the requested languages');
} else if (error instanceof TranscriptsDisabled) {
console.error('Transcripts are disabled for this video');
} else if (error instanceof IpBlocked) {
console.error('Your IP address is blocked by YouTube');
} else {
console.error('An unexpected error occurred:', error);
}
}
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details