-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathytwav_gui.py
More file actions
177 lines (147 loc) · 5.27 KB
/
Copy pathytwav_gui.py
File metadata and controls
177 lines (147 loc) · 5.27 KB
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
#!/usr/bin/env python3
"""
YTWAV GUI - Minimalistyczny interfejs graficzny dla YouTube Audio Downloader
Retro-style Tkinter GUI (400x120px, nierozszerzalne)
Wymagania:
- tkinter (wbudowany w Python)
- ytdl_wav.py (logika pobierania)
Autor: Senior Python Developer
Licencja: MIT
"""
import tkinter as tk
from tkinter import messagebox
import shutil
import sys
import os
# Import funkcji pobierania z głównego modułu
try:
from ytdl_wav import download_wav
except ImportError:
messagebox.showerror("Błąd", "Nie można zaimportować ytdl_wav.py")
sys.exit(1)
class YTWavGUI:
"""Minimalistyczny GUI dla pobierania audio z YouTube."""
def __init__(self):
self.root = tk.Tk()
self.setup_window()
self.create_widgets()
self.check_ffmpeg_on_startup()
def setup_window(self):
"""Konfiguruje główne okno aplikacji."""
self.root.title("YT → WAV Downloader")
self.root.geometry("400x120")
self.root.resizable(False, False)
# Centrowanie okna na ekranie
self.root.update_idletasks()
x = (self.root.winfo_screenwidth() // 2) - (400 // 2)
y = (self.root.winfo_screenheight() // 2) - (120 // 2)
self.root.geometry(f"400x120+{x}+{y}")
def create_widgets(self):
"""Tworzy elementy interfejsu."""
# Etykieta
label = tk.Label(
self.root,
text="Wklej link YouTube:",
font=("Arial", 10)
)
label.pack(pady=(15, 5))
# Pole tekstowe na URL
self.url_entry = tk.Entry(
self.root,
width=50,
font=("Arial", 9)
)
self.url_entry.pack(pady=5)
# Przycisk pobierania
download_btn = tk.Button(
self.root,
text="Pobierz",
command=self.download_audio,
font=("Arial", 10, "bold"),
width=15,
height=1
)
download_btn.pack(pady=(10, 15))
# Focus na pole tekstowe
self.url_entry.focus()
# Bind Enter key do pobierania
self.root.bind('<Return>', lambda event: self.download_audio())
def check_ffmpeg_on_startup(self):
"""Sprawdza dostępność FFmpeg przy starcie aplikacji."""
if not shutil.which("ffmpeg"):
messagebox.showerror(
"Błąd FFmpeg",
"FFmpeg nie jest zainstalowany lub niedostępny w PATH.\n\n"
"Instrukcje instalacji:\n"
"• macOS: brew install ffmpeg"
)
self.root.destroy()
sys.exit(1)
def download_audio(self):
"""Handler przycisku pobierania."""
url = self.url_entry.get().strip()
# Sprawdzenie czy URL został podany
if not url:
messagebox.showwarning(
"Brak linku",
"Proszę wkleić link YouTube do pobrania."
)
return
# Sprawdzenie czy to prawidłowy URL YouTube
youtube_domains = ['youtube.com', 'youtu.be', 'm.youtube.com', 'www.youtube.com']
if not any(domain in url for domain in youtube_domains):
messagebox.showwarning(
"Nieprawidłowy link",
"To nie wygląda na prawidłowy link YouTube."
)
return
# Wyłączenie przycisku podczas pobierania
download_btn = None
for widget in self.root.winfo_children():
if isinstance(widget, tk.Button):
download_btn = widget
break
if download_btn:
download_btn.config(state="disabled", text="Pobieranie...")
self.root.update()
try:
# Wywołanie funkcji pobierania
success = download_wav(url, "wav_out")
if success:
messagebox.showinfo(
"Sukces",
"Audio zostało pomyślnie pobrane i zapisane jako WAV!\n\n"
"Lokalizacja: wav_out/"
)
# Wyczyść pole tekstowe po sukcesie
self.url_entry.delete(0, tk.END)
else:
messagebox.showerror(
"Błąd pobierania",
"Nie udało się pobrać audio.\n\n"
"Sprawdź link YouTube i połączenie internetowe."
)
except Exception as e:
messagebox.showerror(
"Nieoczekiwany błąd",
f"Wystąpił błąd podczas pobierania:\n\n{str(e)}"
)
finally:
# Przywrócenie przycisku
if download_btn:
download_btn.config(state="normal", text="Pobierz")
def run(self):
"""Uruchamia główną pętlę GUI."""
self.root.mainloop()
def main():
"""Główna funkcja GUI."""
try:
app = YTWavGUI()
app.run()
except KeyboardInterrupt:
print("\nZamykanie aplikacji...")
except Exception as e:
messagebox.showerror("Błąd krytyczny", f"Nie można uruchomić aplikacji:\n\n{e}")
sys.exit(1)
if __name__ == "__main__":
main()