File size: 1,031 Bytes
4108c13
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# 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}")