A Python script that retrieves comprehensive metadata from YouTube videos using yt-dlp.
# Install dependency
pip install yt-dlp
# Retrieve metadata from any YouTube video
python youtube_metadata.py "https://youtu.be/AuCUZ9bSbao"
# Save metadata to JSON file
python youtube_metadata.py "https://youtu.be/AuCUZ9bSbao" --json- Key Features
- Installation
- Usage
- Supported URL Formats
- Command Line Options
- Output Format
- Metadata Fields
- JSON Output
- How It Works
- Error Handling
- Dependencies
- Examples
- Troubleshooting
- Comprehensive Metadata: Extract title, channel, views, likes, duration, resolution, tags, description, and more
- Exact Publish Time: Get both local time and UTC time of video publication
- Beautiful Output: Formatted console display with emoji icons and boxed sections
- Smart URL Parsing: Automatically handles all YouTube URL formats
- Optional JSON Export: Save metadata to a JSON file for further processing
- No API Key Required: Uses yt-dlp instead of YouTube Data API
| Requirement | Version | Description |
|---|---|---|
| Python | 3.7+ | Python runtime |
| pip | Latest | Python package manager |
pip install yt-dlpThat's it! No YouTube API key needed.
# Display metadata (default behavior)
python youtube_metadata.py <video_url_or_id>
# With video ID only
python youtube_metadata.py dQw4w9WgXcQ
# With full URL
python youtube_metadata.py "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Save to JSON file
python youtube_metadata.py "https://youtu.be/AuCUZ9bSbao" --jsonThe script automatically extracts the video ID from any of these formats:
| Format | Example |
|---|---|
| Standard URL | https://www.youtube.com/watch?v=VIDEO_ID |
| Short URL | https://youtu.be/VIDEO_ID |
| Shorts | https://www.youtube.com/shorts/VIDEO_ID |
| Embed | https://www.youtube.com/embed/VIDEO_ID |
| Mobile | https://m.youtube.com/watch?v=VIDEO_ID |
| Music | https://music.youtube.com/watch?v=VIDEO_ID |
| Video ID Only | dQw4w9WgXcQ |
| Option | Short | Description |
|---|---|---|
url |
(required) | YouTube video URL or video ID |
--json |
-j |
Save metadata to JSON file |
--help |
-h |
Show help message |
The script displays metadata in a formatted, boxed layout:
┌─────────────────────────────────────────────────────────┐
│ we got lost in China
└─────────────────────────────────────────────────────────┘
📹 Video ID : AuCUZ9bSbao
🔗 URL : https://www.youtube.com/watch?v=AuCUZ9bSbao
┌─ 📅 PUBLISH INFO ─────────────────────────────────────┐
│ 📆 Date : 20260406
│ ⏰ Local Time : 03:31:25 PM
│ 🌐 UTC Time : 08:31:25 PM
└───────────────────────────────────────────────────────┘
┌─ 👤 CHANNEL ──────────────────────────────────────────┐
│ 📺 Name : Ludwig
│ 👥 Followers : 6,850,000
│ 🔗 URL : https://www.youtube.com/channel/UCrPseYLGpNygVi34QpGNqpA
└───────────────────────────────────────────────────────┘
┌─ 📊 STATISTICS ───────────────────────────────────────┐
│ 👁️ Views : 786,850
│ 👍 Likes : 44,913
│ 💬 Comments : 2,900
└───────────────────────────────────────────────────────┘
┌─ 🎬 VIDEO DETAILS ────────────────────────────────────┐
│ ⏱️ Duration : 46m 45s
│ 📐 Resolution : 3840x2160
│ 📁 Category : Gaming
│ 🌍 Language : EN
│ 🔞 Age Limit : No
│ 📡 Live : No
└───────────────────────────────────────────────────────┘
┌─ 📝 DESCRIPTION ──────────────────────────────────────┐
│ Check out Invincible VS here: ... │
│ [Word-wrapped for readability] │
└───────────────────────────────────────────────────────┘
┌─ 🏷️ TAGS ────────────────────────────────────────────┐
│ ludwig, ludwigahgren, ahgren, gaming, chat, ... │
└───────────────────────────────────────────────────────┘
| Field | Format | Example |
|---|---|---|
| 📆 Date | YYYYMMDD | 20260406 |
| ⏰ Local Time | 12-hour format | 03:31:25 PM |
| 🌐 UTC Time | 12-hour format | 08:31:25 PM |
| Field | Description |
|---|---|
| 📺 Name | Channel name |
| 👥 Followers | Subscriber count (formatted with commas) |
| 🔗 URL | Channel URL |
| Field | Description |
|---|---|
| 👁️ Views | Total view count |
| 👍 Likes | Total like count |
| 💬 Comments | Total comment count |
| Field | Description |
|---|---|
| ⏱️ Duration | Formatted as Xh Ym Zs or Ym Zs |
| 📐 Resolution | Video resolution (e.g., 3840x2160 for 4K) |
| 📁 Category | Video category (e.g., Gaming, Music, Education) |
| 🌍 Language | Video language code (e.g., EN, ES, FR) |
| 🔞 Age Limit | Whether video has age restriction |
| 📡 Live | Whether video is a live stream |
Use the --json or -j flag to save metadata to a JSON file:
python youtube_metadata.py "https://youtu.be/AuCUZ9bSbao" --jsonOutput file: AuCUZ9bSbao_metadata.json
{
"title": "we got lost in China",
"video_id": "AuCUZ9bSbao",
"url": "https://www.youtube.com/watch?v=AuCUZ9bSbao",
"upload_date": "20260406",
"timestamp": 1743953485,
"publish_datetime": "2026-04-06T15:31:25+00:00",
"duration_seconds": 2805,
"duration_formatted": "46:45",
"resolution": "3840x2160",
"width": 3840,
"height": 2160,
"view_count": 786850,
"like_count": 44913,
"comment_count": 2900,
"description": "...",
"tags": ["ludwig", "ludwigahgren", ...],
"categories": ["Gaming"],
"channel_id": "UCrPseYLGpNygVi34QpGNqpA",
"channel_name": "Ludwig",
"channel_url": "https://www.youtube.com/channel/UCrPseYLGpNygVi34QpGNqpA",
"subscriber_count": 6850000,
"thumbnail": "...",
"thumbnails": [...],
"age_limit": 0,
"is_live": false,
"language": "en",
"availability": "public"
}The extract_video_id() function handles multiple URL formats:
- Direct Video ID: If input matches 11-character pattern, uses it directly
- YouTube URL: Parses URL and extracts video ID from query params or path
- Short URLs: Handles
youtu.bedomain separately - Special Paths: Handles
/shorts,/embed,/vpaths - Fallback: Regex search for 11-character alphanumeric string
Uses yt-dlp.YoutubeDL with extract_info(url, download=False):
- No video download occurs
- Only metadata is extracted
- All fields are safely accessed with
.get()to handle missing data
- Views, likes, comments formatted with
f"{n:,}" - Duration converted from seconds to
Xh Ym Zsformat
- Unix timestamp converted to UTC-aware datetime
- Local time calculated via
astimezone() - UTC time extracted directly
| Scenario | Behavior |
|---|---|
| Invalid URL | Prints error: "Could not extract video ID from URL" |
| Video not found | yt-dlp raises exception, caught and displayed |
| Network error | Exception caught, error message shown |
| Missing metadata field | Gracefully shows "N/A" |
| Package | Version | Purpose |
|---|---|---|
| yt-dlp | Latest | YouTube metadata extraction |
| urllib.parse | Built-in | URL parsing |
| datetime | Built-in | Time formatting |
| json | Built-in | JSON export |
| argparse | Built-in | Command-line argument parsing |
| re | Built-in | Video ID regex extraction |
$ python youtube_metadata.py "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
Retrieving metadata for: https://www.youtube.com/watch?v=dQw4w9WgXcQ
┌─────────────────────────────────────────────────────────┐
│ Rick Astley - Never Gonna Give You Up
└─────────────────────────────────────────────────────────┘
📹 Video ID : dQw4w9WgXcQ
🔗 URL : https://www.youtube.com/watch?v=dQw4w9WgXcQ
┌─ 📅 PUBLISH INFO ─────────────────────────────────────┐
│ 📆 Date : 20091025
│ ⏰ Local Time : 10:57:33 AM
│ 🌐 UTC Time : 06:57:33 AM
└───────────────────────────────────────────────────────┘$ python youtube_metadata.py "https://youtu.be/abc123" -j
Retrieving metadata for: https://youtu.be/abc123
[... formatted output ...]
Metadata saved to: abc123_metadata.json$ python youtube_metadata.py "https://www.youtube.com/shorts/xyz789"
Retrieving metadata for: https://www.youtube.com/shorts/xyz789
[... works automatically ...]Error: ModuleNotFoundError: No module named 'yt_dlp'
Solution:
pip install yt-dlpError: Video unavailable or This video is not available
Causes:
- Video is private/deleted
- Region restriction
- Age restriction (may require login)
Solution: Use a different video URL
Error: HTTP Error 429: Too Many Requests
Solution:
- Wait a few minutes and retry
- Use
--jsonflag to cache results locally
Error: Various extraction failures
Solution:
pip install --upgrade yt-dlpError: Connection timeout or refused
Solution:
- Verify internet connection
- Check firewall settings
- Try with different video URL
MIT License
Built with yt-dlp for comprehensive YouTube metadata extraction.