-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.pyw
78 lines (71 loc) · 2.81 KB
/
main.pyw
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import PySimpleGUI as sg
from pathlib import Path
import pytube
import re
sg.LOOK_AND_FEEL_TABLE['YouTube'] = \
{'BACKGROUND': '#FFFFFF',
'TEXT': '#000000',
'INPUT': '#FFFFFF',
'TEXT_INPUT': '#000000',
'SCROLL': '#000000',
'BUTTON': ('#FF0000', '#FFFFFF'),
'PROGRESS': ('#FF0000', '#FFFFFF'),
'BORDER': 1, 'SLIDER_DEPTH': 0,
'PROGRESS_DEPTH': 0 }
sg.theme('YouTube')
layout = [
[sg.Column([[sg.Image(str(Path(__file__).parent.joinpath("logo.png")), size=(400, 200),subsample=5,)]], justification='center')],
[sg.Text("Enter the URL of the YouTube video/playlist you want to Download", font=("Arial", 12))],
[sg.InputText()],
[sg.Text("Select Video Quality", font=("Arial",12))],
[sg.Combo(["High", "Low"], size=(10, 10), key='-COMBO-')],
[sg.Text("Select the folder where you want to save the video", font=("Arial", 12))],
[sg.InputText(key="-PATH-"), sg.FolderBrowse()],
[sg.Text("\n")],
[sg.Button("Download",size=(10,2),auto_size_button= True), sg.Button("Cancel",size=(10,2),auto_size_button= True)]
]
def download(url, path, quality):
playlist=r"^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/playlist(.*)$"
video=r"^(https?\:\/\/)?(www\.youtube\.com|youtu\.?be)\/(watch|shorts)(.*)$"
if re.match(playlist, url) or 'list=' in url:
playlist = pytube.Playlist(url)
for video in playlist.videos:
if quality == "High":
video.streams.get_highest_resolution().download(path)
if quality == "Low":
video.streams.get_lowest_resolution().download(path)
return 0
elif re.match(video, url):
video = pytube.YouTube(url)
if quality == "High":
video.streams.get_highest_resolution().download(path)
if quality == "Low":
video.streams.get_lowest_resolution().download(path)
return 0
else:
return -1
def main():
window = sg.Window("YouTube Downloader", layout)
while True:
event, values = window.read()
if event == sg.WIN_CLOSED or event == "Cancel":
break
elif not(values) or values["-PATH-"] == "" or values["-COMBO-"] == "":
sg.popup_ok("Please enter a valid URL and path!")
continue
elif event == "Download":
url = values[1]
path = values["-PATH-"]
quality=values["-COMBO-"]
Path(path).mkdir(parents=True, exist_ok=True)
sg.popup_no_wait("Please wait while we try to download your video")
flag = download(url, path, quality)
if flag == -1:
sg.popup_error("Error in downloading!")
elif flag == 0:
sg.popup("Download complete!")
else:
sg.popup_error("Internal error in Code!")
window.close()
if __name__ == "__main__":
main()