YouTube Transcript API

1.2.4 · active · verified Wed Apr 01

This is a Python API that allows you to retrieve transcripts and subtitles for a given YouTube video. It supports both manually created and automatically generated subtitles, as well as subtitle translation. Unlike older solutions, it does not require a headless browser. The library is actively maintained, currently at version 1.2.4, and receives regular updates to address YouTube's API changes.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to fetch a transcript for a given YouTube video ID using the `YouTubeTranscriptApi` client. It includes error handling for common issues like missing or disabled transcripts and IP blocking. The example retrieves the 'Never Gonna Give You Up' transcript and prints its beginning.

from youtube_transcript_api import YouTubeTranscriptApi, NoTranscriptFound, TranscriptsDisabled, IpBlocked

video_id = "_dQ8wY76uI8" # Example: 'Never Gonna Give You Up' by Rick Astley

try:
    # Instantiate the API client
    ytt_api = YouTubeTranscriptApi()

    # Fetch the transcript for the video
    # By default, it attempts to get English. You can specify languages=['de', 'en'] for priority.
    transcript = ytt_api.fetch(video_id)

    # Print the full transcript text
    full_text = " ".join([item['text'] for item in transcript])
    print(f"Transcript for video ID {video_id}:\n{full_text[:500]}...")

    # You can also list available transcripts and their languages
    # transcript_list = ytt_api.list_transcripts(video_id)
    # for t in transcript_list:
    #     print(f"Available transcript: {t.language} ({'generated' if t.is_generated else 'manual'})")

except NoTranscriptFound:
    print(f"No transcript found for video ID: {video_id}")
except TranscriptsDisabled:
    print(f"Transcripts are disabled for video ID: {video_id}")
except IpBlocked:
    print(f"Your IP was blocked by YouTube. Consider using a proxy.")
except Exception as e:
    print(f"An unexpected error occurred: {e}")

view raw JSON →