How To Summarize YouTube Videos Using AI and Python

How To Summarize YouTube Videos Using AI and Python

If you click our links and make a purchase, we may earn an affiliate commission. Learn more

I have been using my Raspberry Pi for many projects, and this time I decided to try out a project that involved AI. I have been using ChatGPT since it came out, and I felt it’s time I developed a tool that would consume one of its many APIs. After analyzing different projects, I decided to settle on developing a tool that could summarize YouTube videos for me.

Python can be used to consume the OpenAI APIs and use them to summarize YouTube videos according to the prompt given.

In this project, I will guide you step-by-step, taking you through all the steps from Python code to building a web interface for it. Don’t worry, even if you are just getting started or have no background in programming, I’ll make it easy for you.

If you’re like me and sometimes mix up syntax between programming languages, I’ve got just the thing for you. I’ve put together a Python cheat sheet with all the essential syntax in one place, so you can keep it handy and avoid any confusion. Download it here for free!

Requirements for AI Summary Project

Below are some of the things we will need for this project.

Key Prerequisites

  • Python installed (3.9+ recommended): If you are using the latest Raspberry Pi OS, then you are most likely on Python 3. However, you can verify that using the command below:
    python --version
    If you are not on Python 3, read this article and learn how to install the latest Python version on the Raspberry Pi.
  • A free OpenRouter account + API key (link): Instead of installing the OpenAI API package and using it directly, we will use an OpenAI API-compatible library from OpenRouter. I have written step-by-step instructions for obtaining an OpenRouter API key in the section below.
  • Basic familiarity with running Python scripts: Since we will be writing some Python code in this project, it would be great if you have a good background in Python scripting. For that, I would highly recommend getting our Python eBook, which will help you get started with building with Python in days, not months.

    Unlike other Python resources that bombard you with everything you can find in the Python language, this eBook focuses on teaching what you need to get started with Python on the Raspberry Pi. There is no boring theory; we jump directly into the sweet stuff.
  • Install dependencies: We will need a few tools for this project.
    First, you will need to install the Python Package Installer (pip):
    sudo apt install python3-pip
    Once done, install the necessary dependencies on your Raspberry Pi:
    pip3 install youtube-transcript-api openai flask

Obtaining an OpenRouter API Key

OpenRouter is a unified API service that provides developers with access to a wide range of large language models (LLMs) from various providers, including OpenAI.

To get started, visit the OpenRouter website on your browser. You will see an interface similar to the one shown in the image below. Click on the Models menu at the top.

Image

There are so many models listed on that website, but the exact library we will be using is Mistral: Mistral Small 3.1 24B (free). The main reason why I picked this particular model is that it’s free, optimized for reasoning tasks like summarization, and supports a large context window (128k tokens), which makes it ideal for handling long videos and transcripts.

You can decide to read more about this model, and when ready, scroll down the page until you see the Create API Key option. Click on this button.

Image

You will see an option to create an account. Create one, and an API key will be generated for you. Copy this key and save it for later use, as we will need it.

Step 1 – Import Project Dependencies

In this step, we will be importing dependencies that we will need for our project. To get started, use any editor to create a Python file, which you will name as youtube-summarizer.py.

In my case, I will be using the nano editor, a command-line-based editor. However, if you want a graphical editor, be sure to check our list of the 7 Best Code Editors for Programming on Raspberry Pi.

As a norm, before writing your Python code, you first need to import all the dependencies you need for your project. In our case, we need the dependencies below:
import os
import re
import sys
from youtube_transcript_api import YouTubeTranscriptApi
from openai import OpenAI

Explanation

The first module we are importing is the OS module. We need it to access environment variables like the OpenRouter_API_Key since we won’t be writing that directly into our code for security purposes.

re is the regular expression module. For this project, we will be using it to extract the Video ID from the provided YouTube URL. Remember, the normal YouTube URL has this structure – https://www.youtube.com/watch?v=obJOwEy62bk.

The video ID is stored in this last parameter, v=obJOwEy62bk. The only way we can easily extract that ID is by using a regular expression, as you will learn below.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now

sys gives us access to command-line arguments. How this works is that we will write this script in a way that takes a command-line argument. That argument in this case will be a YouTube URL. We will learn more about this in Step 5 below.

youtube_transcript_api is the Python library that will enable us to access YouTube captions from the backend. Lastly, the OpenAI module enables us to connect to the OpenRouter model.

The resulting code for this section should look as shown in the image below.

Image

Step 2 – Configure OpenRouter Client

We have already looked at how to obtain the OpenRouter API key in the prerequisites. Now, in this particular section, we will look at how we will use this API key in our project.

If you are just getting started with programming, let me give you a hint that will help you a long way. We don’t hardcode API keys into our project. Well, you can if you are doing some tests, but in production, you will have to use environmental variables.

