Introduction to Pafy Module in Python

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. Retreive 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. Retreive 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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

View Count : 287205

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

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

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

Example 3:
Program to get the title of a video

filter_none

edit
close

play_arrow

link
brightness_4
code

# 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))

chevron_right


Output :

Title : DSA Self Paced Course | GeeksforGeeks

Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.

To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.