How to summarize a YouTube video with an LLM in Python
Checked on 2026-11-18.
A language model cannot watch a video, but it reads a transcript very well. So summarizing a YouTube video is two steps: get the transcript, then ask a model to summarize it. Here is the complete code.
Step 1: get the transcript
Any of the routes in the Python transcript guide works, including the free library on your own computer. This example uses Pipefetch, because it also runs on a server and returns the title and channel along with the text.
# pip install requests anthropic
import os
import requests
def get_transcript(video: str) -> dict | None:
res = requests.get(
"https://pipefetch.com/v1/youtube/transcript",
params={"video": video},
headers={"x-api-key": os.environ["PIPEFETCH_API_KEY"]},
timeout=90,
)
if res.status_code == 404:
return None # the video has no captions; not charged
res.raise_for_status()
return res.json()["data"]Step 2: send it to a model
This example uses Claude through Anthropic's Python SDK. The same idea works with any model that takes a long prompt.
import anthropic
client = anthropic.Anthropic() # reads ANTHROPIC_API_KEY
def summarize(video: str) -> str | None:
data = get_transcript(video)
if data is None:
return None
prompt = (
"Summarize this YouTube video for someone deciding whether to watch it.\n"
"Give one sentence on what it is about, then the main points as a short list.\n\n"
f"Title: {data['title']}\n"
f"Channel: {data['channel']['name']}\n\n"
f"Transcript:\n{data['text']}"
)
with client.beta.messages.stream(
model="claude-opus-5-5",
max_tokens=16000,
betas=["server-side-fallback-2026-07-01"],
fallbacks="default",
messages=[{"role": "user", "content": prompt}],
) as stream:
message = stream.get_final_message()
if message.stop_reason == "refusal":
return None # the model declined this transcript
return "".join(block.text for block in message.content if block.type == "text")
print(summarize("https://www.youtube.com/watch?v=jNQXAC9IVRw"))Three choices in that call are worth knowing. It streams and collects the final message, which avoids timeouts on long transcripts. The fallbacks setting lets the API hand a request the first model declines to another model instead of failing. And the code checks stop_reason before reading the text, because a transcript can contain anything.
Long videos
Speech runs at roughly 150 words a minute, so a ten-minute video is about 1,500 words and a three-hour podcast about 27,000. Current models with large context windows take that in one request, so you do not need to cut the transcript into pieces first. If you use a model with a small context window, split the transcript by the video's chapters, which the transcript answer includes in chapters when the video has them, summarize each part, then summarize the summaries.
Summaries that point to moments
Ask for the transcript with format set to timestamped and the text comes with a marker such as [0:12] in front of each line. Tell the model to quote those markers next to each point, and readers can jump to the right moment.
Many videos
For a list of videos, loop over them and keep going when one has no captions: that case is a normal answer, not an error, and costs nothing. Each transcript is 1 credit. Automatic captions have no punctuation and misspell names, so for names that matter, include the video's title and channel in the prompt, as the example does.
Try it with 100 free credits. No card, no Google Cloud project, no OAuth. Get an API key or read the docs.
APIs used in this guide
-
1 credit
YouTube Transcript API
The transcript of any public video with captions, as plain text, timestamped segments, SRT or WebVTT.
GET /v1/youtube/transcript -
1 credit
YouTube Video API
Exact views, likes and comment count, description, tags, chapters and channel for one video.
GET /v1/youtube/video
More guides
- How to get a YouTube transcript in Python
- How to get a YouTube transcript in Node.js
- YouTube API quota exceeded: what the error means and how to fix it
- How to find a YouTube channel ID from a handle or URL
- How to download YouTube subtitles as SRT or VTT
- youtube-transcript-api RequestBlocked or IpBlocked: why it happens and how to fix it
- How to get YouTube comments in Python
- How to search YouTube from Python
- How to list a YouTube channel's videos in Python