YouTube captions contain valuable text data locked inside videos. Whether you need captions for accessibility, content analysis, or AI training, extracting them programmatically saves hours of manual work. This guide explains how to extract YouTube captions using the YouTubeTranscripts.co API, which handles both manual and auto-generated captions, plus AI transcription for videos with no captions at all.
Types of YouTube Captions
YouTube videos can have three types of captions: (1) Manual captions uploaded by the creator, which are the most accurate, (2) Auto-generated captions created by YouTube's speech recognition, which vary in quality, and (3) No captions at all, common for older or non-English videos. YouTubeTranscripts.co handles all three cases, using AI transcription as a fallback when no captions exist.
Extracting Timed Segments
The default format returns an array of timed segments, each with the text, start time (in seconds), and duration. This is useful when you need to link text to specific moments in the video.
import httpx
resp = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": "https://youtube.com/watch?v=VIDEO_ID"},
headers={"x-api-key": "YOUR_API_KEY"},
)
data = resp.json()
for segment in data["transcript"]:
minutes = int(segment["start"] // 60)
seconds = int(segment["start"] % 60)
print(f"[{minutes}:{seconds:02d}] {segment['text']}")Getting Plain Text
For NLP, AI, and text analysis, you usually want the full transcript as a single string. Use format=text to get the complete caption text without timestamps.
resp = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": "https://youtube.com/watch?v=VIDEO_ID", "format": "text"},
headers={"x-api-key": "YOUR_API_KEY"},
)
full_text = resp.json()["text"]
word_count = len(full_text.split())
print(f"Transcript: {word_count} words")Downloading SRT Subtitles
The SRT format is the industry standard for subtitles. Use format=srt to get properly formatted subtitle files compatible with all major video editors.
resp = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": "https://youtube.com/watch?v=VIDEO_ID", "format": "srt"},
headers={"x-api-key": "YOUR_API_KEY"},
)
srt_content = resp.json()["srt"]
with open("captions.srt", "w", encoding="utf-8") as f:
f.write(srt_content)Specifying a Language
For multilingual videos, you can request captions in a specific language using the lang parameter. The API supports 30+ languages.
# Get Spanish captions
resp = httpx.get(
"https://api.youtubetranscripts.co/v1/transcript",
params={"url": "https://youtube.com/watch?v=VIDEO_ID", "lang": "es"},
headers={"x-api-key": "YOUR_API_KEY"},
)Conclusion
Extracting YouTube captions is straightforward with the right API. YouTubeTranscripts.co gives you clean, structured captions in the format you need: timed segments for timestamp linking, plain text for NLP, or SRT for video editing. Get started with 150 free requests at youtubetranscripts.co.
Ready to start extracting YouTube transcripts?
Get 150 free API requests. No credit card required.
Get Your Free API Key