Prerequisite: YouTube API
Google provides a large set of APIs for the developer to choose from. Each and every service provided by Google has an associated API. Being one of them, YouTube Data API is very simple to use provides features like –
- Search for videos
- Handle videos like retrieve information about a video, insert a video, delete a video, etc.
- Handle Subscriptions like lists all the subscriptions, insert or delete a subscription.
- Retrieve information about comments like replies to a specific comment identified by a parentid etc.
In this article, we will learn how to extract Image Information of Individual video from the YouTube Playlist Using Python
Image Information that we will fetch:
- Image URL
- Image Width & Height
- Different Size of Images
In YouTube Video, there are five different thumbnails that are:
- default
- high
- maxres
- medium
- standard
Approach:
- We will use the build, list, execute method, it will give Playlist Details
- Inside list method, pass contentDetails in part property and in playlistId property pass PlaylistID or PlaylistURL
- Pass 50 value in maxResults and in pageToken initially pass None Value
Python3
nextPageToken = None # creating youtube resource object youtube = build( 'youtube' , 'v3' ,developerKey = 'Enter API Key' ) while True : # retrieve youtube video results pl_request = youtube.playlistItems(). list ( part = 'snippet' , playlistId = playlistId.get(), maxResults = 50 , pageToken = nextPageToken ) pl_response = pl_request.execute() nextPageToken = pl_response.get( 'nextPageToken' ) if not nextPageToken: break |
- Iterate through all response and fetch Image Information
Below is the Implementation:
Python3
# import module from googleapiclient.discovery import build def playlist_video_links(playlistId): nextPageToken = None # creating youtube resource object youtube = build( 'youtube' , 'v3' ,developerKey = 'Enter API Key' ) while True : # retrieve youtube video results pl_request = youtube.playlistItems(). list ( part = 'snippet' , playlistId = playlistId, maxResults = 50 , pageToken = nextPageToken ) pl_response = pl_request.execute() # Iterate through all response and fetch Image Information for item in pl_response[ 'items' ]: thumbnails = item[ 'snippet' ][ 'thumbnails' ] if 'default' in thumbnails: default = thumbnails[ 'default' ] print (default) if 'high' in thumbnails: high = thumbnails[ 'high' ] print (high) if 'maxres' in thumbnails: maxres = thumbnails[ 'maxres' ] print (maxres) if 'medium' in thumbnails: medium = thumbnails[ 'medium' ] print (medium) if 'standard' in thumbnails: standard = thumbnails[ 'standard' ] print (standard) print ( "\n" ) nextPageToken = pl_response.get( 'nextPageToken' ) if not nextPageToken: break playlist_video_links( 'PLsyeobzWxl7r2ukVgTqIQcl-1T0C2mzau' ) |
Output: