A powerful TypeScript library for fetching YouTube video transcripts with ease.
- π― Fetch transcripts from any YouTube video
- π Configurable HTTP clients (Fetch, Axios, Got, etc.)
- π Support for multiple languages
- πͺ Type-safe with full TypeScript support
- β‘ Modern ESM and CommonJS support
- π‘οΈ Robust error handling
- π§ͺ Well-tested and reliable
npm install captioneer
# or
yarn add captioneer
# or
pnpm add captioneer
import { Captioneer } from 'captioneer';
const captioneer = new Captioneer();
// Fetch captions for a video
const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ');
console.log(captions);
const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ', {
lang: 'es' // Fetch Spanish captions
});
Captioneer provides flexibility in how you make HTTP requests through its HTTP client system.
The library comes with a built-in HTTP client using the Fetch API:
import { Captioneer } from 'captioneer';
// Uses default HTTP client
const captioneer = new Captioneer();
const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ');
You can provide your own HTTP client implementation. Here's an example using Axios:
import { Captioneer, CaptioneerHttpClient } from 'captioneer';
import axios from 'axios';
const axiosClient: CaptioneerHttpClient = {
async get(url, options) {
const response = await axios.get(url, options);
return {
ok: response.status >= 200 && response.status < 300,
text: async () => response.data
};
}
};
const captioneer = new Captioneer(axiosClient);
const captions = await captioneer.fetchCaptions('dQw4w9WgXcQ');
Custom HTTP clients support additional configurations like proxies or custom headers:
const configuredClient: CaptioneerHttpClient = {
async get(url, options) {
const response = await axios.get(url, {
...options,
proxy: {
host: 'proxy-server',
port: 8080,
},
headers: {
...options?.headers,
'Custom-Header': 'value',
},
});
return {
ok: response.status >= 200 && response.status < 300,
text: async () => response.data,
};
},
};
The HTTP client interface ensures compatibility while giving you full control over the request implementation.
import { Captioneer, isCaptioneerError, ErrorCode } from 'captioneer';
try {
const captions = await captioneer.fetchCaptions('VIDEO_ID');
} catch (error) {
if (isCaptioneerError(error)) {
switch (error.code) {
case ErrorCode.LANGUAGE_NOT_AVAILABLE:
console.log('Language not available:', error.details.availableLanguages);
break;
case ErrorCode.VIDEO_UNAVAILABLE:
console.log('Video is not available');
break;
// Handle other specific errors
}
}
}
Main class for interacting with YouTube transcripts.
Methods
fetchCaptions(videoId: string, config?: CaptioneerConfig): Promise<CaptionEntry[]>
Fetches captions for a YouTube video.
- videoId: YouTube video ID or URL
- config: Optional configuration object
- lang: Language code (e.g., 'en', 'es')
Returns an array of caption entries.
interface CaptionEntry {
text: string;
duration: number;
offset: number;
lang: string;
}
interface CaptioneerConfig {
lang?: string;
}
The library provides specific error types for different scenarios:
- TooManyRequestsError: Rate limiting detected
- VideoUnavailableError: Video not accessible
- CaptionsDisabledError: Captions are disabled
- LanguageNotAvailableError: Requested language not available
- InvalidVideoIdError: Invalid video ID format
- NetworkError: Network-related issues
- ParseError: Response parsing failed
# Install dependencies
pnpm install
# Run tests
pnpm test
# Build the library
pnpm build
# Run linting
pnpm lint
Contributions are welcome! We follow a structured branching strategy and conventional commits.
- Fork the repository
- Create your feature branch from
develop
(git checkout -b feature/amazing-feature
) - Write meaningful commit messages following conventional commits (
feat: add amazing feature
) - Push to your branch (
git push origin feature/amazing-feature
) - Open a Pull Request to
develop
- See CONTRIBUTING.md for detailed contribution guidelines
- Check .github/BRANCH_STRATEGY.md for branch workflows
- Follow our Code of Conduct
Your contributions help make Captioneer better for everyone!
This project is licensed under the MIT License - see the LICENSE file for details.
Created by ItsCurstin