RobCaamano commited on
Commit
41c4760
·
1 Parent(s): bb077a1

Update yt_download.py

Browse files
Files changed (1) hide show
  1. 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
- def progress_function(stream, chunk, bytes_remaining):
10
- nonlocal progress_bar
11
- if progress_bar is None:
12
- progress_bar = tqdm(total=stream.filesize, unit='B', unit_scale=True, desc="Downloading Video")
13
- current = stream.filesize - bytes_remaining
14
- progress_bar.n = current
15
- progress_bar.last_print_n = current
16
- progress_bar.update()
17
-
18
- if not os.path.exists('./downloads'):
19
- os.makedirs('./downloads')
20
-
21
- yt = YouTube(
22
- video_url,
23
- on_progress_callback=progress_function,
24
- )
25
-
26
- stream = yt.streams.get_highest_resolution()
27
- stream.download(output_path='./downloads')
28
- if progress_bar:
29
- progress_bar.close()
30
-
31
- if download_captions:
32
- caption = yt.captions.get('en') or yt.captions.get('a.en')
33
- if caption:
34
- caption_convert_to_srt = caption.generate_srt_captions()
35
- caption_convert_to_srt = caption_convert_to_srt.replace("\n\n", "\n")
36
- with open(os.path.join('./downloads', f"{yt.title}.srt"), "w", encoding="utf-8") as file:
37
- file.write(caption_convert_to_srt)
38
- print(f"Captions saved to 'downloads/{yt.title}.srt'")
39
- else:
40
- print("No English captions found for this video.")
41
-
42
- def download_video(url, download_captions=False):
43
- video_path = './downloads/' + YouTube(url).streams.get_highest_resolution().default_filename
44
- download_youtube_video(url, download_captions)
45
- return video_path
46
-
47
- if __name__ == "__main__":
48
- parser = argparse.ArgumentParser(description="Download YouTube video and captions")
49
- parser.add_argument("video_url", help="YouTube video URL")
50
- parser.add_argument("--captions", action="store_true", help="Download captions if available")
51
- args = parser.parse_args()
52
-
53
- download_video(args.video_url, args.captions)
 
 
 
 
 
 
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)