-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
155 lines (115 loc) · 4.63 KB
/
main.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
import tkinter
import customtkinter
from pytube import YouTube
from pytube import Playlist
import threading
import ssl
import os
import re
import time
#Descarga de video
ssl._create_default_https_context = ssl._create_unverified_context
now = time.time()
time_struct = time.gmtime(now)
day = time_struct.tm_wday
month = time_struct.tm_mon
year = time_struct.tm_year
def start_download_thread():
percentage.configure(text="0%")
finishLabel.configure(text="")
progressBar.set(0)
download_thread = threading.Thread(target=download_video)
download_thread.daemon = True
download_thread.start()
percentage.pack()
progressBar.pack(padx=10, pady=10)
def download_video(output_path=f"Videos_descargados/{day}-{month}-{year}"):
try:
url = URL.get()
print(url)
if "youtube.com/playlist" in url:
progressBar.pack_forget()
percentage.pack_forget()
p = Playlist(url)
total_videos = len(p.video_urls)
current_video = 0
for video_url in p.video_urls:
current_video += 1
finishLabel.configure(text=f"Espere...Descargando video {current_video} de {total_videos}")
yt = YouTube(video_url)
invalid_chars = r'".<>:"/\|?*'
cleaned_title = re.sub(f'[{re.escape(invalid_chars)}]', '', yt.title) # Eliminar caracteres inválidos
if music.get():
video = yt.streams.filter(only_audio=True).first()
filename = f"{cleaned_title}.mp3"
else:
video = yt.streams.get_highest_resolution()
filename = f"{cleaned_title}.mp4"
title.configure(text=yt.title, text_color="white")
video.download(filename=filename, output_path=output_path)
finishLabel.configure(text="Descargado!")
progressBar.pack_forget()
else:
yt = YouTube(url, on_progress_callback=on_progress)
if(music.get()):
video = yt.streams.filter(only_audio=True).first()
video = yt.streams.get_audio_only()
filename=f"{video.title}.mp3"
else:
video_stream = yt.streams.filter(progressive=True, file_extension='mp4').order_by('resolution').desc().first()
filename=f"{video.title}.mp4"
title.configure(text=yt.title, text_color="white")
finishLabel.configure(text="",text_color="white")
video.download( output_path=output_path)
finishLabel.configure(text="Descargado!")
URL.set("")
title.configure(text="Ingrese el link del video")
except Exception as e:
finishLabel.configure(text=f"Error {e}", text_color="red")
print(e)
def on_progress(stream, chunk, bytes_remaining):
total_size = stream.filesize
bytes_downloaded = total_size - bytes_remaining
percentage_of_completion = bytes_downloaded / total_size * 100
per = str(int(percentage_of_completion))
percentage.configure(text=per + '%')
percentage.update()
progressBar.set(float(percentage_of_completion) / 100)
#Configuration de interfaz
customtkinter.set_appearance_mode("dark")
customtkinter.set_default_color_theme("blue")
#app frame
app = customtkinter.CTk()
app.geometry("720x480")
app.title("Youtube")
frame = customtkinter.CTkFrame(master=app,
width=200,
height=200,
corner_radius=10)
frame.place(relx=0.5, rely=0.5, anchor=tkinter.CENTER)
#Elementos UI
title = customtkinter.CTkLabel(frame,text="Ingrese el link del video",
compound = "center",
anchor= "center")
title.pack(padx=10, pady=20)
music = customtkinter.BooleanVar()
check_music = customtkinter.CTkCheckBox(frame, text="Audio",variable=music, onvalue=True, offvalue=False)
check_music.pack(side="top", anchor="w",padx=9, pady=10)
#Link input
URL = tkinter.StringVar()
link = customtkinter.CTkEntry(frame, width=350, height=40, textvariable=URL)
link.pack(padx=10)
#Download button
download_button = customtkinter.CTkButton(frame, text="Descargar", command=start_download_thread)
download_button.pack(padx=10, pady=10)
#Download finished
finishLabel = customtkinter.CTkLabel(frame, text="")
finishLabel.pack()
#progress bar
percentage = customtkinter.CTkLabel(frame, text="0%")
percentage.pack_forget()
progressBar = customtkinter.CTkProgressBar(frame, width=400)
progressBar.set(0)
progressBar.pack_forget()
#Run app
app.mainloop()