-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
174 lines (145 loc) · 5.94 KB
/
main.py
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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
import re
import tkinter as tk
from tkinter import filedialog, font
from customtkinter import CTkEntry, CTkButton
from pytube import YouTube
import winsound
# Initialize the main window
root = tk.Tk()
root.title("YouTube Downloader")
root.geometry("889x500")
root.config(bg='#302E2E')
root.resizable(False, False)
# Define fonts
title_font = font.Font(family='Press Start 2P', size=20)
button_font = font.Font(family='Fira Code', size=14)
# Heading label
heading_label = tk.Label(text='YouTube Downloader', font=title_font, bg='#302E2E', fg='#FFFFFF')
heading_label.grid(row=0, column=3, padx=230, pady=30, sticky="w")
# Link entry field
link_field1 = CTkEntry(master=root,
font=('Fira Code', 15),
fg_color='#302E2E',
width=625,
text_color='white',
height=38,
border_width=1,
corner_radius=10,
placeholder_text="Paste the link")
link_field1.grid(row=3, column=3, padx=150, pady=20, sticky="w")
# File path entry field
file_field = CTkEntry(master=root,
font=('Fira Code', 15),
fg_color='#302E2E',
width=440,
text_color='white',
height=38,
border_width=1,
corner_radius=10,
placeholder_text="Select the Folder",
state='disabled')
file_field.grid(row=6, column=3, padx=150, pady=20, sticky="w")
# Label to display completion message
completion_label = tk.Label(master=root,
text='',
font=button_font,
bg='#302E2E',
fg='#FFFFFF')
completion_label.grid(row=9, column=3, padx=350, pady=20, sticky="w")
# Function to select the path
def select_path():
file = filedialog.askdirectory(title="Select Folder")
file_field.configure(state="normal")
file_field.delete(0, tk.END)
if file:
file_field.insert(0, file)
else:
file_field.insert(0, "Select the folder")
file_field.configure(state="disabled")
# Function to sanitize the filename
def sanitize_filename(filename):
return re.sub(r'[\\/*?:"<>|]', "", filename)
def play_sound(sound_type):
if sound_type == "complete":
winsound.PlaySound("complete.wav", winsound.SND_FILENAME)
elif sound_type == "error":
winsound.PlaySound("error.wav", winsound.SND_FILENAME)
# Function to download audio
def audio_download():
urls = link_field1.get()
output_path = file_field.get()
if not urls or output_path == "Select the folder":
print("Please provide both the link and the folder path.")
completion_label.config(text="Please provide link and folder path.", fg='#FF0000')
completion_label.grid(row=9, column=3, padx=250, pady=20, sticky="w")
play_sound("error")
return
vid = YouTube(urls)
audio_stream = vid.streams.filter(only_audio=True).first()
entry = sanitize_filename(vid.title)
print(f"\nVideo found: {entry}\n")
print("Downloading Audio...")
audio_stream.download(output_path, filename=f"{entry}.mp3")
print("Audio download completed.")
completion_label.config(text="Download completed!", fg='#00FF00')
completion_label.grid(row=9, column=3, padx=350, pady=20, sticky="w")
play_sound("complete")
# Function to download video
def video_download():
urls = link_field1.get()
output_path = file_field.get()
if not urls or output_path == "Select the folder":
print("Please provide both the link and the folder path.")
completion_label.config(text="Please provide link and folder path.", fg='#FF0000')
completion_label.grid(row=9, column=3, padx=250, pady=20, sticky="w")
play_sound("error")
return
vid = YouTube(urls)
video_stream = vid.streams.filter(progressive=True, res="1080p").first()
entry = sanitize_filename(vid.title)
if not video_stream:
print("1080p video not available as a single stream with audio.")
video_stream = vid.streams.filter(progressive=True).order_by('resolution').desc().first()
print(f"\nVideo found: {entry}\n")
print("Downloading Video...")
video_stream.download(output_path, filename=f"{entry}.mp4")
print("Video download completed.")
completion_label.config(text="Download completed!", fg='#00FF00')
completion_label.grid(row=9, column=3, padx=350, pady=20, sticky="w")
play_sound("complete")
# Browse button
browse_button = CTkButton(master=root,
font=('Fira Code', 15),
fg_color='#AF2D2D',
width=150,
text='Browse Folder',
text_color='white',
height=38,
anchor='right',
command=select_path,
hover_color='#DF7C7C')
browse_button.grid(row=6, column=3, padx=620, pady=20, sticky="e")
# Mp3 convert button
yt_audio = CTkButton(master=root,
font=('Fira Code', 15),
fg_color='#AF2D2D',
width=300,
text='Mp3 convert',
text_color='white',
height=38,
hover_color='#DF7C7C',
command=audio_download)
yt_audio.grid(row=8, column=3, padx=150, pady=20, sticky="w")
# Mp4 convert button
yt_video = CTkButton(master=root,
font=('Fira Code', 15),
fg_color='#AF2D2D',
width=300,
text='Mp4 convert',
text_color='white',
height=38,
hover_color="#DF7C7C",
command=video_download)
yt_video.grid(row=8, column=3, padx=475, pady=20, sticky="w")
# Run the main loop
root.mainloop()