-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathyoutube-downloader.py
More file actions
75 lines (63 loc) · 2.72 KB
/
youtube-downloader.py
File metadata and controls
75 lines (63 loc) · 2.72 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
# Copyright : fantomas4o (2023)
# Github : https://github.com/fantomas4o/Python-YouTube-MP3-Downloader.git
# Contact : https://urocibg.eu
import tkinter as tk
from tkinter import filedialog
from tkinter import messagebox
from pytube import YouTube
import os
destination_variable = None
def download_mp3(url, destination='.', quality='best'):
global destination_variable
try:
yt = YouTube(url)
audio_stream = yt.streams.filter(only_audio=True).first()
destination_folder = destination_variable.get() if isinstance(destination_variable, tk.StringVar) else destination_variable
audio_stream.download(output_path=destination_folder)
base, ext = os.path.splitext(audio_stream.title)
new_file = os.path.join(destination_folder, base + '.mp3')
os.rename(os.path.join(destination_folder, audio_stream.default_filename), new_file)
messagebox.showinfo("Download Completed", "The file was successfully downloaded and renamed to MP3 format.")
return new_file
except Exception as e:
messagebox.showerror("Error", f"Error while downloading: {str(e)}")
return None
def choose_destination():
global destination_variable
chosen_folder = filedialog.askdirectory()
destination_variable.set(chosen_folder)
def paste_url(root):
clipboard_text = root.clipboard_get()
url_entry.delete(0, tk.END)
url_entry.insert(0, clipboard_text)
def main():
global destination_variable
global url_entry
root = tk.Tk()
root.title("Download MP3 Files")
root.geometry("500x250")
url_label = tk.Label(root, text="URL:")
url_label.pack()
url_entry = tk.Entry(root, width=50)
url_entry.pack()
paste_button = tk.Button(root, text="Paste URL", command=lambda: paste_url(root))
paste_button.pack()
destination_label = tk.Label(root, text="Destination:")
destination_label.pack()
destination_variable = tk.StringVar(root, value=os.getcwd())
destination_entry = tk.Entry(root, textvariable=destination_variable)
destination_entry.pack()
destination_button = tk.Button(root, text="Select Folder", command=choose_destination)
destination_button.pack()
quality_label = tk.Label(root, text="Select Audio Quality:")
quality_label.pack()
quality_options = ['best', 'high', 'medium', 'low']
quality_variable = tk.StringVar(root)
quality_variable.set(quality_options[0])
quality_menu = tk.OptionMenu(root, quality_variable, *quality_options)
quality_menu.pack()
download_button = tk.Button(root, text="Download", command=lambda: download_mp3(url_entry.get(), destination_variable, quality_variable.get()))
download_button.pack()
root.mainloop()
if __name__ == "__main__":
main()