Disclosure: I made the Apify Actor mentioned at the end, so I'm biased. Apify links here are referral links. Everything before that section is free and uses only youtube-transcript-api.
You want a YouTube video's captions as a real subtitle file (.srt or .vtt) that a video player or editor can load, not a wall of text. With Python this is about twenty lines. Below is the script, what I checked in the output, and the two or three things that trip people up. Everything was tested on 26 November 2026 with youtube-transcript-api 1.2.5 (the current version on PyPI).
The free route: fetch, format, write
youtube-transcript-api ships SRTFormatter and WebVTTFormatter, so you don't need to build the files by hand:
"""Save YouTube captions as .srt and .vtt files with youtube-transcript-api."""
import re
import sys
from pathlib import Path
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import SRTFormatter, WebVTTFormatter
api = YouTubeTranscriptApi()
def video_id(url_or_id):
m = re.search(r"(?:v=|youtu\.be/|shorts/|embed/)([\w-]{11})", url_or_id)
return m.group(1) if m else url_or_id
def save_subtitles(url_or_id, languages=("en",), out_dir="subs"):
vid = video_id(url_or_id)
transcript = api.fetch(vid, languages=list(languages))
kind = "auto" if transcript.is_generated else "manual"
out = Path(out_dir)
out.mkdir(exist_ok=True)
stem = f"{vid}.{transcript.language_code}.{kind}"
# encoding="utf-8" matters: captions contain characters like ♪ and non-Latin scripts
(out / f"{stem}.srt").write_text(SRTFormatter().format_transcript(transcript), encoding="utf-8")
(out / f"{stem}.vtt").write_text(WebVTTFormatter().format_transcript(transcript), encoding="utf-8")
return stem, len(transcript.snippets)
if __name__ == "__main__":
import argparse
p = argparse.ArgumentParser()
p.add_argument("videos", nargs="+", help="video URLs or 11-character IDs")
p.add_argument("--lang", default="en", help="comma-separated language codes, in order of preference")
args = p.parse_args()
for arg in args.videos:
stem, n = save_subtitles(arg, languages=args.lang.split(","))
print(f"{stem}: {n} cues")
Run it:
pip install youtube-transcript-api
python save_subs.py aircAruvnKk https://youtu.be/dQw4w9WgXcQ
python save_subs.py https://www.youtube.com/watch?v=9bZkp7q19f0 --lang ko
Output when I ran it:
aircAruvnKk.en.manual: 286 cues
dQw4w9WgXcQ.en.manual: 61 cues
9bZkp7q19f0.ko.auto: 67 cues
The start of dQw4w9WgXcQ.en.manual.srt:
1
00:00:01,360 --> 00:00:03,040
[♪♪♪]
2
00:00:18,640 --> 00:00:21,880
♪ We're no strangers to love ♪
3
00:00:22,640 --> 00:00:26,960
♪ You know the rules
and so do I ♪
A cue with two lines (the third one above) is normal in SRT. I also loaded all three .srt files with ffmpeg and converted them to WebVTT; it parsed every file and the number of cues matched (286, 61 and 67).
Gotcha 1: always write with encoding="utf-8"
Captions contain characters like ♪ and non-Latin scripts (the Korean file above). If you use plain open(path, "w"), Python uses the system's default text encoding, which on many Windows setups is a legacy code page such as cp1252. I simulated that by passing encoding="cp1252" to write_text(), and the ♪ from the Rick Astley captions fails with:
UnicodeEncodeError: 'charmap' codec can't encode character '\u266a' in position 2: character maps to <undefined>
The script above passes encoding="utf-8" explicitly, which avoids it on every platform.
Gotcha 2: overlapping cues in auto-generated captions (the library already handles this)
Auto-generated captions are delivered with start and duration values that overlap the next line, because YouTube builds them as rolling text. Subtitle files with overlapping cues can look wrong in players and editors. I measured this on the English tracks of three videos, comparing the raw snippets with the cue times in the finished SRT:
"""How much do raw caption times overlap, and does the formatter output still overlap?"""
import re
from youtube_transcript_api import YouTubeTranscriptApi
from youtube_transcript_api.formatters import SRTFormatter
api = YouTubeTranscriptApi()
def secs(t):
h, m, r = t.split(":"); s, ms = r.split(",")
return int(h) * 3600 + int(m) * 60 + int(s) + int(ms) / 1000
for vid in ["aircAruvnKk", "dQw4w9WgXcQ", "UF8uR6Z6KLc"]:
for track in api.list(vid):
if track.language_code != "en":
continue
t = track.fetch()
sn = t.snippets
raw = sum(1 for a, b in zip(sn, sn[1:]) if a.start + a.duration > b.start + 0.001)
srt = SRTFormatter().format_transcript(t)
times = re.findall(r"(\S+) --> (\S+)", srt)
out = sum(1 for (s1, e1), (s2, e2) in zip(times, times[1:]) if secs(e1) > secs(s2) + 0.001)
kind = "auto" if track.is_generated else "manual"
print(f"{vid} {kind:6} cues={len(sn):4} raw overlapping pairs={raw:4} overlapping pairs in SRT={out}")
aircAruvnKk manual cues= 286 raw overlapping pairs= 0 overlapping pairs in SRT=0
aircAruvnKk auto cues= 533 raw overlapping pairs= 525 overlapping pairs in SRT=0
dQw4w9WgXcQ manual cues= 61 raw overlapping pairs= 0 overlapping pairs in SRT=0
dQw4w9WgXcQ auto cues= 52 raw overlapping pairs= 43 overlapping pairs in SRT=0
UF8uR6Z6KLc manual cues= 244 raw overlapping pairs= 0 overlapping pairs in SRT=0
UF8uR6Z6KLc auto cues= 341 raw overlapping pairs= 331 overlapping pairs in SRT=0
So the raw auto captions overlap almost everywhere (525 of 532 neighbouring pairs for the first video), and the formatter's output has none. In the library's source, the formatter ends each cue at the next cue's start when the next one begins earlier. You don't need your own fix, but it's good to know it's there if you ever build the file yourself from transcript.snippets.
Gotcha 3: the language code has to match
api.fetch() asks for English by default. For the Korean auto-caption track above, fetch("9bZkp7q19f0") raises NoTranscriptFound, so the script takes --lang ko. Region variants like de-DE or pt-BR also need to be asked for exactly. I covered the full list of causes in NoTranscriptFound vs TranscriptsDisabled.
Also, the file name in the script includes auto or manual. That matters when you pick files for editing: auto captions contain recognition mistakes, human-made captions usually don't.
When you want many videos, or you're blocked on a server
The loop above works for a handful of videos from your own machine. From a cloud server, YouTube often blocks requests (see RequestBlocked on a cloud server), and for playlists you also need the list of video IDs first (playlist walkthrough).
The hosted option: an Apify Actor with includeSrt
I built YouTube Transcript Scraper for the many-videos case. It takes video, playlist or channel URLs, and with includeSrt switched on every row has an srt string next to the metadata. It's pay-per-event: $0.004 per transcript delivered on the free and Bronze plans (checked in the live pricing today), and videos with no captions are not charged. Create a free Apify account if you don't have one.
import os
from pathlib import Path
from apify_client import ApifyClient
client = ApifyClient(os.environ["APIFY_TOKEN"])
run = client.actor("logicbase_systems/youtube-transcript").call(run_input={
"videoUrls": [
"https://www.youtube.com/watch?v=aircAruvnKk",
"https://www.youtube.com/watch?v=9bZkp7q19f0",
],
"languages": ["en", "ko"],
"includeSrt": True,
"includeSegments": False, # we only want the .srt text here
})
out = Path("subs_actor")
out.mkdir(exist_ok=True)
for row in client.dataset(run.default_dataset_id).iterate_items():
if row["status"] != "ok":
print(row["videoId"], "skipped:", row["error"]["code"])
continue
kind = "auto" if row["isAutoGenerated"] else "manual"
path = out / f"{row['videoId']}.{row['language']}.{kind}.srt"
path.write_text(row["srt"], encoding="utf-8")
print(path, row["segmentCount"], "cues")
Output when I ran exactly this:
subs_actor/aircAruvnKk.en.manual.srt 286 cues
subs_actor/9bZkp7q19f0.ko.auto.srt 67 cues
The run took about 21 seconds for two videos, cost about $0.0013 in total (including the small start fee), and both files also parsed in ffmpeg. Compared with the library's files, the Korean one was byte-for-byte identical. The 3Blue1Brown one differed in two cues: where the library keeps a two-line cue, the Actor puts the two lines on one line. The cue count and timings were the same.
Straight answer: for one or a few videos from your own machine, the free script is all you need. The Actor is for the cases where you have a big list, or your server gets blocked, and you'd rather not run the fetching yourself.
Top comments (0)