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
import pafy
video = pafy.new(url)
count = video.viewcount
print ( "View Count : " + str (count))
|
Output :
View Count : 287205
Example 2:
Program to get the thumb image of a video
Python3
import pafy
video = pafy.new(url)
count = video.thumb
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
import pafy
video = pafy.new(url)
value = video.title
print ( "Title : " + str (value))
|
Output :
Title : DSA Self Paced Course | GeeksforGeeks