Skip to main content

Get Youtube Video Metadata with Python (yt-dlp)

Β· 5 min read
Serhii Hrekov
software engineer, creator, artist, programmer, projects founder

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:

KeyDescriptionExample Output
titleThe full video title."Never Gonna Give You Up"
view_countTotal lifetime views.1450230400
like_countNumber of likes.16000000
upload_dateDate in YYYYMMDD format."20091025"
tagsList of keywords used.["Rick Astley", "80s"]
categoriesThe video category.["Music"]

πŸ“š Sources & Technical Refs​


πŸ“‹ 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).