Now, in your terminal, run this command to add your API key as an environmental variable:
export OPENROUTER_API_KEY="your_api_key_here"

Once done, go back to your editor where you are writing the Python code and add these lines:
#--- Configure OpenRouter Client ---
client = OpenAI(
api_key=os.environ.get("OPENROUTER_API_KEY"),
base_url="https://openrouter.ai/api/v1"
)

Explanation

Let us try to understand what exactly this code is doing.

Basically, this code is setting up a connection to the OpenAI API through OpenRouter. It pulls our API key from the computer’s environment (remember where I mentioned environment variables above) and sets the API’s web address. The result is a client object that we will use to send requests.

Image

Step 3 – Extracting YouTube Transcripts

I believe we are doing well up to this point. So far, we have covered three major things:

  • We created an API key on OpenRouter.
  • We created a Python file and imported project dependencies.
  • We have written the code to configure the API.

In this section, we will be looking at how to extract YouTube transcripts. However, let us first understand how YouTube captions work, why you can’t just scrape them that easily, and why using the youtube-transcript-api is the simplest way.

Basically, YouTube automatically generates captions for most videos. Unfortunately, these captions aren’t like some plain text on a page where you can just access and copy them. They are generated and displayed dynamically, which makes scraping quite hard and a messy process.

Even if you are a developer and try the inspect element trick, you will still have a hard time accessing them in between the HTML files. And that is the reason why we always use a library like youtube-transcript-api.

This script directly connects to YouTube’s caption data and returns a clean transcript that we can proceed to use in our project.

Full Code

Now that you have a good understanding of how YouTube captions work, let’s get started writing code.

Grab my Python cheat sheet!
If like me, you always mix the languages syntax, download my cheat sheet for Python here!
Download now
# --- Extract Transcript from YouTube ---
def get_transcript(video_url: str) -> str:
    # Extract video ID from URL
    match = re.search(r"v=([a-zA-Z0-9_-]{11})", video_url)
    if not match:
        raise ValueError("Invalid YouTube URL. Must contain ?v=VIDEO_ID")

    video_id = match.group(1)

    # Create an instance and fetch transcript
    ytt_api = YouTubeTranscriptApi()
    fetched_transcript = ytt_api.fetch(video_id)

    # Join all snippets into a single string
    return " ".join(snippet.text for snippet in fetched_transcript)

Explanations

The code above might look somewhat complicated, but the logic is quite simple. We are writing a function called get_transcript that takes a YouTube video link and pulls out the transcript (subtitles) in plain text.

First, we write a regular expression to find the video’s unique ID from the URL. If the URL doesn’t have a valid ID, it raises an error.

# Extract video ID from URL
    match = re.search(r"v=([a-zA-Z0-9_-]{11})", video_url)
    if not match:
        raise ValueError("Invalid YouTube URL. Must contain ?v=VIDEO_ID")

We then call the YouTubeTranscriptApi to fetch the transcript of that video. The transcript comes in small pieces (snippets), so the function combines all of them into one long string of text and returns it.

# Create an instance and fetch transcript
    ytt_api = YouTubeTranscriptApi()
    fetched_transcript = ytt_api.fetch(video_id)

    # Join all snippets into a single string
    return " ".join(snippet.text for snippet in fetched_transcript)

The resulting code should look something similar to the image below.

Image

Step 4 – Using AI to Summarize

In the previous step, we learnt how to extract YouTube captions. In this section, we will learn how to summarize these captions using AI.

After obtaining the transcript, we now need to summarize it using the model we are using from OpenRouter – Mistral Small 3.1 24B. It’s in this section that we will also provide a prompt that the AI will use to summarize the video.

For example, in our case, we need to get a one-line summary of the video and five bullet points as key takeaways.

Full Code

Here is the code that we will use to summarize the video. Basically, we are creating a function that takes in a block of text (in this case, a YouTube transcript) and generates a summary using an AI model.

def summarize_text(text: str) -> str:
    """
    Send transcript to Mistral Small 3.1 24B (via OpenRouter) and get summary.
    """
    prompt = f"""
    Summarize the following transcript in one concise sentence,
    then list 5 key takeaways as bullet points:

    {text[:5000]}  # limit to first 5000 characters for token safety
    """

    response = client.chat.completions.create(
        model="mistralai/mistral-small-3.2-24b-instruct:free",
        messages=[{"role": "user", "content": prompt}]
    )

    return response.choices[0].message.content

Explanations

First, we write a prompt asking the model to summarize the transcript into one short sentence and then list five key takeaways as bullet points. And in order to prevent the model from being overloaded, the function limits the input text to the first 5000 characters.

