Spaces:
Runtime error
Runtime error
# import subprocess | |
# subprocess.run("pip install pytube", shell=True) | |
from pytube import YouTube | |
from tqdm import tqdm | |
def progress_function(stream, chunk, bytes_remaining): | |
global pbar | |
# Calculate the progress in terms of bytes downloaded | |
total_size = stream.filesize | |
bytes_downloaded = total_size - bytes_remaining | |
percentage_of_completion = bytes_downloaded / total_size * 100 | |
pbar.update(len(chunk)) | |
def download_youtube_video(url, output_path): | |
try: | |
# Create a YouTube object | |
yt = YouTube(url, on_progress_callback=progress_function) | |
# Get the highest resolution stream available | |
stream = yt.streams.get_highest_resolution() | |
global pbar | |
pbar = tqdm(total=stream.filesize, unit='B', unit_scale=True, desc='Downloading') | |
# Download the video | |
stream.download(output_path=output_path) | |
print(f"Video downloaded successfully and saved to {output_path}") | |
except Exception as e: | |
print(f"An error occurred: {e}") | |