Download Youtube title, views, and other metadata with Python script

Script Your Way Around YouTube. Python has become a go-to for various programming tasks, including web scraping and automation.

Let's start with extracting video metadata such as the title, number of views, and duration. We can use both pytube and youtube_dl for this.

Extracting Video Metadata Pytube #

Use pip to install the libraries:

pip install pytube

Here's an example with pytube:

from pytube import YouTube

url = "YOUR_YOUTUBE_VIDEO_URL"
yt = YouTube(url)

print("Title: ", yt.title)
print("Number of views: ", yt.views)
print("Length of video: ", yt.length, "seconds")
print("Description: ", yt.description)

Extracting Video Metadata with youtube_dl #

And here's how you can do it with youtube_dl:

pip install youtube-dl
from youtube_dl import YoutubeDL

ydl = YoutubeDL()

url = "YOUR_YOUTUBE_VIDEO_URL"
info_dict = ydl.extract_info(url, download=False)

print("Title: ", info_dict['title'])
print("Number of views: ", info_dict['view_count'])
print("Length of video: ", info_dict['duration'], "seconds")
print("Description: ", info_dict['description'])

Downloading Videos #

If you're interested in diving deeper into the possibilities of Python and YouTube, don't miss a detailed guide on downloading YouTube videos with Python.

Both pytube and youtube_dl provide the feature of downloading videos from Youtube.

Using pytube:

from pytube import YouTube

url = "YOUR_YOUTUBE_VIDEO_URL"
yt = YouTube(url)

# download the highest quality video
yt.streams.get_highest_resolution().download()

Using youtube_dl:

from youtube_dl import YoutubeDL

url = "YOUR_YOUTUBE_VIDEO_URL"
ydl = YoutubeDL()
ydl.download([url])

Python's capabilities for interacting with YouTube are extensive and offer many exciting possibilities. By using the examples above, you can begin your exploration and discover even more ways to automate and interact with this platform. Happy scripting!

Published