prompt = f"""
    Summarize the following transcript in one concise sentence,
    then list 5 key takeaways as bullet points:

    {text[:5000]}  # limit to first 5000 characters for token safety
    """

The prompt is then sent to the Mistral Small 3.2 model via the OpenRouter API using the client.chat.completions.create method. Finally, the function returns the model’s response, which contains the requested summary and bullet points.

response = client.chat.completions.create(
        model="mistralai/mistral-small-3.2-24b-instruct:free",
        messages=[{"role": "user", "content": prompt}]
    )

    return response.choices[0].message.content

The resulting code should look something similar to the image below.

Image

Step 5 – Creating a Command-Line Script

We are doing well up to this point. So far, we have covered:

  • How to import dependencies into our project.
  • Configure the OpenRouter API.
  • Extract YouTube captions.
  • Summarize the generated captions according to our custom prompt.

Now we will write the code that will allow us to run our program and enable it to take a command-line argument. In this case, the argument will be a YouTube video URL.
To give you a better understanding of what I mean, look at the command below:
python3 youtube-summarizer.py "<YouTube_URL>"

As you can see, the above line is executing a Python program called youtube-summarizer.py. But the program is also taking an argument, which is the YouTube video URL that you want to summarize.

Full Code

Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

Let us write the code that will help us achieve that feature.

def main():
    if len(sys.argv) < 2:
        print("Usage: python youtube_transcript_summarizer.py <youtube_url>")
        sys.exit(1)

    url = sys.argv[1]
    print(f"\nFetching transcript for: {url}\n")
    try:
        transcript = get_transcript(url)
        summary = summarize_text(transcript)

        print("=== Video Summary ===\n")
        print(summary)

    except Exception as e:
        print(f"Error: {e}")


if __name__ == "__main__":
    main()

Explanation

This code defines the main entry point of a Python program that summarizes YouTube videos. It first checks if the user has provided a YouTube URL as a command-line argument. If not, it shows the correct usage and exits.

if len(sys.argv) < 2:
        print("Usage: python youtube_transcript_summarizer.py <youtube_url>")
        sys.exit(1)

If a URL is given, the program fetches the video’s transcript using the get_transcript function, then passes that transcript to summarize_text, which generates a summary using an AI model.

url = sys.argv[1]
    print(f"\nFetching transcript for: {url}\n")
    try:
        transcript = get_transcript(url)
        summary = summarize_text(transcript)

Finally, it prints the summary in a clear format.

print("=== Video Summary ===\n")
        print(summary)

You will also notice that in this code, we are using try-except to handle any arising errors. If anything goes wrong (like an invalid URL or missing transcript), the program catches the error and displays it instead of crashing.

Image

From the image above, you can see that I executed the program, provided a YouTube video URL, and got a really good summary, which followed the prompt we provided in our code in Step 4.

Step 6 – Add a Simple Web Interface

So far, we’ve been running our summarizer through the command line. That’s fine for developers, but wouldn’t it be nice if anyone could just paste a YouTube link into a webpage and instantly get the summary?

Since our project is developed using Python, it would be best if we used one of the many Python frameworks for building graphical user interfaces. The main ones include PyQT, Flask and Tkinter.

In this section, we’ll make a webpage for our summarizer using Flask. It’s a Python framework that allows us to create interactive sites and their backends with minimal setup.

Setting up Flask

First, we’ll need Flask installed:
pip3 install flask

That is all you need. Flask is lightweight and doesn’t need much setup.

Creating the Backend (gui-youtube-summarizer.py)

Let’s reuse the transcript and summarization functions we already wrote. The only difference is that instead of grabbing a YouTube URL from the command line, we’ll accept it from a web form.

First, create a Python file called app.py.

Once done, copy the code below and paste it into the new app.py file you just created:

from flask import Flask, render_template, request
import os, re
from youtube_transcript_api import YouTubeTranscriptApi
from openai import OpenAI

app = Flask(__name__)

# --- OpenRouter Client ---
client = OpenAI(
    api_key=os.environ.get("OPENROUTER_API_KEY"),
    base_url="https://openrouter.ai/api/v1"
)

# --- Reuse transcript extraction ---
def get_transcript(video_url: str) -> str:
    match = re.search(r"v=([a-zA-Z0-9_-]{11})", video_url)
    if not match:
        raise ValueError("Invalid YouTube URL. Must contain ?v=VIDEO_ID")
    video_id = match.group(1)
    ytt_api = YouTubeTranscriptApi()
    fetched_transcript = ytt_api.fetch(video_id)
    return " ".join(snippet.text for snippet in fetched_transcript)

# --- Reuse summarization ---
def summarize_text(text: str) -> str:
    prompt = f"""
    Summarize the following transcript in one concise sentence,
    then list 5 key takeaways as bullet points:

    {text[:5000]}
    """
    response = client.chat.completions.create(
        model="mistralai/mistral-small-3.2-24b-instruct:free",
        messages=[{"role": "user", "content": prompt}]
    )
    return response.choices[0].message.content

