Added functionality to save and load output directories.
This commit is contained in:
+78
-19
@@ -1,6 +1,15 @@
|
|||||||
import os
|
import os
|
||||||
import tkinter as tk
|
import tkinter as tk
|
||||||
|
from tkinter import filedialog
|
||||||
from pytube import YouTube
|
from pytube import YouTube
|
||||||
|
import pickle
|
||||||
|
|
||||||
|
# Speicherort für MP3- und MP4-Dateien
|
||||||
|
mp3_output_directory = ""
|
||||||
|
mp4_output_directory = ""
|
||||||
|
|
||||||
|
# Dateinamen für die Speicherung der Verzeichnisse
|
||||||
|
config_file = "directories.pickle"
|
||||||
|
|
||||||
|
|
||||||
def create_directory(directory):
|
def create_directory(directory):
|
||||||
@@ -8,6 +17,40 @@ def create_directory(directory):
|
|||||||
os.makedirs(directory)
|
os.makedirs(directory)
|
||||||
|
|
||||||
|
|
||||||
|
def save_directories():
|
||||||
|
global mp3_output_directory, mp4_output_directory
|
||||||
|
|
||||||
|
directories = {
|
||||||
|
"mp3": mp3_output_directory,
|
||||||
|
"mp4": mp4_output_directory
|
||||||
|
}
|
||||||
|
|
||||||
|
with open(config_file, "wb") as f:
|
||||||
|
pickle.dump(directories, f)
|
||||||
|
|
||||||
|
|
||||||
|
def load_directories():
|
||||||
|
global mp3_output_directory, mp4_output_directory
|
||||||
|
|
||||||
|
if os.path.isfile(config_file):
|
||||||
|
with open(config_file, "rb") as f:
|
||||||
|
directories = pickle.load(f)
|
||||||
|
mp3_output_directory = directories.get("mp3", "")
|
||||||
|
mp4_output_directory = directories.get("mp4", "")
|
||||||
|
|
||||||
|
|
||||||
|
def select_directory(format_type):
|
||||||
|
global mp3_output_directory, mp4_output_directory
|
||||||
|
|
||||||
|
# Dialogfenster für die Auswahl des Ordners öffnen
|
||||||
|
directory = filedialog.askdirectory()
|
||||||
|
|
||||||
|
if format_type == "mp3":
|
||||||
|
mp3_output_directory = directory
|
||||||
|
elif format_type == "mp4":
|
||||||
|
mp4_output_directory = directory
|
||||||
|
|
||||||
|
|
||||||
def download_video():
|
def download_video():
|
||||||
url = url_entry.get()
|
url = url_entry.get()
|
||||||
save_as_mp3 = mp3_var.get()
|
save_as_mp3 = mp3_var.get()
|
||||||
@@ -15,7 +58,7 @@ def download_video():
|
|||||||
selected_quality = quality_var.get()
|
selected_quality = quality_var.get()
|
||||||
|
|
||||||
if not save_as_mp3 and not save_as_mp4:
|
if not save_as_mp3 and not save_as_mp4:
|
||||||
status_label.config(text="Bitte wählen Sie mindestens ein Format aus.")
|
status_label.config(text="Please select at least one format.")
|
||||||
return
|
return
|
||||||
|
|
||||||
try:
|
try:
|
||||||
@@ -23,38 +66,38 @@ def download_video():
|
|||||||
|
|
||||||
if save_as_mp3:
|
if save_as_mp3:
|
||||||
audio = video.streams.filter(only_audio=True).first()
|
audio = video.streams.filter(only_audio=True).first()
|
||||||
create_directory("mp3")
|
create_directory(mp3_output_directory)
|
||||||
audio.download(output_path="D:/Youtube Download/mp3/")
|
audio.download(output_path=mp3_output_directory)
|
||||||
status_label.config(text="MP3-Datei erfolgreich heruntergeladen.")
|
status_label.config(text="MP3 file downloaded successfully.")
|
||||||
|
|
||||||
if save_as_mp4:
|
if save_as_mp4:
|
||||||
create_directory("mp4")
|
create_directory(mp4_output_directory)
|
||||||
stream = video.streams.filter(progressive=True)
|
stream = video.streams.filter(progressive=True)
|
||||||
filtered_streams = []
|
filtered_streams = []
|
||||||
for s in stream:
|
for s in stream:
|
||||||
if s.resolution == selected_quality:
|
if s.resolution == selected_quality:
|
||||||
filtered_streams.append(s)
|
filtered_streams.append(s)
|
||||||
if len(filtered_streams) > 0:
|
if len(filtered_streams) > 0:
|
||||||
filtered_streams[0].download(output_path="D:/Youtube Download/mp4/")
|
filtered_streams[0].download(output_path=mp4_output_directory)
|
||||||
status_label.config(text="MP4-Datei erfolgreich heruntergeladen.")
|
status_label.config(text="MP4 file downloaded successfully.")
|
||||||
else:
|
else:
|
||||||
status_label.config(text="Kein Stream in der angegebenen Qualität gefunden.")
|
status_label.config(text="No stream found in the specified quality.")
|
||||||
|
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
status_label.config(text="Fehler beim Herunterladen: " + str(e))
|
status_label.config(text="Error while downloading: " + str(e))
|
||||||
|
|
||||||
|
|
||||||
# GUI erstellen
|
# Create GUI
|
||||||
window = tk.Tk()
|
window = tk.Tk()
|
||||||
window.title("YouTube Downloader")
|
window.title("YouTube Downloader")
|
||||||
|
|
||||||
# URL-Eingabefeld
|
# URL entry field
|
||||||
url_label = tk.Label(window, text="YouTube-URL:")
|
url_label = tk.Label(window, text="YouTube URL:")
|
||||||
url_label.pack()
|
url_label.pack()
|
||||||
url_entry = tk.Entry(window, width=50)
|
url_entry = tk.Entry(window, width=50)
|
||||||
url_entry.pack()
|
url_entry.pack()
|
||||||
|
|
||||||
# Auswahlmöglichkeiten für Dateiformate
|
# Format selection checkboxes
|
||||||
format_label = tk.Label(window, text="Format:")
|
format_label = tk.Label(window, text="Format:")
|
||||||
format_label.pack()
|
format_label.pack()
|
||||||
mp3_var = tk.IntVar()
|
mp3_var = tk.IntVar()
|
||||||
@@ -64,21 +107,37 @@ mp4_var = tk.IntVar()
|
|||||||
mp4_checkbox = tk.Checkbutton(window, text=".mp4", variable=mp4_var)
|
mp4_checkbox = tk.Checkbutton(window, text=".mp4", variable=mp4_var)
|
||||||
mp4_checkbox.pack()
|
mp4_checkbox.pack()
|
||||||
|
|
||||||
# Auswahlmöglichkeiten für Videoqualität
|
# Video quality selection dropdown
|
||||||
quality_label = tk.Label(window, text="Videoqualität:")
|
quality_label = tk.Label(window, text="Video Quality:")
|
||||||
quality_label.pack()
|
quality_label.pack()
|
||||||
quality_var = tk.StringVar()
|
quality_var = tk.StringVar()
|
||||||
quality_var.set("720p") # Standard-Qualität
|
quality_var.set("720p") # Default quality
|
||||||
quality_dropdown = tk.OptionMenu(window, quality_var, "144p", "240p", "360p", "480p", "720p")
|
quality_dropdown = tk.OptionMenu(window, quality_var, "144p", "240p", "360p", "480p", "720p")
|
||||||
quality_dropdown.pack()
|
quality_dropdown.pack()
|
||||||
|
|
||||||
# Download-Button
|
# MP3 output directory selection button
|
||||||
|
mp3_directory_button = tk.Button(window, text="Select MP3 Output Directory",
|
||||||
|
command=lambda: select_directory("mp3"))
|
||||||
|
mp3_directory_button.pack()
|
||||||
|
|
||||||
|
# MP4 output directory selection button
|
||||||
|
mp4_directory_button = tk.Button(window, text="Select MP4 Output Directory",
|
||||||
|
command=lambda: select_directory("mp4"))
|
||||||
|
mp4_directory_button.pack()
|
||||||
|
|
||||||
|
# Download button
|
||||||
download_button = tk.Button(window, text="Download", command=download_video)
|
download_button = tk.Button(window, text="Download", command=download_video)
|
||||||
download_button.pack()
|
download_button.pack()
|
||||||
|
|
||||||
# Statuslabel
|
# Status label
|
||||||
status_label = tk.Label(window, text="")
|
status_label = tk.Label(window, text="")
|
||||||
status_label.pack()
|
status_label.pack()
|
||||||
|
|
||||||
# GUI starten
|
# Load directories
|
||||||
|
load_directories()
|
||||||
|
|
||||||
|
# Save directories before closing the window
|
||||||
|
window.protocol("WM_DELETE_WINDOW", lambda: [save_directories(), window.destroy()])
|
||||||
|
|
||||||
|
# Start GUI
|
||||||
window.mainloop()
|
window.mainloop()
|
||||||
|
|||||||
Reference in New Issue
Block a user