How to get YouTube Video Thumbnail using Python?

Introduction

YouTube video thumbnails are the small preview images that appear on video thumbnails, search results, and related videos. These images play a crucial role in attracting viewers and increasing engagement on YouTube.

In this tutorial, we will explore how to extract YouTube video thumbnails using Python. By the end of this tutorial, you will have a working Python code that can extract the thumbnail image of any YouTube video using its video ID and the YouTube API. Let’s get started!

Setting up Python Environment

We will be using the YouTube Data API v3 in this article to get YouTube video thumbnail images. So, please make sure that you have the YouTube’s API key.

Apart from that we will be using the google-api-python-client python library and its version is 2.70.0. You can install the library with the exact version by running the below command.

pip install google-api-python-client==2.70.0

Fetch YouTube Video Data using YouTube’s API

Now we will extract the thumbnail URL of this YouTube video. We will use the YouTube API key and ID of the YouTube video in the Python code below. The video ID can be easily found in the video’s URL.

from googleapiclient.discovery import build

yt_api_key = "Your YouTube Data API Key"

api_service_name = "youtube"
api_version = "v3"
youtube = build(api_service_name, api_version, developerKey = yt_api_key)

video_id = 'Vgb8b2D0vPY'

# fetch video details
request = youtube.videos().list(
    part = 'snippet',
    id = video_id
)

response = request.execute()

The code above can fetch information such as title, description, publish date, language, thumbnail URLs, etc.

Extract Youtube Video’s Thumbnail URL

Run the following python code to display the video’s thumbnail details.

response['items'][0]['snippet']['thumbnails']

Output:

{'default': {'url': 'https://i.ytimg.com/vi/Vgb8b2D0vPY/default.jpg',
  'width': 120,
  'height': 90},
 'medium': {'url': 'https://i.ytimg.com/vi/Vgb8b2D0vPY/mqdefault.jpg',
  'width': 320,
  'height': 180},
 'high': {'url': 'https://i.ytimg.com/vi/Vgb8b2D0vPY/hqdefault.jpg',
  'width': 480,
  'height': 360},
 'standard': {'url': 'https://i.ytimg.com/vi/Vgb8b2D0vPY/sddefault.jpg',
  'width': 640,
  'height': 480},
 'maxres': {'url': 'https://i.ytimg.com/vi/Vgb8b2D0vPY/maxresdefault.jpg',
  'width': 1280,
  'height': 720}}

In the output we can see that there are multiple URLs for thumbnail images of the video. Each thumbnail image has a different resolution. So, based on your requirement you can select the right URL to download the corresponding thumbnail image.

Download YouTube Video Thumbnail Image

Once you have extracted the thumbnail URL, you can use the requests python library to fetch the image data and use the PIL library to save the image to the disk. Use the Python code snippet below.

# get thumbnail image URL for medium resolution
img_url = response['items'][0]['snippet']['thumbnails']['medium']['url']

import requests
from PIL import Image

# get thumbnail image data
response = requests.get(img_url)

# save the thumbnail image to disk
with open("image.jpg", "wb") as f:
  f.write(response.content)

Finally, we have successfully saved a YouTube video’s thumbnail image using Python. The same code can be used to save hundreds of thumbnail images at once.

End Notes

I hope now you can easily extract any YouTube video’s thumbnail images that too in different resolutions. Similarly, you can pull out other information as well, such as view count of the video, how many likes it got, how many comments the video has and much more. Feel free to connect with me if you need any help and do check out my other articles.

Leave a Reply

Your email address will not be published. Required fields are marked *