Skip to content

DavidNaak/yt-transcript-api

Repository files navigation

YouTube Transcript API - FastAPI Wrapper

A FastAPI wrapper for the YouTube Transcript API that allows you to easily retrieve transcripts/subtitles for YouTube videos via HTTP endpoints.

Features

  • 🚀 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

Installation

  1. Clone this repository:

    git clone <your-repo-url>
    cd yt-transcript-api
  2. Install dependencies:

    pip install -r requirements.txt
  3. Start the server:

    python main.py

The API will be available at http://localhost:8000


API Endpoints

1. Get Transcript

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 ♪ ..."
}

2. List Available Transcripts

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": [...]
    }
  ]
}

Usage Examples

Basic Usage

# 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"

Using Python Requests

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']}")

API Documentation

Once the server is running, you can access:


Error Handling

The API returns appropriate HTTP status codes:

  • 200: Success
  • 400: 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"
}

Supported Languages

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...

Notes

  • Video ID: Use only the video ID, not the full URL. For https://www.youtube.com/watch?v=dQw4w9WgXcQ, use dQw4w9WgXcQ
  • 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

Development

Running Tests

# Test the API endpoints
curl "http://localhost:8000/getTranscript/dQw4w9WgXcQ"
curl "http://localhost:8000/listTranscripts/dQw4w9WgXcQ"

Project Structure

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages