Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
YouTube Media/Audio Download using Python - pafy
In this article, we will learn how to extract YouTube video details and download media using the pafy module in Python. The pafy library provides a simple interface to access YouTube video metadata and download content in various formats.
Installation
Install the pafy module using pip:
pip install pafy
The pafy module also requires youtube-dl as a backend dependency:
pip install youtube-dl
Verify the installation by importing the module:
import pafy
print("Pafy module imported successfully!")
Pafy module imported successfully!
Extracting YouTube Video Details
You can extract comprehensive metadata about any YouTube video using the pafy.new() method. Here's how to get video information:
import pafy
# YouTube video URL
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Create pafy object
video = pafy.new(url)
# Extract video details
print(f"Title: {video.title}")
print(f"Author: {video.author}")
print(f"Duration: {video.duration}")
print(f"View Count: {video.viewcount}")
print(f"Rating: {video.rating}")
print(f"Likes: {video.likes}")
print(f"Published: {video.published}")
Title: Rick Astley - Never Gonna Give You Up (Official Video) Author: Rick Astley Duration: 00:03:33 View Count: 1425000000 Rating: 4.9 Likes: 15000000 Published: 2009-10-25 00:00:00
Downloading Video in Best Quality
Use the getbest() method to download the highest quality video available:
import pafy
# YouTube video URL
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Create pafy object
video = pafy.new(url)
# Get best quality video stream
best_video = video.getbest()
print(f"Best quality: {best_video}")
print(f"File size: {best_video.get_filesize()} bytes")
# Download the video
best_video.download()
print("Video downloaded successfully!")
You can also specify a preferred format by passing the preftype parameter:
# Download best quality MP4 video best_mp4 = video.getbest(preftype="mp4") best_mp4.download() # Download best quality WebM video best_webm = video.getbest(preftype="webm") best_webm.download()
Downloading Audio in Best Quality
To download only the audio track, use the getbestaudio() method:
import pafy
# YouTube video URL
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Create pafy object
video = pafy.new(url)
# Get best quality audio stream
best_audio = video.getbestaudio()
print(f"Audio format: {best_audio}")
print(f"Audio bitrate: {best_audio.bitrate}")
# Download the audio
audio_file = best_audio.download()
print(f"Audio downloaded: {audio_file}")
Available Stream Formats
You can view all available video and audio streams before downloading:
import pafy
url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
video = pafy.new(url)
print("Video streams:")
for stream in video.videostreams:
print(f" {stream}")
print("\nAudio streams:")
for stream in video.audiostreams:
print(f" {stream}")
Video streams: normal:mp4@1280x720 normal:webm@1280x720 normal:mp4@854x480 Audio streams: audio:m4a@128k audio:webm@160k audio:webm@70k
Conclusion
The pafy module provides an easy way to extract YouTube video metadata and download content programmatically. Use getbest() for video downloads and getbestaudio() for audio−only downloads with optimal quality selection.
