This script allows users to download YouTube videos in the highest resolution available using the pytube library.
- Make sure you have Python installed on your system.
- Install the
pytubelibrary:pip install pytube
- Run the script:
python youtube_downloader.py
- Enter the YouTube video URL when prompted.
Enter the YouTube video URL: https://www.youtube.com/watch?v=xyz123
Downloading: Example Video Title
Resolution: 1080p
Filesize: 20.34 MB
Download completed successfully!- Python 3.x
pytubelibrary (can be installed usingpip install pytube)
This Python script allows users to download videos from YouTube using the pytube library. Below is an explanation of how each part of the code works.
from pytube import YouTubeThe pytube library is used to interact with YouTube and download videos. Here, we are importing the YouTube class from the pytube module.
def download_youtube_video(url):
try:
# Create a YouTube object
yt = YouTube(url)This function download_youtube_video accepts a url as input. The YouTube object is created by passing the YouTube video URL to the YouTube class, which helps retrieve the video metadata and streams.
# Get the highest resolution stream available
video_stream = yt.streams.get_highest_resolution()Using the streams attribute of the YouTube object, the method get_highest_resolution() retrieves the highest quality video stream available for download.
print(f"Downloading: {yt.title}")
print(f"Resolution: {video_stream.resolution}")
print(f"Filesize: {video_stream.filesize / 1024 / 1024:.2f} MB")Here, we display some basic information about the video:
yt.title: The title of the YouTube video.video_stream.resolution: The resolution of the video (e.g., 720p, 1080p).video_stream.filesize: The size of the video file in bytes, converted to megabytes (MB) using division by 1024 twice.
video_stream.download()The download() method is used to download the video to the current working directory.
except Exception as e:
print(f"An error occurred: {e}")If any error occurs during the download process (e.g., invalid URL or connection issues), the error is caught and printed to the console.
if __name__ == "__main__":
video_url = input("Enter the YouTube video URL: ")
download_youtube_video(video_url)- The
__name__ == "__main__"block ensures that the script runs only when executed directly, not when imported as a module. - The user is prompted to input a YouTube video URL, and the
download_youtube_videofunction is called with the provided URL.
This script:
- Accepts a YouTube video URL from the user.
- Retrieves the highest quality video stream available.
- Downloads the video to the user's device.