How to beginner · 4 min read

How to summarize YouTube video transcript

Quick answer
Use the OpenAI Python SDK to send the YouTube transcript text as input to a chat model like gpt-4o with a prompt to summarize it. Extract the transcript (via YouTube API or third-party tools), then call client.chat.completions.create with the transcript in the messages array and parse the summary from the response.

PREREQUISITES

  • Python 3.8+
  • OpenAI API key (free tier works)
  • pip install openai>=1.0

Setup

Install the openai Python package and set your OpenAI API key as an environment variable.

  • Install package: pip install openai
  • Set environment variable in your shell: export OPENAI_API_KEY='your_api_key' (Linux/macOS) or setx OPENAI_API_KEY "your_api_key" (Windows)
bash
pip install openai
output
Collecting openai
  Downloading openai-1.x.x-py3-none-any.whl (xx kB)
Installing collected packages: openai
Successfully installed openai-1.x.x

Step by step

Extract the YouTube transcript text (using YouTube API or third-party libraries), then send it to the OpenAI gpt-4o chat model with a prompt to summarize. The example below assumes you have the transcript as a string.

python
import os
from openai import OpenAI

# Initialize OpenAI client
client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])

# Example YouTube transcript text (replace with actual transcript)
youtube_transcript = "In this video, we explore the basics of AI summarization techniques..."

# Prepare messages for chat completion
messages = [
    {"role": "user", "content": f"Summarize the following YouTube video transcript:\n\n{youtube_transcript}"}
]

# Call the chat completion endpoint
response = client.chat.completions.create(
    model="gpt-4o",
    messages=messages
)

# Extract summary text
summary = response.choices[0].message.content
print("Summary:", summary)
output
Summary: This video explains the fundamentals of AI summarization techniques, covering key concepts and practical approaches to effectively condense information.

Common variations

  • Async usage: Use async with await client.chat.completions.create(...) for asynchronous calls.
  • Streaming: Enable stream=True to receive partial summary tokens as they generate.
  • Different models: Use lighter models like gpt-4o-mini for faster, cheaper summaries.
  • Chunking: For long transcripts, split text into chunks and summarize each, then combine summaries.
python
import asyncio
import os
from openai import OpenAI

async def async_summarize(transcript: str):
    client = OpenAI(api_key=os.environ["OPENAI_API_KEY"])
    messages = [{"role": "user", "content": f"Summarize the following YouTube video transcript:\n\n{transcript}"}]
    response = await client.chat.completions.create(
        model="gpt-4o-mini",
        messages=messages
    )
    return response.choices[0].message.content

# Example async call
if __name__ == "__main__":
    transcript = "This is a sample transcript for async summarization."
    summary = asyncio.run(async_summarize(transcript))
    print("Async summary:", summary)
output
Async summary: This sample transcript demonstrates how to perform asynchronous summarization using the OpenAI API.

Troubleshooting

  • If you get 401 Unauthorized, verify your OPENAI_API_KEY environment variable is set correctly.
  • If the transcript is too long, split it into smaller chunks to avoid token limits.
  • For incomplete summaries, increase max_tokens parameter in chat.completions.create.
  • Check network connectivity if requests time out or fail.

Key Takeaways

  • Use the OpenAI Python SDK with gpt-4o to summarize YouTube transcripts efficiently.
  • Extract transcripts externally, then send them as user messages for summarization.
  • Handle long transcripts by chunking to respect token limits.
  • Async and streaming calls improve responsiveness for large inputs.
  • Always set your API key securely via environment variables.
Verified 2026-04 · gpt-4o, gpt-4o-mini
Verify ↗