1
1
import yt_dlp
2
2
import os
3
3
import argparse
4
+ from win10toast import ToastNotifier
4
5
5
6
def get_default_dir ():
6
7
return os .path .join (os .path .expanduser ('~' ), "Downloads" )
@@ -10,35 +11,19 @@ def download_media(url, save_to=None, resolution=None):
10
11
save_to = get_default_dir ()
11
12
12
13
if not os .path .exists (save_to ):
13
- os .mkdir (save_to )
14
+ os .mkdir (save_to , exist_ok = True )
14
15
15
16
ydl_opts = {
16
17
'outtmpl' : os .path .join (save_to , '%(title)s.%(ext)s' ), # Output path and filename template
18
+ 'format' : 'bestvideo[height<=?{0}]+bestaudio/best[height<=?{0}]' .format (resolution or '1080' )
17
19
}
18
20
19
21
try :
20
22
with yt_dlp .YoutubeDL (ydl_opts ) as ydl :
21
23
info_dict = ydl .extract_info (url , download = True )
22
- formats = info_dict .get ('formats' , [])
23
-
24
- selected_format = None
25
-
26
- for format_item in formats :
27
- if 'height' in format_item :
28
- if resolution and f'{ resolution } p' in format_item .get ('format_note' , '' ).lower ():
29
- selected_format = format_item
30
- break
31
- elif not resolution :
32
- selected_format = format_item
33
-
34
- if selected_format :
35
- print (f"Selected Resolution: { selected_format ['format_note' ]} " )
36
- ydl_opts ['format' ] = selected_format ['format_id' ]
37
- ydl .download ([url ])
38
- downloaded_file_path = os .path .join (save_to , f"{ info_dict .get ('title' )} .mp4" )
39
- print (f"Download completed successfully. File saved to: { downloaded_file_path } " )
40
- else :
41
- print ("No suitable format found for the requested resolution." )
24
+ downloaded_file_path = os .path .join (save_to , f"{ info_dict .get ('title' )} .mp4" )
25
+ ToastNotifier ().show_toast ('Downloading Completed Successfully' , f'File saved to : { downloaded_file_path } ' )
26
+ print (f"Download completed successfully. File saved to: { downloaded_file_path } " )
42
27
43
28
print ('\n ------------------------------Video Info------------------------------\n ' )
44
29
print (f"Title: { info_dict .get ('title' )} " )
0 commit comments