A FastAPI wrapper for the YouTube Transcript API that allows you to easily retrieve transcripts/subtitles for YouTube videos via HTTP endpoints.
- 🚀 FastAPI-powered: Modern, fast web framework with automatic API documentation
- 📝 Transcript Retrieval: Get full transcripts with timing information
- 🌍 Multi-language Support: Support for multiple languages and auto-generated transcripts
- 📊 Metadata: Get video metadata including language, generation type, and translation options
- 🔍 Transcript Listing: List all available transcripts for a video
- ⚡ Real-time: No API keys or authentication required
-
Clone this repository:
git clone <your-repo-url> cd yt-transcript-api
-
Install dependencies:
pip install -r requirements.txt
-
Start the server:
python main.py
The API will be available at http://localhost:8000
GET /getTranscript/{video_id}
Retrieve the transcript for a YouTube video.
Parameters:
video_id(path): YouTube video ID (e.g., "dQw4w9WgXcQ")languages(query, optional): Comma-separated list of language codes (default: "en")preserve_formatting(query, optional): Whether to preserve HTML formatting (default: false)
Example:
curl "http://localhost:8000/getTranscript/dQw4w9WgXcQ"Response:
{
"video_id": "dQw4w9WgXcQ",
"language": "English",
"language_code": "en",
"is_generated": false,
"snippets": [
{
"text": "♪ We're no strangers to love ♪",
"start": 18.64,
"duration": 3.24
}
],
"full_text": "♪ We're no strangers to love ♪ ..."
}GET /listTranscripts/{video_id}
List all available transcripts for a YouTube video.
Parameters:
video_id(path): YouTube video ID
Example:
curl "http://localhost:8000/listTranscripts/dQw4w9WgXcQ"Response:
{
"video_id": "dQw4w9WgXcQ",
"available_transcripts": [
{
"video_id": "dQw4w9WgXcQ",
"language": "English",
"language_code": "en",
"is_generated": false,
"is_translatable": true,
"translation_languages": [...]
}
]
}# Get English transcript
curl "http://localhost:8000/getTranscript/dQw4w9WgXcQ"
# Get German transcript (fallback to English)
curl "http://localhost:8000/getTranscript/dQw4w9WgXcQ?languages=de,en"
# Get transcript with formatting preserved
curl "http://localhost:8000/getTranscript/dQw4w9WgXcQ?preserve_formatting=true"import requests
# Get transcript
response = requests.get("http://localhost:8000/getTranscript/dQw4w9WgXcQ")
transcript = response.json()
print(f"Video ID: {transcript['video_id']}")
print(f"Language: {transcript['language']}")
print(f"Full Text: {transcript['full_text']}")
# Get individual snippets
for snippet in transcript['snippets']:
print(f"{snippet['start']}s: {snippet['text']}")Once the server is running, you can access:
- Interactive API docs: http://localhost:8000/docs
- ReDoc documentation: http://localhost:8000/redoc
- OpenAPI schema: http://localhost:8000/openapi.json
The API returns appropriate HTTP status codes:
200: Success400: Bad request (invalid video ID, no transcript available, etc.)500: Internal server error
Error responses include a descriptive message:
{
"detail": "Failed to fetch transcript: No transcript available for this video"
}The API supports all languages available on YouTube, including:
- English (en)
- Spanish (es)
- French (fr)
- German (de)
- Japanese (ja)
- Korean (ko)
- Chinese (zh)
- And many more...
- Video ID: Use only the video ID, not the full URL. For
https://www.youtube.com/watch?v=dQw4w9WgXcQ, usedQw4w9WgXcQ - Auto-generated transcripts: The API can retrieve both manually created and auto-generated transcripts
- Rate limiting: Be mindful of YouTube's rate limits when making many requests
- IP blocking: Some cloud providers may be blocked by YouTube. Consider using proxies for production deployments
# Test the API endpoints
curl "http://localhost:8000/getTranscript/dQw4w9WgXcQ"
curl "http://localhost:8000/listTranscripts/dQw4w9WgXcQ"