-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
245 lines (196 loc) · 9.23 KB
/
Copy pathmain.py
File metadata and controls
245 lines (196 loc) · 9.23 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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
# Importar os módulos necessários
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLineEdit, QPushButton, QProgressBar, QLabel, QVBoxLayout
from PyQt5.QtCore import Qt, QThread, pyqtSignal
from ytmusicapi import YTMusic
import yt_dlp
import re
import os
import subprocess
from PyQt5.QtWidgets import QTextEdit
import mediafiregrabber
import urllib.request
from PyQt5.QtWidgets import QCheckBox
from PyQt5.QtWidgets import QComboBox
# Criar uma classe para o thread de download
class DownloadThread(QThread):
title_signal = pyqtSignal(str)
artist_signal = pyqtSignal(str)
videoId_signal = pyqtSignal(str)
# Definir um sinal para atualizar a barra de progresso
progress = pyqtSignal(int)
# Definir um sinal para atualizar o status
status = pyqtSignal(str)
def __init__(self, query, quality, open_folder_after_download):
# Inicializar o thread com o nome da música
QThread.__init__(self)
self.query = query
self.quality = quality
self.open_folder_after_download = open_folder_after_download
def run(self):
# Executar o código de download no thread
def __init__(self, query, quality):
QThread.__init__(self)
self.query = query
self.quality = quality
# Baixar ffmpeg se
self.baixar_ffmpeg()
try:
# Inicializar a API do YouTube Music
ytmusic = YTMusic()
# Pesquisar uma música pelo título
results = ytmusic.search(self.query, filter="songs")
# Obter o primeiro resultado da pesquisa
song = results[0]
# Obter o título, o artista e o vídeoId da música
title = song["title"]
artist = song["artists"][0]["name"]
videoId = song["videoId"]
self.title_signal.emit(title)
self.artist_signal.emit(artist)
self.videoId_signal.emit(videoId)
# Criar uma instância do yt-dlp
ydl_opts = {
'ffmpeg_location': r'./ffmpeg.exe',
"format": "bestaudio/best",
"outtmpl": f"./musics\\{title}",
"postprocessors": [{
"key": "FFmpegExtractAudio",
"preferredcodec": "mp3",
"preferredquality": self.quality,
}],
# Definir uma função para atualizar o sinal de progresso
"progress_hooks": [self.update_progress],
}
ydl = yt_dlp.YoutubeDL(ydl_opts)
# Baixar a música em formato mp3
ydl.download([f"https://music.youtube.com/watch?v={videoId}"])
# Abrir a pasta após o download e selecionar o arquivo
# Abrir a pasta após o download e selecionar o arquivo
if self.open_folder_after_download:
file_path = os.path.abspath(f"./musics/{title}.mp3") # Adicione a extensão .mp3 uma vez aqui
print(f"File path: {file_path}")
subprocess.run(f'explorer /select,"{file_path}"', check=True)
except Exception as e:
import traceback
print(traceback.format_exc() + str(e))
def baixar_ffmpeg(self):
# Verifique se o ffmpeg está presente
ffmpeg_path = r'./ffmpeg.exe'
if not os.path.exists(ffmpeg_path):
self.status.emit("FFmpeg, não encontrado na pasta raiz, Baixando o ffmpeg...")
# Se não estiver presente, baixe-o
file_mediafire = mediafiregrabber.downloadlink('https://www.mediafire.com/file/dpovs3t1ae1chu7/ffmpeg.exe/file')
link_direto = file_mediafire
filename = ffmpeg_path
# Crie o diretório se ele não existir
os.makedirs(os.path.dirname(filename), exist_ok=True)
# Função para atualizar a barra de progresso
def reporthook(blocknum, blocksize, totalsize):
readsofar = blocknum * blocksize
if totalsize > 0:
percent = readsofar * 1e2 / totalsize
self.progress.emit(int(percent))
urllib.request.urlretrieve(link_direto, filename, reporthook)
self.status.emit("FFmpeg baixado com sucesso.")
def update_progress(self, d):
# Atualizar o sinal de progresso com o percentual do download
if d["status"] == "downloading":
# Remover os códigos de escape ANSI da string do percentual
p_str = re.sub(r'\x1b\[.*?m', '', d["_percent_str"])
# Remover o símbolo de percentagem e converter para um inteiro
p = int(float(p_str.replace("%", "")))
self.progress.emit(p)
# Criar uma classe para a janela principal
class MainWindow(QWidget):
def __init__(self):
# Inicializar a janela
QWidget.__init__(self)
self.setWindowTitle("Baixar Música")
self.resize(300, 100)
# Criar os widgets da interface gráfica
self.label = QLabel("Digite o nome da música:")
self.input = QLineEdit()
self.button = QPushButton("Baixar")
self.progress = QProgressBar()
# Adicione esta linha para criar a caixa de texto
self.status = QTextEdit()
self.status.setReadOnly(True) # torna a caixa de texto somente leitura
self.status.setVisible(False) # torna a caixa de texto oculta por padrão
# Conectar o botão ao método de baixar a música
self.button.clicked.connect(self.download)
# Criar a caixa de seleção
self.quality_combobox = QComboBox()
self.quality_combobox.addItem("192 (Padrão)")
self.quality_combobox.addItem("128 (Baixa)")
self.quality_combobox.addItem("320 (Alta)")
# Criar um layout vertical para organizar os widgets
self.layout = QVBoxLayout()
self.layout.addWidget(self.label)
self.layout.addWidget(self.input)
self.layout.addWidget(self.button)
self.layout.addWidget(self.progress)
self.layout.addWidget(self.quality_combobox)
# Adicione esta linha para adicionar a caixa de texto ao layout
self.layout.addWidget(self.status)
# Definir o layout da janela
self.setLayout(self.layout)
# Criar a caixa de seleção
self.details_checkbox = QCheckBox("Detalhes")
self.details_checkbox.stateChanged.connect(self.toggle_status)
# Adicionar a caixa de seleção ao layout
self.layout.addWidget(self.details_checkbox)
# Criar a caixa de seleção parar a opção de mostrar na pasta depois que baixar
self.mostrar_pasta_checkbox = QCheckBox("Mostrar na pasta após o download")
# Adicionar a caixa de seleção ao layout
self.layout.addWidget(self.mostrar_pasta_checkbox)
def toggle_status(self, state):
# Mostrar ou ocultar a caixa de texto de status
self.status.setVisible(state == Qt.Checked)
self.adjustSize() # Ajustar o tamanho da janela
def download(self):
# Obter o nome da música do campo de entrada
query = self.input.text()
# Obter a qualidade selecionada
quality = self.quality_combobox.currentText().split(" ")[0]
# Criar um thread de download com o nome da música e a qualidade
self.thread = DownloadThread(query, quality, self.mostrar_pasta_checkbox.isChecked())
self.thread.title_signal.connect(self.update_title)
self.thread.artist_signal.connect(self.update_artist)
self.thread.videoId_signal.connect(self.update_videoId)
self.thread.progress.connect(self.update_progress)
self.thread.status.connect(self.update_status)
# Verificar se o campo de entrada não está vazio
if query:
# Desabilitar o botão e o campo de entrada
self.button.setEnabled(False)
self.input.setEnabled(False)
# Atualizar a caixa de status
self.status.append("Iniciando o download...")
# Iniciar o thread de download
self.thread.start()
def update_title(self, title):
self.status.append(f"Titulo: {title}")
def update_artist(self, artist):
self.status.append(f"Artista: {artist}")
def update_videoId(self, videoId):
self.status.append(f"ID: {videoId}")
def update_progress(self, value):
# Atualizar o valor da barra de progresso
self.progress.setValue(value)
# Se o valor for 100, habilitar o botão e o campo de entrada
if value == 100:
self.button.setEnabled(True)
self.input.setEnabled(True)
self.status.append("Download concluído.")
def update_status(self, message):
# Atualizar a caixa de status
self.status.append(message)
# Criar uma instância da aplicação
app = QApplication(sys.argv)
# Criar uma instância da janela principal
window = MainWindow()
# Mostrar a janela
window.show()
# Executar a aplicação
sys.exit(app.exec_())