Sign inSign up

oldgrandpavanu/youtubetranscriptapi

By oldgrandpavanu

Updated 11 months ago

A REST API wrapper for the python youtube-transcript-api application

Image
Developer tools
Data science
Web analytics
1

1.7K

oldgrandpavanu/youtubetranscriptapi repository overview

YouTube Transcript REST API

A lightweight, production-ready REST API service that extracts and formats YouTube video transcripts (subtitles/captions). Built with FastAPI and powered by the excellent youtube-transcript-api Python library.

What is this?

This Docker container provides a simple REST API interface to fetch YouTube video transcripts without needing the YouTube Data API or authentication. It's perfect for:

  • Content analysis and NLP projects
  • Accessibility tools
  • Video summarization services
  • Educational applications
  • Research projects requiring video transcript data

Key Features

  • Multiple Output Formats: Export transcripts as JSON, Plain Text, WebVTT, or SRT subtitles
  • Language Support: Fetch transcripts in any available language, including auto-generated captions
  • Discovery Endpoint: List all available transcript languages for any video
  • Optional API Key Protection: Secure your API with optional key-based authentication
  • CORS Enabled: Ready for cross-origin requests from web applications
  • Comprehensive Error Handling: Proper HTTP status codes for age-restricted, unavailable, or blocked content
  • Built-in Documentation: Interactive Swagger UI and ReDoc documentation included
  • Health Checks: Docker health monitoring built-in
  • Production Ready: Built on FastAPI with Uvicorn ASGI server

Quick Start

Pull and Run
docker pull oldgrandpavanu/youtubetranscriptapi:latest
docker run -d -p 8000:8000 --name youtube-transcript-api oldgrandpavanu/youtubetranscriptapi:latest

Access the API at http://localhost:8000 and documentation at http://localhost:8000/docs

Run with API Key Protection
docker run -d -p 8000:8000 -e API_KEY=your_secret_key_here --name youtube-transcript-api oldgrandpavanu/youtubetranscriptapi:latest

Docker Compose

Create a compose.yaml file:

services:
  api:
    image: oldgrandpavanu/youtubetranscriptapi:latest
    ports:
      - "8000:8000"
    environment:
      - API_KEY=${API_KEY:-}  # Optional: set via environment variable
    restart: unless-stopped
    healthcheck:
      test: ["CMD", "curl", "-f", "http://localhost:8000/"]
      interval: 30s
      timeout: 10s
      retries: 3

Start the service:

# Without API key
docker compose up -d

# With API key protection
API_KEY=your_secret_key_here docker compose up -d

API Usage

Endpoint Overview
  • GET / - Welcome message and API information
  • GET /transcript - Fetch video transcript in various formats
  • GET /transcripts - List all available transcripts for a video
  • GET /docs - Interactive Swagger UI documentation
  • GET /redoc - ReDoc API documentation
Get Transcript

Fetch a transcript in JSON format (default):

curl "http://localhost:8000/transcript?video_id=dQw4w9WgXcQ&language=en"

Fetch as plain text:

curl "http://localhost:8000/transcript?video_id=dQw4w9WgXcQ&language=en&format=text"

Fetch as SRT subtitles:

curl "http://localhost:8000/transcript?video_id=dQw4w9WgXcQ&language=en&format=srt"

Fetch as WebVTT:

curl "http://localhost:8000/transcript?video_id=dQw4w9WgXcQ&language=en&format=webvtt"

Parameters:

  • video_id (required): YouTube video ID
  • language (optional, default: "en"): Language code (e.g., "en", "es", "fr", "de")
  • format (optional, default: "json"): Output format - json, text, webvtt, or srt
List Available Transcripts

Discover all available transcript languages for a video:

curl "http://localhost:8000/transcripts?video_id=dQw4w9WgXcQ"

Response Example:

{
  "video_id": "dQw4w9WgXcQ",
  "available_transcripts": [
    {
      "language": "English",
      "language_code": "en",
      "is_generated": false,
      "is_translatable": true
    },
    {
      "language": "Spanish",
      "language_code": "es",
      "is_generated": true,
      "is_translatable": true
    }
  ]
}
Using API Key Authentication

When the API_KEY environment variable is set, include the key in your requests:

curl -H "X-API-Key: your_secret_key_here" \
  "http://localhost:8000/transcript?video_id=dQw4w9WgXcQ&language=en"

Environment Variables

VariableRequiredDefaultDescription
API_KEYNoNoneWhen set, requires X-API-Key header for all protected endpoints
API_KEY_FILENoNonePath to file containing API key (recommended for production). Mutually exclusive with API_KEY

