Suparnpreet commited on
Commit
4108c13
1 Parent(s): 83c1062

Update ytdownloader.py

Browse files
Files changed (1) hide show
  1. ytdownloader.py +33 -21
ytdownloader.py CHANGED
@@ -1,21 +1,33 @@
1
- # import subprocess
2
- # subprocess.run("pip install pytube", shell=True)
3
-
4
- from pytube import YouTube
5
-
6
- def download_youtube_video(url, output_path):
7
- try:
8
- # Create a YouTube object
9
- yt = YouTube(url)
10
-
11
- # Get the highest resolution stream available
12
- stream = yt.streams.get_highest_resolution()
13
-
14
- # Download the video
15
- stream.download(output_path=output_path)
16
-
17
- print(f"Video downloaded successfully and saved to {output_path}")
18
- except Exception as e:
19
- print(f"An error occurred: {e}")
20
-
21
-
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # import subprocess
2
+ # subprocess.run("pip install pytube", shell=True)
3
+
4
+ from pytube import YouTube
5
+ from tqdm import tqdm
6
+
7
+ def progress_function(stream, chunk, bytes_remaining):
8
+ global pbar
9
+ # Calculate the progress in terms of bytes downloaded
10
+ total_size = stream.filesize
11
+ bytes_downloaded = total_size - bytes_remaining
12
+ percentage_of_completion = bytes_downloaded / total_size * 100
13
+ pbar.update(len(chunk))
14
+ def download_youtube_video(url, output_path):
15
+ try:
16
+ # Create a YouTube object
17
+ yt = YouTube(url, on_progress_callback=progress_function)
18
+
19
+ # Get the highest resolution stream available
20
+ stream = yt.streams.get_highest_resolution()
21
+ global pbar
22
+ pbar = tqdm(total=stream.filesize, unit='B', unit_scale=True, desc='Downloading')
23
+
24
+
25
+ # Download the video
26
+ stream.download(output_path=output_path)
27
+
28
+ print(f"Video downloaded successfully and saved to {output_path}")
29
+ except Exception as e:
30
+ print(f"An error occurred: {e}")
31
+
32
+
33
+