Skip to content

Added YouTube Thumbnail Downloader #152

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions Y/youtube-thumbnail-downloader/Readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
## YouTube thumbnail extractor in Python
First you need to Install the pytube library. You can install pytube using following:<br>
pip install pytube
<br>

## Note
Make sure to replace "https://www.youtube.com/watch?v=your_video_id" with the actual YouTube video URL you want to extract the thumbnail from and specify the desired output directory.
<br>
Run the script. It will extract the thumbnail URL and download the thumbnail image to the specified directory.
<br>
Thank you
43 changes: 43 additions & 0 deletions Y/youtube-thumbnail-downloader/yt_thumbnail.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import os
from pytube import YouTube
from pytube.exceptions import VideoUnavailable

# Function to extract thumbnails
def extract_thumbnail(video_url, output_directory):
try:
# Validate video URL
if not video_url.startswith("https://www.youtube.com/watch?v="):
raise ValueError("Invalid YouTube video URL")

# Create a YouTube object
yt = YouTube(video_url)

# Get video's thumbnail URL
thumbnail_url = yt.thumbnail_url

# Download thumbnail image
thumbnail_path = os.path.join(output_directory, "thumbnail.jpg")
yt.streams.filter(file_extension='jpg').first().download(output_path=output_directory, filename="thumbnail")

return thumbnail_path
except VideoUnavailable as e:
return f"Video is unavailable: {str(e)}"
except ValueError as e:
return f"Invalid video URL: {str(e)}"
except Exception as e:
return f"An error occurred: {str(e)}"

if __name__ == "__main__":
video_url = "https://www.youtube.com/watch?v=your_video_id" # Replace with the YouTube video URL
output_directory = "thumbnails" # Replace with your desired output directory

# Create the output directory if it doesn't exist
if not os.path.exists(output_directory):
os.makedirs(output_directory)

thumbnail_url = extract_thumbnail(video_url, output_directory)
if "Error" in thumbnail_url:
print(thumbnail_url)
else:
print(f"Thumbnail URL: {thumbnail_url}")
print(f"Thumbnail image downloaded to {output_directory}/thumbnail.jpg")