Get Youtube Video Metadata with Python (yt-dlp)
While simply grabbing a thumbnail only requires a basic URL trick, accessing a video's metadata-like its title, view count, and description-requires a tool that can "scrape" or "query" the actual page data.
In the Python world, the gold standard for this is yt-dlp. It is a faster, more frequently updated successor to the original youtube-dl. Unlike the official Google API, yt-dlp doesn't require an API key or complex project setup, making it perfect for quick scripts.
π οΈ The Power of Metadata Extractionβ
When you request info from a YouTube link, yt-dlp returns a massive Python dictionary containing everything from the upload date to the exact tags used by the creator.
π» The Implementationβ
I have placed the complete script in the block below. This code is designed to be "silent," meaning it extracts the data without actually downloading the heavy video file.
# π Python Script: YouTube Metadata Extractor (yt-dlp)
This script uses `yt-dlp` to fetch a "manifest" of the video and prints out the specific fields you need.
### 1. Requirements
```bash
pip install yt-dlp
2. The Codeβ
import yt_dlp
def get_youtube_metadata(url):
# 'quiet': True prevents the console from being flooded with logs
# 'skip_download': True ensures we only get the text data, not the video
ydl_opts = {
'quiet': True,
'skip_download': True,
}
try:
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
# Extract info returns a dictionary
info = ydl.extract_info(url, download=False)
# Pulling specific fields
metadata = {
"Title": info.get('title'),
"Views": info.get('view_count'),
"Description": info.get('description'),
"Author": info.get('uploader'),
"Duration": info.get('duration'), # In seconds
"Upload Date": info.get('upload_date')
}
return metadata
except Exception as e:
print(f"β Error fetching metadata: {e}")
return None
# --- Execution ---
video_url = "[https://www.youtube.com/watch?v=dQw4w9WgXcQ](https://www.youtube.com/watch?v=dQw4w9WgXcQ)"
data = get_youtube_metadata(video_url)
if data:
print(f"π₯ TITLE: {data['Title']}")
print(f"ποΈ VIEWS: {data['Views']:,}") # Adds commas for readability
print("-" * 30)
# Print first 200 characters of the description
print(f"π DESCRIPTION: {data['Description'][:200]}...")
π Common Metadata Fieldsβ
When you use ydl.extract_info(), you get access to dozens of hidden fields. Here are the most useful ones:
| Key | Description | Example Output |
|---|---|---|
title | The full video title. | "Never Gonna Give You Up" |
view_count | Total lifetime views. | 1450230400 |
like_count | Number of likes. | 16000000 |
upload_date | Date in YYYYMMDD format. | "20091025" |
tags | List of keywords used. | ["Rick Astley", "80s"] |
categories | The video category. | ["Music"] |
π Sources & Technical Refsβ
- [1.1] yt-dlp GitHub: Official Documentation - The full list of available metadata keys and embedding options.
- [2.1] Real Python: Python and YouTube: A Match Made in Heaven - Comparison of different libraries for video data processing.
- [3.1] PyPI: yt-dlp Project Page - Installation and versioning updates.
π Pro Tip: Handling "Age Restricted" Videosβ
If a video is age-restricted, yt-dlp might fail unless you provide your browser cookies. You can do this easily in your script by adding 'cookiefile': 'cookies.txt' to your ydl_opts (after exporting them from your browser).
