Pafy Module in Python
Pafy Module in Python
For a better reading experience, check the published document
What is pafy?
It is a module that can download videos from Youtube to our local device. It can also get all information about the video such as the number of likes, author’s info, description, and a lot more.
Installing the module
To install this module, run these two commands on your shell
pip install pafy youtube.dl youtube_dl pip install youtube-dl==2020.12.2
Importing the module
We use the (*) to import all classes in the module
from pafy import *
Creating “puff“, the download managers
We are going to create an object from the class pafy and give it the name “puff”. He will be in charge of dealing with videos.
We are going to use Taylor Swift’s song “Afterglow“ to test puff’s abilities.
# first, we get the URL of the video afterglow_url = "https://www.youtube.com/watch?v=8HxbqAsppwU" # then, we create puff and pudd using the (new) method puff = new(afterglow_url)
What did you get, puff ?
We can see the info that puff gathered by using his attributes
Quick overview
we can take a peek at the video info
print(puff)
This is what will show up:
Video details
we can select what we want from the attributes that come with puff
puff.thumb # return the url of the thumbnail (video’s display image) puff.bigthumb # url of the thumbnail zoomed puff.bigthumbhd # url of the thumbnail in HD format puff.likes # number of likes on that video puff.dislikes # number of dislikes on that video puff.description # description of that video puff.title # title of that video puff.author # channel name that uploaded the video puff.videoid # ID of that video on Youtube puff.duration # duration of that video puff.rating # rating of that video puff.viewcount # number of views on that video puff.length # length of the video in seconds puff.category # category of that video puff.username # username of the account who uploads that video puff.keyword # keywords of that video puff.published # date and time of publishing that video puff.mix # mix playlist provided by youtube for this video puff.viewcount # viewcount of the video
This is what will show up:
Note:
Tell puff to pick a proper resolution
We can check the available resolutions for the video and pick one of them to download the video
Which one?
# we pick only one of these based on what we want res = puff.getbest() # return the best available resolution of the video # normal:mp4@1280x720 res = puff.getbest(preftype="webm") # return the 360 resolution of the video # normal:webm@640x360 res = puff.getbest(preftype="webm", ftypestrick=False) # return the best resolution despise the value of (preftype) res = puff.getbest(preftype="webm", ftypestrick=True) # return the resolution of (preftype) res = puff.getbestvideo() # this is only for video that has no audio # video:mp4@1280x720 res = puff.getbestaudio() # this is only for audio that has no video # audio:mp4@1280x720
What did you choose?
After selecting a resolution, puff can tell us some info about the video before we download it
vid = puff.getbest() # to get the best stream(resolution) of the video vid.title # return the title of the video vid.extension # return the extension of the video vid.notes # return the quality of the video vid.quality # return the resolution of the video vid.get_filesize # return the size of the video in bytes
Streams of the video
Now we need to tell puff what is the type of file that we want
Which one ?
# we pick only one of these based on what we want puff.streams # return streams that handles video and audio puff.videostreams # return streams that handles only a video without audio puff.audiostreams # return streams that handles only an audio without video puff.oggsstreams # return all the ogg audio streams puff.m4astreams # return all the m4a streams puff.webmstreams # return all the webm streams puff.allstreams # return all the streams # we can do this for better view: for stream in puff.allstreams: print(stream) # to pick the 4th stream for example: streams = puff.allstreams selected_stream = streams[3]
What did you choose?
After selecting a stream, puff can tell us some info about the video before we download it
stream = puff.streams() stream.url # the direct access URL of the stream (mplayer, vlc, wget, curl) stream.url_https # the direct access HTTPS URL of the stream stream.bitrate # bitrate of the stream (only for audio) stream.dimensions # (x, y) resolution of a video stream stream.extension # format of the stream ('ogg', 'm4a', 'mp4', 'flv', 'webm', '3gp') stream.mediatype # ('normal', 'audio', 'video') stream.quality # resolution or the bitrate, (video or audio respectively) stream.resolution # resolution of a video, like:“820x640”, set to “0x0” for audio streams stream.rawbitrate # bitrate of an audio stream, set to None for video streams stream.threed # True if the stream is a 3D video stream.title # title of the video, same as puff.title stream.notes # any additional notes regarding the stream
Finally !!! the downloading part
Download it as a video or audio?
We can download the video by either choosing its resolution or stream. After selecting one of them, we can tell puff to start downloading
# download by resoluton res = puff.getbest() # selecting a video resolution res.download() # start downloading # download by stream as a video vid = puff.videostreams[0] # selecting a video stream vid.download() # start downloading # download by stream as an audio aud = puff.audiostreams[0] # selecting a video stream aud.download() # start downloading
Where did my file go ?
After the downloading process is completed (you can see it from the terminal), the file will be saved on our device, but where?
res = puff.getbest() # default location res.download() # save the video in the same directory of this python file # custom location res.download("D:\python") # save the video in a given path to a directory
Comments
Post a Comment