|
|
|
|
|
|
|
from pytube import YouTube |
|
from tqdm import tqdm |
|
|
|
def progress_function(stream, chunk, bytes_remaining): |
|
global pbar |
|
|
|
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: |
|
|
|
yt = YouTube(url, on_progress_callback=progress_function) |
|
|
|
|
|
stream = yt.streams.get_highest_resolution() |
|
global pbar |
|
pbar = tqdm(total=stream.filesize, unit='B', unit_scale=True, desc='Downloading') |
|
|
|
|
|
|
|
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}") |
|
|
|
|
|
|
|
|