Summarize YouTube videos by extracting transcripts and generating structured notes. Use when the user wants to summarize a YouTube video, extract key points from a talk, create study notes from a lecture, or get timestamps for important moments. Supports multiple URL formats and languages.
Extract transcripts from YouTube videos and generate structured, actionable summaries using the STAR + R-I-S-E framework.
pip install youtube-transcript-api
If pip is not available, try:
pip3 install youtube-transcript-api
Verify installation:
from youtube_transcript_api import YouTubeTranscriptApi
print("youtube-transcript-api is ready")
YouTube URLs come in many formats. Parse all of them to extract the 11-character video ID:
| URL Format | Example | Extraction |
|---|---|---|
| Standard | https://www.youtube.com/watch?v=dQw4w9WgXcQ | Query param v |
| Short | https://youtu.be/dQw4w9WgXcQ | Path segment |
| Embed | https://www.youtube.com/embed/dQw4w9WgXcQ | Path after /embed/ |
| Live | https://www.youtube.com/live/dQw4w9WgXcQ | Path after /live/ |
| Shorts | https://www.youtube.com/shorts/dQw4w9WgXcQ | Path after /shorts/ |
| With timestamp | https://youtu.be/dQw4w9WgXcQ?t=120 | Ignore query params |
| With playlist | https://www.youtube.com/watch?v=dQw4w9WgXcQ&list=PLxx | Use only v param |
| Mobile | https://m.youtube.com/watch?v=dQw4w9WgXcQ | Same as standard |
| Bare ID | dQw4w9WgXcQ | Use directly |
import re
from urllib.parse import urlparse, parse_qs
def extract_video_id(url_or_id: str) -> str:
"""Extract YouTube video ID from various URL formats."""
url_or_id = url_or_id.strip()
# Bare video ID (11 chars, alphanumeric + dash + underscore)
if re.match(r'^[A-Za-z0-9_-]{11}$', url_or_id):
return url_or_id
parsed = urlparse(url_or_id)
# youtu.be short links
if parsed.hostname in ('youtu.be',):
return parsed.path.lstrip('/')
# Standard, mobile, embed, live, shorts
if parsed.hostname in ('www.youtube.com', 'youtube.com', 'm.youtube.com'):
if parsed.path == '/watch':
qs = parse_qs(parsed.query)
return qs.get('v', [None])[0]
for prefix in ('/embed/', '/live/', '/shorts/', '/v/'):
if parsed.path.startswith(prefix):
return parsed.path[len(prefix):].split('/')[0]
raise ValueError(f"Cannot extract video ID from: {url_or_id}")
from youtube_transcript_api import YouTubeTranscriptApi
def fetch_transcript(video_id: str, preferred_langs=None):
"""
Fetch transcript with language fallback.
Args:
video_id: The 11-character YouTube video ID
preferred_langs: Ordered list of language codes, e.g. ['zh-Hans', 'zh', 'en']
Defaults to ['en'] if not specified
Returns:
List of transcript segments with 'text', 'start', 'duration' keys
"""
if preferred_langs is None:
preferred_langs = ['en']
try:
transcript_list = YouTubeTranscriptApi.list_transcripts(video_id)
# Try manually created transcripts first (more accurate)
for lang in preferred_langs:
try:
transcript = transcript_list.find_manually_created_transcript([lang])
return transcript.fetch()
except Exception:
continue
# Fall back to auto-generated transcripts
for lang in preferred_langs:
try:
transcript = transcript_list.find_generated_transcript([lang])
return transcript.fetch()
except Exception:
continue
# Last resort: get any available transcript and translate
for transcript in transcript_list:
try:
if 'en' in preferred_langs:
return transcript.translate('en').fetch()
return transcript.fetch()
except Exception:
continue
raise RuntimeError("No transcript available in any language")
except Exception as e:
raise RuntimeError(f"Failed to fetch transcript for {video_id}: {e}")
Use the STAR + R-I-S-E combined framework for maximum clarity:
When generating the summary from the transcript, use this structure:
You are summarizing a YouTube video transcript. Follow these rules strictly:
1. TITLE & METADATA
- Video title (if known)
- Estimated duration from timestamps
- Primary topic/category
2. EXECUTIVE SUMMARY (2-3 sentences)
- The single most important takeaway
- Who should watch this and why
3. KEY POINTS (bulleted, with timestamps)
- [MM:SS] Point description
- Maximum 10 key points
- Each point should be self-contained and actionable
4. DETAILED NOTES (organized by topic/section)
- Group related content under headers
- Include specific examples, numbers, quotes
- Preserve technical accuracy
5. STAR ANALYSIS
- Situation: ...
- Task: ...
- Action: ...
- Result: ...
6. ACTIONABLE TAKEAWAYS
- What the viewer should do after watching
- Resources mentioned in the video
User says: "Summarize this video: https://youtube.com/watch?v=..."
User says: "I need detailed study notes from this lecture"
User says: "Summarize this Chinese/Japanese/Korean video in English"
User provides multiple URLs:
User says: "Find where they talk about X in this video"
[MM:SS](https://youtu.be/ID?t=SECONDS)# 📺 Video Summary: [Title]
**Duration**: ~XX minutes | **Language**: English | **Category**: Technology
## Executive Summary
[2-3 sentence overview]
## Key Points
- **[0:00]** Introduction and context setting
- **[2:15]** First major point with supporting evidence
- **[8:30]** Key technical concept explained
- **[15:00]** Practical demonstration
- **[22:45]** Conclusion and call to action
## Detailed Notes
### Section 1: [Topic] (0:00 - 5:30)
[Detailed notes with specific information]
### Section 2: [Topic] (5:30 - 12:00)
[Detailed notes with specific information]
## STAR Analysis
- **Situation**: [Context]
- **Task**: [Problem/Question]
- **Action**: [Methods/Steps]
- **Result**: [Outcomes/Conclusions]
## Actionable Takeaways
1. [First action item]
2. [Second action item]
3. [Third action item]
## Resources Mentioned
- [Resource 1](url)
- [Resource 2](url)
Problem: Some videos have no transcript (auto-generated or manual).
Solutions:
['en', 'en-US', 'en-GB']Error handling: