144 lines
4.2 KiB
Python
144 lines
4.2 KiB
Python
import os
|
|
import tkinter as tk
|
|
from tkinter import filedialog
|
|
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):
|
|
if not os.path.exists(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():
|
|
url = url_entry.get()
|
|
save_as_mp3 = mp3_var.get()
|
|
save_as_mp4 = mp4_var.get()
|
|
selected_quality = quality_var.get()
|
|
|
|
if not save_as_mp3 and not save_as_mp4:
|
|
status_label.config(text="Please select at least one format.")
|
|
return
|
|
|
|
try:
|
|
video = YouTube(url)
|
|
|
|
if save_as_mp3:
|
|
audio = video.streams.filter(only_audio=True).first()
|
|
create_directory(mp3_output_directory)
|
|
audio.download(output_path=mp3_output_directory)
|
|
status_label.config(text="MP3 file downloaded successfully.")
|
|
|
|
if save_as_mp4:
|
|
create_directory(mp4_output_directory)
|
|
stream = video.streams.filter(progressive=True)
|
|
filtered_streams = []
|
|
for s in stream:
|
|
if s.resolution == selected_quality:
|
|
filtered_streams.append(s)
|
|
if len(filtered_streams) > 0:
|
|
filtered_streams[0].download(output_path=mp4_output_directory)
|
|
status_label.config(text="MP4 file downloaded successfully.")
|
|
else:
|
|
status_label.config(text="No stream found in the specified quality.")
|
|
|
|
except Exception as e:
|
|
status_label.config(text="Error while downloading: " + str(e))
|
|
|
|
|
|
# Create GUI
|
|
window = tk.Tk()
|
|
window.title("YouTube Downloader")
|
|
|
|
# URL entry field
|
|
url_label = tk.Label(window, text="YouTube URL:")
|
|
url_label.pack()
|
|
url_entry = tk.Entry(window, width=50)
|
|
url_entry.pack()
|
|
|
|
# Format selection checkboxes
|
|
format_label = tk.Label(window, text="Format:")
|
|
format_label.pack()
|
|
mp3_var = tk.IntVar()
|
|
mp3_checkbox = tk.Checkbutton(window, text=".mp3", variable=mp3_var)
|
|
mp3_checkbox.pack()
|
|
mp4_var = tk.IntVar()
|
|
mp4_checkbox = tk.Checkbutton(window, text=".mp4", variable=mp4_var)
|
|
mp4_checkbox.pack()
|
|
|
|
# Video quality selection dropdown
|
|
quality_label = tk.Label(window, text="Video Quality:")
|
|
quality_label.pack()
|
|
quality_var = tk.StringVar()
|
|
quality_var.set("720p") # Default quality
|
|
quality_dropdown = tk.OptionMenu(window, quality_var, "144p", "240p", "360p", "480p", "720p")
|
|
quality_dropdown.pack()
|
|
|
|
# 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.pack()
|
|
|
|
# Status label
|
|
status_label = tk.Label(window, text="")
|
|
status_label.pack()
|
|
|
|
# Load directories
|
|
load_directories()
|
|
|
|
# Save directories before closing the window
|
|
window.protocol("WM_DELETE_WINDOW", lambda: [save_directories(), window.destroy()])
|
|
|
|
# Start GUI
|
|
window.mainloop()
|