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
# importing pafyimport pafy # url of video # instant created video = pafy.new(url) # getting number of likescount = video.viewcount # showing likesprint("View Count : " + str(count)) |
Output :
View Count : 287205
Example 2:
Program to get the thumb image of a video
# importing pafyimport pafy # url of video # instant created video = pafy.new(url) # getting thumb imagecount = video.thumb # showing likesprint("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
# importing pafyimport pafy # url of video # instant created video = pafy.new(url) # getting titlevalue = video.title # showing likesprint("Title : " + str(value)) |
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. And to begin with your Machine Learning Journey, join the Machine Learning – Basic Level Course


