
Introduction
As a Python and AI enthusiast, I wanted to build something both simple and powerful. So I wrote a script that summarizes YouTube videos using transcripts and Gemini AI.
Table of Contents
ToggleIn this guide, I’ll show you:
How I built it
The Python packages I used
Step-by-step explanation of the code
How to fix beginner errors
How to get and use your Gemini API key
Perfect for beginners who want to start working with real-world AI tools in Python.
What This Tool Does
This tool lets you:
Paste a YouTube video link
Extract its transcript using
youtube-transcript-api
Generate a concise summary using Gemini AI
Output the summary in a clean format (Key Takeaways, Main Points, and Conclusion)
Prerequisites
Before starting, make sure you have the following:
Python 3.8 or above installed
Basic understanding of functions and imports in Python
A Google account to get your Gemini API key
Installing Required Packages
Open your terminal (or PowerShell) and run:
pip install youtube-transcript-api google-generativeai
This installs:
youtube-transcript-api
– to fetch video subtitlesgoogle-generativeai
– to interact with Gemini AI
Step-by-Step Code Explanation
Let’s break down the script into sections:
Import Libraries
from youtube_transcript_api import YouTubeTranscriptApi
import google.generativeai as genai
import re
These libraries handle transcript fetching, Gemini AI calls, and text processing.
Add Your Gemini API Key
gemini_api_key = "YOUR_GEMINI_API_KEY_HERE"
genai.configure(api_key=gemini_api_key)
This sets up access to Google Gemini.
Extract Video ID from YouTube URL
def extract_video_id(url):
pattern = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
match = re.search(pattern, url)
return match.group(1) if match else None
YouTube URLs look like https://www.youtube.com/watch?v=abcdef12345
. This function extracts the abcdef12345
part.
Get the Transcript
def get_transcript(video_id):
transcript = YouTubeTranscriptApi.get_transcript(video_id)
full_text = " ".join([item['text'] for item in transcript])
return full_text
This fetches subtitles and joins them into a single string.
Summarize the Transcript with Gemini AI
def summarize_text(text):
model = genai.GenerativeModel("gemini-2.0-flash")
prompt = f"""
Summarize the following YouTube video transcript into a well-structured summary with bullet points or clear sections, while keeping this short and simple:
- Key Takeaways
- Main Points Discussed
- Conclusion
Transcript:
{text}
"""
response = model.generate_content(prompt)
return response.text
This sends the full transcript to Gemini with a clear summarization prompt.
Clean the Gemini Output
def clean_gemini_output(raw_text):
cleaned = re.sub(r"\*\*(.*?)\*\*", lambda m: m.group(1).upper(), raw_text)
cleaned = re.sub(r"^\s*\*\s*", "- ", cleaned, flags=re.MULTILINE)
cleaned = re.sub(r"\*", "", cleaned)
cleaned = re.sub(r"\n{3,}", "\n\n", cleaned).strip()
return cleaned
This function formats the output:
Removes Markdown symbols
Cleans extra newlines
Makes output beginner-friendly
Run the Program
if __name__ == "__main__":
youtube_url = input("Enter the YouTube URL: ")
video_id = extract_video_id(youtube_url)
if not video_id:
print("Invalid YouTube URL.")
else:
transcript = get_transcript(video_id)
summary = summarize_text(transcript)
formatted_summary = clean_gemini_output(summary)
print("\n📄 Video Summary:\n")
print(formatted_summary)
This is the main driver. It gets the input, processes the transcript, and prints the summary.
Common Beginner Errors and Fixes
Error: ModuleNotFoundError: No module named 'api'
from api import gemini_api_key
But forgot to create the api.py
file.
Fix: Either:
Paste the API key directly in your script:
gemini_api_key = "YOUR_API_KEY"
OR: Create a file named
api.py
with that variable defined.
How to Get Your Gemini API Key
Follow these steps:
Sign in with your Google account
Click “Create API Key”
Copy and paste it into your code like this:
gemini_api_key = "AIzaSyA12345XXXXXXXXXXXX"
Full Python Code (Copy and Run)
from youtube_transcript_api import YouTubeTranscriptApi
import google.generativeai as genai
import re
# ✅ Insert your Gemini API Key here
gemini_api_key = "YOUR_GEMINI_API_KEY_HERE"
# Configure Gemini
genai.configure(api_key=gemini_api_key)
# Extracts video ID from the YouTube URL
def extract_video_id(url):
pattern = r"(?:v=|\/)([0-9A-Za-z_-]{11}).*"
match = re.search(pattern, url)
return match.group(1) if match else None
# Gets the video transcript
def get_transcript(video_id):
transcript = YouTubeTranscriptApi.get_transcript(video_id)
full_text = " ".join([item['text'] for item in transcript])
return full_text
# Sends the transcript to Gemini AI for summarization
def summarize_text(text):
model = genai.GenerativeModel("gemini-2.0-flash")
prompt = f"""
Summarize the following YouTube video transcript into a well-structured summary with bullet points or clear sections, while keeping this short and simple:
- Key Takeaways
- Main Points Discussed
- Conclusion
Transcript:
{text}
"""
response = model.generate_content(prompt)
return response.text
# Cleans the AI response for neat formatting
def clean_gemini_output(raw_text):
cleaned = re.sub(r"\*\*(.*?)\*\*", lambda m: m.group(1).upper(), raw_text)
cleaned = re.sub(r"^\s*\*\s*", "- ", cleaned, flags=re.MULTILINE)
cleaned = re.sub(r"\*", "", cleaned)
cleaned = re.sub(r"\n{3,}", "\n\n", cleaned).strip()
return cleaned
# Main program execution
if __name__ == "__main__":
youtube_url = input("Enter the YouTube URL: ")
video_id = extract_video_id(youtube_url)
if not video_id:
print("Invalid YouTube URL.")
else:
transcript = get_transcript(video_id)
summary = summarize_text(transcript)
formatted_summary = clean_gemini_output(summary)
print("\n📄 Video Summary:\n")
print(formatted_summary)
Example Output
KEY TAKEAWAYS:
- Python + Gemini AI can automate summarization.
- Useful for students, educators, and content creators.
MAIN POINTS:
- Extracting YouTube video transcript.
- Prompting Gemini for summary.
- Cleaning output for display.
CONCLUSION:
This script is a practical way to combine Python and AI for video analysis.

Conclusion
Creating this project from scratch helped me understand how:
APIs work in real projects
To fix import/module errors as a beginner
To format and clean AI output effectively
To integrate AI in practical tools
It’s a small but powerful project to build your Python + AI skills!
FAQs
What is Gemini AI?
Google’s next-gen large language model that powers text and reasoning tasks.
Does this work for all videos?
Only those with captions/subtitles available.
Can I run this in Jupyter or Colab?
Yes! But make sure you use input()
properly or hard-code the YouTube URL.
Is this beginner-friendly?
Absolutely. The code is written and explained to help beginners understand everything.