In this article we will see how we can check each item of playlist is HD or not in pafy. Pafy is a python library to download YouTube content and retrieve metadata. Pafy object is the object which contains all the information about the given video. A playlist in YouTube is a list, or group, of videos that plays in order, one video after the other. On YouTube, HD indicates that a video has between 720 and 1080 lines of vertical resolution (shown as 720p or 1080p in the quality settings of the YouTube player) compared to 360 or 480, which are typical for SD. We can get the playlist from youtube in pafy with the help of get_playlist method, below is the command given to do this
pafy.get_playlist(url)
The playlist url should exist on youtube as it get the information of those videos which are present on the youtube. YouTube is an American online video-sharing platform.
Steps for implementation 1. Import the pafy module 2. Get the playlist with the help of URL of playlist 3. Return play list work as dictionary so use ‘items’ as key with the return playlist 4. Store the result in variable and from the items select the single item 5. With the single item use ‘playlist_meta’ key with it to get the meta data 6. With the meta data use ‘is_hd’ key this will return True if item is available in HD
Below is the implementation
Python3
# importing pafy import pafy # url of playlist url = "https: / / www.youtube.com / playlist? list = PLqM7alHXFySGqCvcwfqqMrteqWukz9ZoE" # getting playlist playlist = pafy.get_playlist(url) # getting playlist items items = playlist["items"] # selecting single item item = items[ 1 ] # getting pafy object i_pafy = item[ 'pafy' ] # getting meta data metadata = item[ 'playlist_meta' ] # checking if item is HD value = metadata[ 'is_hd' ] # printing value print (value) |
Output :
True
Another example
Python3
# importing pafy import pafy # url of playlist url = "https: / / www.youtube.com / playlist? list = PLqM7alHXFySE71A2bQdYp37vYr0aReknt" # getting playlist playlist = pafy.get_playlist(url) # getting playlist items items = playlist["items"] # selecting single item item = items[ 2 ] # getting meta data metadata = item[ 'playlist_meta' ] # checking if item is HD value = metadata[ 'is_hd' ] # printing value print (value) |
Output :
True