# --- Flask routes ---
@app.route("/", methods=["GET", "POST"])
def index():
    summary = None
    error = None
    if request.method == "POST":
        url = request.form.get("youtube_url")
        try:
            transcript = get_transcript(url)
            summary = summarize_text(transcript)
        except Exception as e:
            error = str(e)
    return render_template("index.html", summary=summary, error=error)

if __name__ == "__main__":
    app.run(debug=True)

Note: If you want the web page to be reachable from another device on your network, you can replace:
app.run(debug=True)
with something like:
app.run(host="0.0.0.0", port=5000, debug=True)
Yes, you can also use another port if needed.

Creating the Frontend (templates/index.html)

Lost in the terminal? Grab My Pi Cheat-Sheet!
Download the free PDF, keep it open, and stop wasting time on Google.
Download now

Now, let’s add a simple HTML page where users can paste their YouTube link. Follow the steps below if you are using the terminal.

First, create a folder called templates using the command below:
mkdir templates

Once done, navigate inside this folder using the cd command:
cd templates

Now we will need to create a new HTML file inside the templates folder (Flask automatically looks there for HTML files):
nano index.html

Now, copy the code below and paste it into the index.html file you just created:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>YouTube Summarizer</title>
  <style>
    body { font-family: Arial, sans-serif; margin: 2rem; }
    form { margin-bottom: 1rem; }
    textarea { width: 100%; height: 200px; margin-top: 1rem; }
    .error { color: red; }
  </style>
</head>
<body>
  <h1> YouTube Summarizer</h1>
  <form method="POST">
    <label for="youtube_url">Paste YouTube URL:</label><br>
    <input type="text" id="youtube_url" name="youtube_url" style="width: 80%;" required>
    <button type="submit">Summarize</button>
  </form>

  {% if error %}
    <p class="error">Error: {{ error }}</p>
  {% endif %}

  {% if summary %}
    <h2>Summary</h2>
    <textarea readonly>{{ summary }}</textarea>
  {% endif %}
</body>
</html

Running the Web App

Once you have done every step above, run the command below to launch your web application:
python3 app.py

Image

Flask will start a local server at http://127.0.0.1:5000. Open it in your browser, paste a YouTube link, and you’ll get a neat summary right on the page.

Image

Good work on finishing all the steps for this project! This concludes our tutorial, but if you’d like to take this project further, check out the next section for some ideas.


🛠 This tutorial doesn't work anymore? Report the issue here, so that I can update it!

If you enjoy learning about Raspberry Pi, you’ll feel right at home in the RaspberryTips Community. It’s a friendly group of makers helping each other grow. Join us today for $1 and see what it’s like inside.

Improvements & Next Steps

From the results we got both from the command-line summarizer and the web-based summarizer, this project is working perfectly. But there are a few areas that you can go ahead and improve.

Improving the User Interface (UI)

Let’s be honest, the current user interface is more than basic. There is so much that we can do on this part. You can add some colors, more features, like maybe adding a history part where users can view previous searches, and much more.

Handling Long Videos

This project works perfectly for short videos. But if we want to summarize videos that are more than 1 hour long, then you’ll be dealing with very long transcripts, which may exceed the size limits that our AI can handle.

To fix that, we can modify the code in such a way that the transcript is first split into smaller parts and then summarized individually. The summarized parts can then be combined into a final summary.

If you do that, trust me, you can even put a subscription plan and host this project online.

Troubleshooting Common Errors

We are techies, but the people who will be interacting with our project might not be. So we can improve this project so it handles errors occurring on the user’s end and gives clear messages in response to these errors.

Some common errors that they can face include invalid links, missing transcripts, or API limits. To handle such cases, the project can display helpful message responses like “double-check the link” or “try again later” to reduce confusion.

Whenever you’re ready, here are other ways I can help you:

Test Your Raspberry Pi Level (Free): Not sure why everything takes so long on your Raspberry Pi? Take this free 3-minute assessment and see what’s causing the problems.

The RaspberryTips Community: Need help or want to discuss your Raspberry Pi projects with others who actually get it? Join the RaspberryTips Community and get access to private forums, exclusive lessons, and direct help (try it for just $1).

Master your Raspberry Pi in 30 days: If you are looking for the best tips to become an expert on Raspberry Pi, this book is for you. Learn useful Linux skills and practice multiple projects with step-by-step guides.

Master Python on Raspberry Pi: Create, understand, and improve any Python script for your Raspberry Pi. Learn the essentials step-by-step without losing time understanding useless concepts.

You can also find all my recommendations for tools and hardware on this page.

Similar Posts