This Python utility provides functionality to interact with YouTube videos using the pytube
library. It allows you to retrieve video information, list available streams, and download videos in the highest resolution available.
To use this utility, you need to have Python installed along with the pytube
library.
- Install Python: Python Downloads
- Install
pytube
:pip install pytube
This function retrieves various information about a YouTube video, including title, description, views, rating, length, author, publish date, keywords, and thumbnail URL.
from pytube import YouTube
def get_video_info(video_url):
video = YouTube(video_url)
video_info = {
"title": video.title,
"description": video.description,
"views": video.views,
"rating": video.rating,
"length": video.length,
"author": video.author,
"publish_date": video.publish_date,
"keywords": video.keywords,
"thumbnail_url": video.thumbnail_url,
}
return video_info
This function lists all available progressive streams for a YouTube video, including the itag, resolution, mime type, and file size.
from pytube import YouTube
def list_available_streams(video_url):
video = YouTube(video_url)
streams = video.streams.filter(progressive=True)
available_streams = [
{
"itag": stream.itag,
"resolution": stream.resolution,
"mime_type": stream.mime_type,
"filesize": stream.filesize,
} for stream in streams
]
return available_streams
This function downloads the YouTube video in the highest resolution available to a specified output path.
from pytube import YouTube
def download_video(video_url, output_path='.'):
video = YouTube(video_url)
stream = video.streams.get_highest_resolution()
stream.download(output_path)
return f"Video downloaded to {output_path}"
- Retrieve detailed information about a YouTube video.
- List all available progressive streams for a video.
- Download a YouTube video in the highest resolution available.
pytube
: A lightweight, dependency-free Python library (and command-line utility) for downloading YouTube videos.
Install dependencies using pip:
pip install pytube
Here's an example of how to use the utility:
if __name__ == '__main__':
video_url = "https://www.youtube.com/watch?v=dQw4w9WgXcQ"
# Get video info
video_info = get_video_info(video_url)
print("Video Info:", video_info)
# List available streams
streams = list_available_streams(video_url)
print("Available Streams:", streams)
# Download video
download_message = download_video(video_url, output_path='.')
print(download_message)
This project is licensed under the MIT License - see the LICENSE file for details.