Last active
January 30, 2022 18:40
-
-
Save Mukhopadhyay/fae36e56f0d7503bd377fb0a2ff0f90e to your computer and use it in GitHub Desktop.
Method for downloading YouTube video thumbnails
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
from typing import Optional | |
def download_youtube_thumbnail(video_id: str, filename: Optional[str] = 'image.jpg') -> None: | |
""" | |
Method for downloading youtube thumbnail (if explicitly set by the | |
publisher) in maximum resolution. If filename is not passed then the | |
image will be saved as 'image.jpg'. | |
Args: | |
video_id: str: Unique ID of the video for which the thumbnail is to be downloaded. | |
e.g., https://www.youtube.com/watch?v=12345678910, in this link | |
'12345678910' is the video_id (length 11) | |
filename: str: File of the image to be saved. (Default is 'image.jpg') | |
Returns: | |
None | |
""" | |
url = 'https://img.youtube.com/vi/{}/maxresdefault.jpg'.format(video_id) | |
with open(filename, 'wb') as file: | |
file.write(requests.get(url).content) | |
print('Image saved as {}!'.format(filename)) | |
return |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment