-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathytvd.py
292 lines (241 loc) · 10.6 KB
/
ytvd.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
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
import sys
import os
import tkinter as tk
from tkinter import ttk, messagebox, filedialog
from yt_dlp import YoutubeDL
import webbrowser
import threading
# Detect platform and set FFmpeg path
ffmpeg_path = os.path.join(os.path.dirname(__file__), "ffmpeg", "bin")
def fetch_qualities():
"""Fetch available video qualities for the given URL."""
video_url = url_entry.get()
if not video_url.strip():
messagebox.showerror("Error", "Please enter a YouTube URL.")
return
# Disable fetch button while fetching
fetch_button.config(state=tk.DISABLED)
quality_dropdown.set('Fetching qualities...')
def fetch_thread():
ydl_opts = {'quiet': True}
try:
with YoutubeDL(ydl_opts) as ydl:
info = ydl.extract_info(video_url, download=False)
formats = info.get('formats', [])
qualities = sorted(
set(f"{fmt['height']}p" for fmt in formats if fmt.get('height') and fmt.get('ext') == 'mp4'),
key=lambda x: int(x.replace('p', ''))
)
if not qualities:
messagebox.showerror("Error", "No available video qualities found.")
return
# Update the quality dropdown with fetched qualities
quality_dropdown['values'] = qualities
quality_var.set(qualities[-1]) # Automatically set to the highest quality
download_video_button.config(state=tk.NORMAL) # Enable download video button
download_mp3_button.config(state=tk.NORMAL) # Enable download MP3 button
except Exception as e:
messagebox.showerror("Error", f"Could not fetch qualities: {e}")
finally:
# Re-enable fetch button after fetching is done
fetch_button.config(state=tk.NORMAL)
quality_dropdown.set(quality_var.get()) # Set the dropdown to the selected quality
# Run the fetch operation in a separate thread
threading.Thread(target=fetch_thread, daemon=True).start()
def download_video():
"""Download the selected video quality."""
video_url = url_entry.get()
output_folder = folder_path.get()
video_quality = quality_var.get()
if not video_url.strip():
messagebox.showerror("Error", "Please enter a YouTube URL.")
return
if not output_folder:
messagebox.showerror("Error", "Please select a download folder.")
return
if not os.path.isdir(output_folder):
messagebox.showerror("Error", "Invalid download folder. Please select a valid directory.")
return
ydl_opts = {
'ffmpeg_location': ffmpeg_path,
'format': f'bestvideo[height={video_quality.replace("p", "")}][vcodec^=avc1]+bestaudio[acodec^=mp4a]/best',
'outtmpl': os.path.join(output_folder, '%(title)s.%(ext)s'),
'merge_output_format': 'mp4',
'progress_hooks': [progress_hook],
}
# Disable buttons during download
fetch_button.config(state=tk.DISABLED)
download_video_button.config(state=tk.DISABLED)
download_mp3_button.config(state=tk.DISABLED)
def download_thread():
try:
progress_var.set(0)
progress_bar.update()
with YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
messagebox.showinfo("Success", "Video downloaded successfully!")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
finally:
# Reset form fields (except download folder)
url_entry.delete(0, tk.END)
quality_dropdown.set('')
quality_dropdown['values'] = []
progress_var.set(0)
progress_bar.update()
download_size_log.set("")
download_size_log_label.pack_forget()
# Re-enable fetch button and disable download button
fetch_button.config(state=tk.NORMAL)
download_video_button.config(state=tk.DISABLED)
download_mp3_button.config(state=tk.DISABLED)
threading.Thread(target=download_thread, daemon=True).start()
def download_mp3():
"""Download the audio as MP3."""
video_url = url_entry.get()
output_folder = folder_path.get()
if not video_url.strip():
messagebox.showerror("Error", "Please enter a YouTube URL.")
return
if not output_folder:
messagebox.showerror("Error", "Please select a download folder.")
return
if not os.path.isdir(output_folder):
messagebox.showerror("Error", "Invalid download folder. Please select a valid directory.")
return
# Configure ydl_opts for MP3 download
ydl_opts = {
'ffmpeg_location': ffmpeg_path,
'format': 'bestaudio/best', # Download the best available audio
'outtmpl': os.path.join(output_folder, '%(title)s.%(ext)s'), # Output file template
'postprocessors': [{
'key': 'FFmpegExtractAudio', # Extract audio
'preferredcodec': 'mp3', # Convert to MP3
'preferredquality': '192', # Set audio quality (192kbps)
}],
'progress_hooks': [progress_hook],
}
# Disable buttons during download
fetch_button.config(state=tk.DISABLED)
download_video_button.config(state=tk.DISABLED)
download_mp3_button.config(state=tk.DISABLED)
def download_thread():
try:
progress_var.set(0)
progress_bar.update()
with YoutubeDL(ydl_opts) as ydl:
ydl.download([video_url])
messagebox.showinfo("Success", "MP3 downloaded successfully!")
except Exception as e:
messagebox.showerror("Error", f"An error occurred: {e}")
finally:
# Reset form fields (except download folder)
url_entry.delete(0, tk.END)
quality_dropdown.set('')
quality_dropdown['values'] = []
progress_var.set(0)
progress_bar.update()
download_size_log.set("")
download_size_log_label.pack_forget()
# Re-enable fetch button and disable download buttons
fetch_button.config(state=tk.NORMAL)
download_video_button.config(state=tk.DISABLED)
download_mp3_button.config(state=tk.DISABLED)
threading.Thread(target=download_thread, daemon=True).start()
def progress_hook(d):
"""Update progress bar based on download progress."""
if d['status'] == 'downloading':
total_bytes = d.get('total_bytes', 0) or d.get('total_bytes_estimate', 0)
downloaded_bytes = d.get('downloaded_bytes', 0)
progress = (downloaded_bytes / total_bytes) * 100 if total_bytes else 0
# Update progress bar
progress_var.set(progress)
progress_bar.update()
# Update download size log
download_size_log.set(f"Downloading: {downloaded_bytes / (1024 * 1024):.2f} MB / {total_bytes / (1024 * 1024):.2f} MB")
# Ensure the log label is visible during the download
if not download_size_log_label.winfo_ismapped():
download_size_log_label.pack(pady=5) # Show the label if it's not already visible
else:
# Hide the download size log once the download completes or is paused
download_size_log.set("") # Clear the log
download_size_log_label.pack_forget() # Hide the label
def browse_folder():
folder = filedialog.askdirectory()
if folder:
folder_path.set(folder)
def open_github(event):
try:
webbrowser.open("https://github.com/nubsuki/YouTube-Downloader")
except Exception as e:
messagebox.showerror("Error", f"Failed to open GitHub: {e}")
# Initialize the main window with title
app = tk.Tk()
app.title("YT Downloader")
# Configure window appearance
window_width = 320
window_height = 420
app.configure(bg="#2e2e2e")
# Calculate screen dimensions for centering
screen_width = app.winfo_screenwidth()
screen_height = app.winfo_screenheight()
# Center window on screen
position_top = int(screen_height / 2 - window_height / 2)
position_left = int(screen_width / 2 - window_width / 2)
# Apply window geometry settings
app.geometry(f'{window_width}x{window_height}+{position_left}+{position_top}')
# Lock window size
app.resizable(False, False)
# Load and set application icon
if hasattr(sys, "_MEIPASS"):
icon_path = os.path.join(sys._MEIPASS, "icon.png")
else:
icon_path = "icon.png"
try:
icon = tk.PhotoImage(file=icon_path)
app.iconphoto(True, icon)
except Exception as e:
print(f"Failed to load icon: {e}")
# URL input
tk.Label(app, text="YouTube URL:", bg="#2e2e2e", fg="white").pack(pady=5)
url_entry = tk.Entry(app, width=50, bg="#555555", fg="white")
url_entry.pack(pady=5)
# Fetch qualities button
fetch_button = tk.Button(app, text="Fetch Qualities", command=fetch_qualities, bg="#555555", fg="white")
fetch_button.pack(pady=5)
# Quality selection
tk.Label(app, text="Select Video Quality:", bg="#2e2e2e", fg="white").pack(pady=5)
quality_var = tk.StringVar()
quality_dropdown = ttk.Combobox(app, textvariable=quality_var, state="readonly")
quality_dropdown.pack(pady=5)
# Folder selection
tk.Label(app, text="Download Folder:", bg="#2e2e2e", fg="white").pack(pady=5)
folder_path = tk.StringVar()
folder_entry = tk.Entry(app, textvariable=folder_path, width=50, bg="#555555", fg="white")
folder_entry.pack(pady=5)
browse_button = tk.Button(app, text="Browse", command=browse_folder, bg="#555555", fg="white")
browse_button.pack(pady=5)
# Progress bar
tk.Label(app, text="Download Progress:", bg="#2e2e2e", fg="white").pack(pady=5)
progress_var = tk.DoubleVar()
progress_bar = ttk.Progressbar(app, variable=progress_var, maximum=100)
progress_bar.pack(pady=5, fill=tk.X, padx=10)
# Size log
download_size_log = tk.StringVar()
download_size_log.set("")
download_size_log_label = tk.Label(app, textvariable=download_size_log, bg="#2e2e2e", fg="white")
# Create a frame to hold the download buttons
button_frame = tk.Frame(app, bg="#2e2e2e")
button_frame.pack(pady=10)
# Download video button
download_video_button = tk.Button(button_frame, text="Download Video", command=download_video, bg="#555555", fg="white", state=tk.DISABLED)
download_video_button.pack(side=tk.LEFT, padx=5)
# Download MP3 button
download_mp3_button = tk.Button(button_frame, text="Download MP3", command=download_mp3, bg="#555555", fg="white", state=tk.DISABLED)
download_mp3_button.pack(side=tk.LEFT, padx=5)
# Author label
name_label = tk.Label(app, text="Nubsuki", font=("Arial", 6), fg="white", bg="#2e2e2e", cursor="hand2", padx=10, pady=10)
name_label.place(relx=1.0, rely=1.0, anchor="se")
# Bind the label click to open GitHub
name_label.bind("<Button-1>", open_github)
app.mainloop()