Spaces:
Runtime error
Runtime error
Update ytdownloader.py
Browse files- 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 |
-
|
7 |
-
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
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 |
+
|