Production Deployment with Docker Secrets

For production environments, use Docker secrets instead of environment variables for enhanced security.

Docker Swarm Secrets
# Create the secret
echo "your_production_api_key" | docker secret create api_key -

# Deploy as a service
docker service create \
  --name youtube-transcript-api \
  --secret api_key \
  --env API_KEY_FILE=/run/secrets/api_key \
  --publish 8000:8000 \
  --replicas 3 \
  oldgrandpavanu/youtubetranscriptapi:latest

# Or using docker stack with compose file
docker stack deploy -c compose.yaml youtube-transcript-api
Docker Compose with Secrets (Local Development)

Create a compose.yaml:

services:
  api:
    image: oldgrandpavanu/youtubetranscriptapi:latest
    ports:
      - "8000:8000"
    environment:
      - API_KEY_FILE=/run/secrets/api_key
    secrets:
      - api_key
    restart: unless-stopped

secrets:
  api_key:
    file: ./secrets/api_key.txt  # For local dev
    # external: true              # For production swarm

Then create the secret file and start:

mkdir -p secrets
echo "your_secret_key" > secrets/api_key.txt
chmod 600 secrets/api_key.txt
docker compose up -d
Kubernetes Secrets

For Kubernetes deployments:

# Create the secret
kubectl create secret generic youtube-api-key \
  --from-literal=api-key=your_secret_key_here

# Reference in deployment
apiVersion: apps/v1
kind: Deployment
metadata:
  name: youtube-transcript-api
spec:
  template:
    spec:
      containers:
      - name: api
        image: oldgrandpavanu/youtubetranscriptapi:latest
        env:
        - name: API_KEY
          valueFrom:
            secretKeyRef:
              name: youtube-api-key
              key: api-key
Security Benefits

Using the _FILE suffix pattern with Docker secrets provides:

  • No exposure in docker inspect: Secrets are not visible in container metadata
  • Encryption at rest: Secrets are encrypted in Docker Swarm's Raft log
  • Encryption in transit: Secrets are transmitted encrypted over TLS
  • Access control: Only authorized services can access specific secrets
  • Audit trail: Secret access can be logged and monitored
  • No environment variable leaks: Prevents accidental exposure in logs or process listings

Error Handling

The API provides meaningful HTTP status codes and error messages:

Status CodeScenario
200Success
401Invalid or missing API key (when protection enabled)
403Age-restricted content or IP blocked by YouTube
404Video unavailable, transcript not found, or transcripts disabled
429Request blocked by YouTube (rate limiting)
500Internal server error

Technical Details

Stack
  • Framework: FastAPI 0.120.3
  • ASGI Server: Uvicorn 0.38.0
  • Core Library: youtube-transcript-api 1.2.3
  • Python Version: 3.13 (slim)
  • Configuration: python-dotenv 1.2.1
Container Specifications
  • Base Image: python:3.13-slim
  • Exposed Port: 8000
  • Working Directory: /app
  • Health Check: Built-in HTTP health check on root endpoint
  • Restart Policy: unless-stopped (in compose)
Volume Mounting

For development, mount the application directory:

docker run -p 8000:8000 -v $(pwd):/app oldgrandpavanu/youtubetranscriptapi:latest
Architecture

The application follows a clean, single-file architecture (main.py) with:

  • FastAPI application initialization
  • CORS middleware for cross-origin requests
  • Optional API key security layer
  • Two main endpoints with comprehensive error handling
  • Integration with youtube-transcript-api for transcript fetching
  • Multiple formatter support (JSON, Text, WebVTT, SRT)

Use Cases

Content Analysis

Extract transcripts for sentiment analysis, keyword extraction, or topic modeling.

Accessibility

Convert video content to readable text or downloadable subtitle files.

Education

Create study materials from educational videos or generate summaries.

Research

Analyze large collections of video content for academic research.

Integration

Integrate into workflows, automation tools, or content management systems.

Documentation

Once the container is running, access the interactive documentation:

Source Code

GitHub Repository: https://github.com/coryrolstad/YoutubeTranscriptApi

License

MIT License - Free for personal and commercial use.

Credits

This project is a REST API wrapper around the excellent youtube-transcript-api Python package maintained by Jonas Depoix.

Support

For issues, feature requests, or contributions, please visit the GitHub Issues page.

Tag summary

Content type

Image

Digest

sha256:9a9e4e83e

Size

47 MB

Last updated

11 months ago

docker pull oldgrandpavanu/youtubetranscriptapi