EN
English
简体中文
Log inGet started for free

Blog

blog

get-youtube-transcripts-programmatically

How to Get YouTube Transcripts Programmatically

This page is published by Thordata, one of the products compared.

You can get YouTube transcripts programmatically with a Python transcript library for public videos or the official API for captions on videos you manage. For a managed scraping workflow, a web scraper is another option. This guide is for developers collecting captions from one video or processing many videos. It covers how to choose a method, handle missing captions, and store text with its timing and language.

Choose a transcript retrieval method

Retrieve an available YouTube caption track, then convert its timed segments into the text or subtitle format your application needs. Use the official captions API for videos you can manage, a community Python library for public captions, or a managed scraper when batch collection matters more than maintaining retrieval code. Each route has different access and output requirements.

MethodPermissionsCaption availabilityLanguage selectionOutputImplementation effort
Official captions APIRequires access to manage the videoDepends on its caption tracksSelect a trackCaption fileAPI authentication and parsing
Community Python libraryNo channel access for public videosDepends on accessible captionsRequest a languageTimed segmentsInstall and handle retrieval errors
Managed scraperService accountDepends on accessible captionsConfigure target languagesStructured files or API resultsConfigure jobs and delivery

For scheduled batches, Thordata’s web scraper is a managed option.

Check the video and captions before requesting them

A YouTube transcript request needs a valid video ID, a requested language, and a caption track the retrieval method can access. Extract the ID before making the request, then distinguish a missing track from a malformed URL; otherwise, a batch job may report both as the same failure.

  1. Parse the URL rather than splitting its text on =. For a youtube.com/watch URL, read the v query parameter; for a youtu.be URL, read the first path segment. Reject an empty value, extra path segments, or an ID containing characters outside letters, digits, _, and -.
  2. Select the requested language before fetching captions. A video can have captions in one language but no accessible track in another.
  3. Record whether the available track was supplied manually or generated automatically. Track type and availability vary by video; do not assume every video has either one.

Use a Python transcript library for public videos

youtube-transcript-api can request available captions for a public video by identifier and preferred language without a YouTube API key. Check the installed version before running this example; it uses the YouTubeTranscriptApi().fetch() interface.

from importlib.metadata import version
from youtube_transcript_api import YouTubeTranscriptApi

print(version("youtube-transcript-api"))
transcript = YouTubeTranscriptApi().fetch("VIDEO_ID", languages=["en"])
segments = [
    {"text": item.text, "start": item.start, "duration": item.duration}
    for item in transcript
]
text = " ".join(item["text"] for item in segments)

A representative segment is {"text": "Welcome back", "start": 1.2, "duration": 2.4}. Save segments alongside text: joining captions discards the boundaries needed to locate a quotation in the video.

Handle disabled or unavailable captions as a missing transcript, and a missing requested language as a language mismatch. For temporary request failures, retry with a delay and a fixed attempt limit; do not loop indefinitely on videos that have no captions.

Use the official API for captions you can manage

The YouTube Data API can download a caption track when an OAuth-authorized account has permission to manage the video. Obtain an access token for that account before listing tracks; a video ID and API key alone are insufficient for the download. Use this route for captions on videos you control, rather than as a general public-video transcript endpoint.

  1. Call captions.list with the video ID and part=snippet to get the available tracks.
  2. Select a track by its language and whether it is a standard or automatically generated caption track. Keep its caption ID.
  3. Call captions.download with that caption ID. Set tfmt=srt if SRT is the file format your workflow needs, then save the response.

Public access to a video does not grant permission to download its captions through this API method.

Process transcripts across multiple videos

A retrieval queue makes multi-video transcript collection resumable. Store each video ID, requested language, retrieval status, and source method in a job record; mark a job complete only after its transcript is saved. Keep failures in the queue with a reason, so a later run can distinguish unavailable captions from a request that needs retrying.

Thordata’s YouTube web scraper is a managed option when maintaining that retrieval loop is impractical. It supports scheduled or batch input by keyword or channel or playlist ID, handles up to 10K URLs per batch, and provides transcripts in 100+ languages with JSON, CSV, or XLSX output.

Use API delivery when another service must ingest results automatically. Choose SFTP or AWS S3 file delivery when a batch pipeline already reads files from storage.

Store text, timing, and language consistently

Store each YouTube caption as a timed segment tied to a video identifier. A consistent record lets a program retrieve text by video, search within it, and align a match with playback. Keep the reported language and caption source alongside the words; neither can be reconstructed reliably from flattened text.

FieldStore
video_idYouTube video identifier
textSegment text
start_secondsPlayback offset
duration_secondsSegment length
languageReported caption language
caption_sourceOriginal or translated track

Save the segment records first. Derive plain text by joining their text values only when an index or downstream task needs it; retain start_seconds to link search hits back to playback. A web scraper can supply structured output for this workflow.

Use separate statuses for captions_missing, empty_response, and request_failed. A missing track is not a successful request that returned no text.

Troubleshoot failures and use transcripts responsibly

A programmatic YouTube transcript request can fail even when the video is public. Classify the failure before retrying: missing captions and unavailable languages need a different response from a temporary request error. Permission errors may require an authorized account or a different retrieval method, not another request with the same credentials.

FailureResponse
Absent captionsRecord “no accessible captions”; do not retry the same video.
Unavailable languageUse an available track only if the workflow permits it.
Permission errorStop; use an authorized route for captions you manage.
Transient request failureRetry a bounded number of times with increasing backoff.

Log the video ID, requested language, failure category, attempt count, and timestamp. For managed extraction workflows, Thordata’s Web Scraper is another option. Respect platform terms, video owners’ rights, and applicable privacy requirements when storing or reusing transcript text.

FAQ

Can I get a transcript when a video has no captions?

Yes, but the audio must be transcribed rather than retrieved as an existing caption track. An Apify YouTube Scraper option can generate AI transcripts for videos without subtitles; transcription options marked with a dollar sign cost extra. Generated text should be kept distinct from creator-supplied captions in the output.

Can I retrieve transcripts from YouTube Shorts?

Yes. Thordata’s YouTube Shorts scraper lists transcripts in 100+ languages and supports JSON, CSV and XLSX output. For scheduled collection by keyword or channel or playlist ID, use a scraper workflow rather than a script built around individual video URLs; Thordata also lists a Web Scraper for managed collection.

Can a transcript be returned as an SRT or WebVTT file?

Yes. One Apify YouTube Scraper returns transcripts as SRT or WebVTT, as well as timed JSON segments or plain text. Choose timed JSON when downstream code needs to attach metadata to individual segments; choose SRT or WebVTT when the output will be used as subtitles.

Can I request a transcript in another language?

Yes, if the retrieval tool supports track selection or translation. Apify’s YouTube Data Scraper accepts transcriptLanguage to select a track and translateTo to auto-translate; its transcript requests require US residential IPs. Store the requested language alongside the returned text so translated output is not mistaken for the original caption track.