Open In App

How to extract image information from YouTube Playlist using Python?

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 –



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:



In YouTube Video, there are five different thumbnails that are:

Approach:




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

Below is the Implementation:




# 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:


Article Tags :