Spaces:
Sleeping
Sleeping
Commit
·
41c4760
1
Parent(s):
bb077a1
Update yt_download.py
Browse files- yt_download.py +58 -53
yt_download.py
CHANGED
@@ -1,53 +1,58 @@
|
|
1 |
-
import argparse
|
2 |
-
from pytube import YouTube
|
3 |
-
from tqdm import tqdm
|
4 |
-
import os
|
5 |
-
|
6 |
-
def download_youtube_video(video_url, download_captions=False):
|
7 |
-
progress_bar = None
|
8 |
-
|
9 |
-
|
10 |
-
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
progress_bar.
|
16 |
-
progress_bar.
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
if
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
|
36 |
-
|
37 |
-
|
38 |
-
|
39 |
-
|
40 |
-
|
41 |
-
|
42 |
-
|
43 |
-
|
44 |
-
|
45 |
-
|
46 |
-
|
47 |
-
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
from pytube import YouTube
|
3 |
+
from tqdm import tqdm
|
4 |
+
import os
|
5 |
+
|
6 |
+
def download_youtube_video(video_url, download_captions=False):
|
7 |
+
progress_bar = None
|
8 |
+
|
9 |
+
# Updates progress bar
|
10 |
+
def progress_function(stream, chunk, bytes_remaining):
|
11 |
+
nonlocal progress_bar
|
12 |
+
if progress_bar is None:
|
13 |
+
progress_bar = tqdm(total=stream.filesize, unit='B', unit_scale=True, desc="Downloading Video")
|
14 |
+
current = stream.filesize - bytes_remaining
|
15 |
+
progress_bar.n = current
|
16 |
+
progress_bar.last_print_n = current
|
17 |
+
progress_bar.update()
|
18 |
+
|
19 |
+
if not os.path.exists('./downloads'):
|
20 |
+
os.makedirs('./downloads')
|
21 |
+
|
22 |
+
# Youtube object with inputted url
|
23 |
+
yt = YouTube(
|
24 |
+
video_url,
|
25 |
+
on_progress_callback=progress_function,
|
26 |
+
)
|
27 |
+
|
28 |
+
# Downloading video
|
29 |
+
stream = yt.streams.get_highest_resolution()
|
30 |
+
stream.download(output_path='./downloads')
|
31 |
+
if progress_bar:
|
32 |
+
progress_bar.close()
|
33 |
+
|
34 |
+
# Download captions (default=disabled)
|
35 |
+
if download_captions:
|
36 |
+
caption = yt.captions.get('en') or yt.captions.get('a.en')
|
37 |
+
if caption:
|
38 |
+
caption_convert_to_srt = caption.generate_srt_captions()
|
39 |
+
caption_convert_to_srt = caption_convert_to_srt.replace("\n\n", "\n")
|
40 |
+
with open(os.path.join('./downloads', f"{yt.title}.srt"), "w", encoding="utf-8") as file:
|
41 |
+
file.write(caption_convert_to_srt)
|
42 |
+
print(f"Captions saved to 'downloads/{yt.title}.srt'")
|
43 |
+
else:
|
44 |
+
print("No English captions found for this video.")
|
45 |
+
|
46 |
+
# Driver function
|
47 |
+
def download_video(url, download_captions=False):
|
48 |
+
video_path = './downloads/' + YouTube(url).streams.get_highest_resolution().default_filename
|
49 |
+
download_youtube_video(url, download_captions)
|
50 |
+
return video_path
|
51 |
+
|
52 |
+
if __name__ == "__main__":
|
53 |
+
parser = argparse.ArgumentParser(description="Download YouTube video and captions")
|
54 |
+
parser.add_argument("video_url", help="YouTube video URL")
|
55 |
+
parser.add_argument("--captions", action="store_true", help="Download captions if available")
|
56 |
+
args = parser.parse_args()
|
57 |
+
|
58 |
+
download_video(args.video_url, args.captions)
|