Skip to content
Related Articles

Related Articles

Introduction to Pafy Module in Python

View Discussion
Improve Article
Save Article
Like Article
  • Last Updated : 06 Aug, 2021

In this article we will learn about the pafy module. Pafy is a Python library to download YouTube content and retrieve metadata.
 

Below are the list of features Pafy offers
1. Retrieve metadata such as viewcount, duration, rating, author, thumbnail, keywords 
2. Download video or audio at requested resolution / bitrate / format / filesize 
2. Command line tool (ytdl) for downloading directly from the command line 
3. Retrieve the URL to stream the video in a player such as vlc or mplayer 
4. Works with age-restricted videos and non-embeddable videos 
5. Small, standalone, single importable module file 
6. Select highest quality stream for download or streaming 
7. Download video only (no audio) in m4v or webm format 
8. Download audio only (no video) in ogg or m4a format 
9. Retrieve playlists and playlist metadata 
10. Works with Python 2.6+ and 3.3+ 

Installation 
In order to install pafy we use the command given below 

pip install pafy

Note : Pafy is optionally depends on youtube-dl so therefore for more stable usage it is recommended to install youtube-dl before installing pafy. Below is the command to install youtube-dl 

pip install youtube_dl

Example 1: 
Program to get the number of views on a video 

Python3




# importing pafy
import pafy
   
# url of video
url = "https://www.youtube.com / watch?v = mmjDZGSr7EA"
   
# instant created
video = pafy.new(url)
 
# getting number of likes
count = video.viewcount
 
# showing likes
print("View Count : " + str(count))


Output : 

View Count : 287205

Example 2: 
Program to get the thumb image of a video 

Python3




# importing pafy
import pafy
   
# url of video
url = "https://www.youtube.com / watch?v = mmjDZGSr7EA"
   
# instant created
video = pafy.new(url)
 
# getting thumb image
count = video.thumb
 
# showing likes
print("Thumb Image : " + str(count))


Output : 

Thumb Image : http://i.ytimg.com/vi/vG2PNdI8axo/default.jpg

Example 3: 
Program to get the title of a video 

Python3




# importing pafy
import pafy
   
# url of video
url = "https://www.youtube.com / watch?v = vG2PNdI8axo"
   
# instant created
video = pafy.new(url)
 
# getting title
value = video.title
 
# showing likes
print("Title : " + str(value))


Output : 

Title : DSA Self Paced Course | GeeksforGeeks

 


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!