Skip to content

Latest commit

 

History

4 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

YouTube Video Metadata Retriever

A Python script that retrieves comprehensive metadata from YouTube videos using yt-dlp.

Quick Start

# 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

Table of Contents

  1. Key Features
  2. Installation
  3. Usage
  4. Supported URL Formats
  5. Command Line Options
  6. Output Format
  7. Metadata Fields
  8. JSON Output
  9. How It Works
  10. Error Handling
  11. Dependencies
  12. Examples
  13. Troubleshooting

Key Features

  • 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

Installation

Prerequisites

Requirement Version Description
Python 3.7+ Python runtime
pip Latest Python package manager

Install yt-dlp

pip install yt-dlp

That's it! No YouTube API key needed.

Usage

Basic Usage

# 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" --json

Supported URL Formats

The 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

Command Line Options

Option Short Description
url (required) YouTube video URL or video ID
--json -j Save metadata to JSON file
--help -h Show help message

Output Format

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, ...      │
└───────────────────────────────────────────────────────┘

Metadata Fields

PUBLISH INFO

Field Format Example
📆 Date YYYYMMDD 20260406
⏰ Local Time 12-hour format 03:31:25 PM
🌐 UTC Time 12-hour format 08:31:25 PM

CHANNEL

Field Description
📺 Name Channel name
👥 Followers Subscriber count (formatted with commas)
🔗 URL Channel URL

STATISTICS

Field Description
👁️ Views Total view count
👍 Likes Total like count
💬 Comments Total comment count

VIDEO DETAILS

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

JSON Output

Use the --json or -j flag to save metadata to a JSON file:

python youtube_metadata.py "https://youtu.be/AuCUZ9bSbao" --json

Output file: AuCUZ9bSbao_metadata.json

JSON Structure

{
  "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"
}

How It Works

URL Parsing

The extract_video_id() function handles multiple URL formats:

  1. Direct Video ID: If input matches 11-character pattern, uses it directly
  2. YouTube URL: Parses URL and extracts video ID from query params or path
  3. Short URLs: Handles youtu.be domain separately
  4. Special Paths: Handles /shorts, /embed, /v paths
  5. Fallback: Regex search for 11-character alphanumeric string

Metadata Extraction

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

Number Formatting

  • Views, likes, comments formatted with f"{n:,}"
  • Duration converted from seconds to Xh Ym Zs format

Time Handling

  • Unix timestamp converted to UTC-aware datetime
  • Local time calculated via astimezone()
  • UTC time extracted directly

Error Handling

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"

Dependencies

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

Examples

Example 1: Quick Video Check

$ 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
└───────────────────────────────────────────────────────┘

Example 2: Save Metadata for Analysis

$ python youtube_metadata.py "https://youtu.be/abc123" -j

Retrieving metadata for: https://youtu.be/abc123
[... formatted output ...]
Metadata saved to: abc123_metadata.json

Example 3: Check YouTube Shorts

$ python youtube_metadata.py "https://www.youtube.com/shorts/xyz789"

Retrieving metadata for: https://www.youtube.com/shorts/xyz789
[... works automatically ...]

Troubleshooting

yt-dlp Not Installed

Error: ModuleNotFoundError: No module named 'yt_dlp'

Solution:

pip install yt-dlp

Video Unavailable

Error: 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

Rate Limiting

Error: HTTP Error 429: Too Many Requests

Solution:

  • Wait a few minutes and retry
  • Use --json flag to cache results locally

Old yt-dlp Version

Error: Various extraction failures

Solution:

pip install --upgrade yt-dlp

Network Issues

Error: Connection timeout or refused

Solution:

  • Verify internet connection
  • Check firewall settings
  • Try with different video URL

License

MIT License

Author

Built with yt-dlp for comprehensive YouTube metadata extraction.

About

A Python script that retrieves comprehensive metadata from YouTube videos using yt-dlp.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages