-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmp3-player.py
More file actions
461 lines (375 loc) · 18.1 KB
/
mp3-player.py
File metadata and controls
461 lines (375 loc) · 18.1 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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
import sys
import random
import re
import os
from PyQt6.QtWidgets import (QApplication, QWidget, QPushButton, QVBoxLayout, QHBoxLayout,
QFileDialog, QSlider, QListWidget, QListWidgetItem, QLabel, QLineEdit,
QProgressBar, QMessageBox, QGroupBox, QInputDialog)
from PyQt6.QtMultimedia import QMediaPlayer, QAudioOutput
from PyQt6.QtCore import Qt, QUrl, pyqtSignal, QThread
import spotipy
from spotipy.oauth2 import SpotifyOAuth
from dotenv import load_dotenv
import yt_dlp
class DownloadWorker(QThread):
progress = pyqtSignal(int, str)
finished = pyqtSignal(str)
error = pyqtSignal(str)
def __init__(self, username, playlist_id, output_folder, spotify_client):
super().__init__()
self.username = username
self.playlist_id = playlist_id
self.output_folder = output_folder
self.songs = []
self.sp = spotify_client
def run(self):
try:
# fetch playlist
playlist_name, song_names = self.fetch_playlist()
# create folder
playlist_folder = os.path.join(self.output_folder, self.sanitize_filename(playlist_name))
if not os.path.exists(playlist_folder):
os.makedirs(playlist_folder)
# download
for i, song in enumerate(song_names, start=1):
self.progress.emit(i, song)
file_path = self.download_song(song, playlist_folder, i, len(song_names))
if file_path:
self.songs.append(file_path)
self.finished.emit(playlist_folder)
except Exception as e:
self.error.emit(str(e))
def fetch_playlist(self):
playlist = self.sp.user_playlist(user=self.username, playlist_id=self.playlist_id)
playlist_name = playlist['name']
results = self.sp.user_playlist_tracks(user=self.username, playlist_id=self.playlist_id)
tracks = results['items']
while results['next']:
results = self.sp.next(results)
tracks.extend(results['items'])
song_names = [item['track']['name'] + ' - ' + item['track']['artists'][0]['name'] for item in tracks]
return playlist_name, song_names
def sanitize_filename(self, name):
return re.sub(r'[<>:"/\\|?*]', '', name)
def download_song(self, song_name, output_folder, current, total):
ydl_opts = {
'format': 'bestaudio/best',
'postprocessors': [{
'key': 'FFmpegExtractAudio',
'preferredcodec': 'mp3',
'preferredquality': '192',
}],
'quiet': True,
'outtmpl': f'{output_folder}/{current:03d}-%(title)s.%(ext)s',
}
with yt_dlp.YoutubeDL(ydl_opts) as ydl:
try:
search_result = ydl.extract_info(f"ytsearch1:{song_name}", download=True)
if 'entries' in search_result:
video_info = search_result['entries'][0]
title = video_info.get('title', 'Unknown')
file_path = f"{output_folder}/{current:03d}-{self.sanitize_filename(title)}.mp3"
return file_path
except Exception as e:
print(f"Failed to download {song_name}: {e}")
return None
class AudioPlayer(QMediaPlayer):
def __init__(self):
super().__init__()
self.audio_output = QAudioOutput()
self.setAudioOutput(self.audio_output)
self.mediaStatusChanged.connect(self.on_media_status_changed)
def set_source(self, file_path):
self.setSource(QUrl.fromLocalFile(file_path))
self.play()
def set_volume(self, volume):
self.audio_output.setVolume(volume / 100.0)
def on_media_status_changed(self, status):
if status == QMediaPlayer.MediaStatus.EndOfMedia:
self.parent().playlist.play_next()
class Playlist(QWidget):
def __init__(self, player):
super().__init__()
self.player = player
self.list_widget = QListWidget()
self.list_widget.itemDoubleClicked.connect(self.play_selected)
layout = QVBoxLayout()
layout.addWidget(self.list_widget)
self.setLayout(layout)
def add_to_playlist(self, file_path):
# extract just the filename without full path
filename = os.path.basename(file_path)
item = QListWidgetItem(filename)
item.setData(Qt.ItemDataRole.UserRole, file_path)
self.list_widget.addItem(item)
def add_files_to_playlist(self, file_paths):
for file_path in file_paths:
self.add_to_playlist(file_path)
# select and play the first item if playlist empty
if self.list_widget.count() == len(file_paths):
self.list_widget.setCurrentRow(0)
first_item = self.list_widget.item(0)
if first_item:
self.play_selected(first_item)
def play_selected(self, item):
file_path = item.data(Qt.ItemDataRole.UserRole)
self.player.set_source(file_path)
def play_next(self):
current_row = self.list_widget.currentRow()
if current_row < self.list_widget.count() - 1:
next_item = self.list_widget.item(current_row + 1)
self.list_widget.setCurrentItem(next_item)
self.play_selected(next_item)
def play_previous(self):
current_row = self.list_widget.currentRow()
if current_row > 0:
previous_item = self.list_widget.item(current_row - 1)
self.list_widget.setCurrentItem(previous_item)
self.play_selected(previous_item)
def shuffle(self):
items = []
for i in range(self.list_widget.count()):
item = self.list_widget.item(i)
items.append((item.text(), item.data(Qt.ItemDataRole.UserRole)))
if items:
random.shuffle(items)
self.list_widget.clear()
for display_text, file_path in items:
item = QListWidgetItem(display_text)
item.setData(Qt.ItemDataRole.UserRole, file_path)
self.list_widget.addItem(item)
self.list_widget.setCurrentRow(0)
if self.list_widget.item(0):
self.play_selected(self.list_widget.item(0))
def clear_playlist(self):
self.list_widget.clear()
class Controls(QWidget):
def __init__(self, player, playlist):
super().__init__()
self.player = player
self.playlist = playlist
self.open_button = QPushButton("Open MP3")
self.open_folder_button = QPushButton("Open Folder")
self.play_button = QPushButton("Play")
self.pause_button = QPushButton("Pause")
self.stop_button = QPushButton("Stop")
self.next_button = QPushButton("Next")
self.previous_button = QPushButton("Previous")
self.shuffle_button = QPushButton("Shuffle")
self.seek_slider = QSlider(Qt.Orientation.Horizontal)
self.volume_slider = QSlider(Qt.Orientation.Horizontal)
self.volume_slider.setRange(0, 100)
self.volume_slider.setValue(50)
self.volume_slider.valueChanged.connect(self.adjust_volume)
self.open_button.clicked.connect(self.open_file)
self.open_folder_button.clicked.connect(self.open_folder)
self.play_button.clicked.connect(self.player.play)
self.pause_button.clicked.connect(self.player.pause)
self.stop_button.clicked.connect(self.player.stop)
self.next_button.clicked.connect(self.playlist.play_next)
self.previous_button.clicked.connect(self.playlist.play_previous)
self.shuffle_button.clicked.connect(self.playlist.shuffle)
self.seek_slider.sliderMoved.connect(self.player.setPosition)
self.player.positionChanged.connect(self.update_position)
self.player.durationChanged.connect(self.update_duration)
# control layout
button_layout = QHBoxLayout()
button_layout.addWidget(self.play_button)
button_layout.addWidget(self.pause_button)
button_layout.addWidget(self.stop_button)
button_layout.addWidget(self.next_button)
button_layout.addWidget(self.previous_button)
button_layout.addWidget(self.shuffle_button)
file_buttons_layout = QHBoxLayout()
file_buttons_layout.addWidget(self.open_button)
file_buttons_layout.addWidget(self.open_folder_button)
layout = QVBoxLayout()
layout.addWidget(self.seek_slider)
layout.addWidget(QLabel("Volume:"))
layout.addWidget(self.volume_slider)
layout.addLayout(button_layout)
layout.addLayout(file_buttons_layout)
self.setLayout(layout)
def open_file(self):
file_paths, _ = QFileDialog.getOpenFileNames(self, "Open MP3 Files", "", "MP3 Files (*.mp3)")
if file_paths:
self.playlist.add_files_to_playlist(file_paths)
def open_folder(self):
folder_path = QFileDialog.getExistingDirectory(self, "Select Folder Containing MP3s")
if folder_path:
# Find all MP3 files in the selected folder
mp3_files = []
for file in os.listdir(folder_path):
if file.lower().endswith('.mp3'):
mp3_files.append(os.path.join(folder_path, file))
if mp3_files:
# Sort files alphabetically
mp3_files.sort()
self.playlist.add_files_to_playlist(mp3_files)
QMessageBox.information(self, "Folder Loaded",
f"Successfully loaded {len(mp3_files)} songs from the folder.")
else:
QMessageBox.warning(self, "No MP3 Files",
"No MP3 files were found in the selected folder.")
def update_position(self, position):
self.seek_slider.setValue(position)
def update_duration(self, duration):
self.seek_slider.setRange(0, duration)
def adjust_volume(self, volume):
self.player.set_volume(volume)
class SpotifyDownloader(QWidget):
download_complete = pyqtSignal(str)
def __init__(self, spotify_client=None):
super().__init__()
self.sp = spotify_client
self.username_label = QLabel("Spotify Username:")
self.username_input = QLineEdit()
self.playlist_label = QLabel("Spotify Playlist URL:")
self.playlist_input = QLineEdit()
self.download_button = QPushButton("Download Playlist")
self.download_button.clicked.connect(self.start_download)
self.progress_bar = QProgressBar()
self.progress_bar.setRange(0, 100)
self.progress_bar.setValue(0)
self.status_label = QLabel("Enter a Spotify playlist URL to download")
layout = QVBoxLayout()
layout.addWidget(self.username_label)
layout.addWidget(self.username_input)
layout.addWidget(self.playlist_label)
layout.addWidget(self.playlist_input)
layout.addWidget(self.download_button)
layout.addWidget(self.progress_bar)
layout.addWidget(self.status_label)
self.setLayout(layout)
def start_download(self):
if not self.sp:
QMessageBox.warning(self, "Error", "Spotify client not initialized!")
return
username = self.username_input.text().strip()
playlist_url = self.playlist_input.text().strip()
if not username or not playlist_url:
QMessageBox.warning(self, "Input Error", "Please enter both username and playlist URL")
return
try:
playlist_id = self.extract_playlist_id(playlist_url)
output_folder = QFileDialog.getExistingDirectory(self, "Select Output Directory")
if output_folder:
self.download_button.setEnabled(False)
self.status_label.setText("Fetching playlist information...")
self.progress_bar.setValue(0)
# Create worker thread
self.download_thread = DownloadWorker(username, playlist_id, output_folder, self.sp)
self.download_thread.progress.connect(self.update_progress)
self.download_thread.finished.connect(self.download_finished)
self.download_thread.error.connect(self.download_error)
self.download_thread.start()
except ValueError as e:
QMessageBox.warning(self, "Invalid URL", str(e))
def extract_playlist_id(self, playlist_url):
match = re.search(r'playlist/([a-zA-Z0-9]+)', playlist_url)
if match:
return match.group(1)
else:
raise ValueError("Invalid Spotify Playlist URL")
def update_progress(self, current, song_name):
try:
# get total from thread
total = len(self.download_thread.sp.user_playlist_tracks(
user=self.download_thread.username,
playlist_id=self.download_thread.playlist_id
)['items'])
percentage = int((current / total) * 100)
self.progress_bar.setValue(percentage)
self.status_label.setText(f"Downloading {current}/{total}: {song_name}")
except Exception as e:
self.status_label.setText(f"Downloading: {song_name}")
# just show indeterminate progress if we cant get the total
self.progress_bar.setRange(0, 0)
def download_finished(self, playlist_folder):
self.download_button.setEnabled(True)
self.status_label.setText(f"Download complete! Files saved to: {playlist_folder}")
self.progress_bar.setValue(100)
self.download_complete.emit(playlist_folder)
def download_error(self, error_message):
self.download_button.setEnabled(True)
self.status_label.setText(f"Error: {error_message}")
QMessageBox.critical(self, "Download Error", error_message)
class SpotifyPlayerApp(QWidget):
def __init__(self):
super().__init__()
self.setWindowTitle("Spotify Playlist Downloader & Player")
self.setGeometry(100, 100, 800, 600)
# initialize Spotify client
self.sp = self.setup_spotify_client()
self.player = AudioPlayer()
self.playlist = Playlist(self.player)
self.player.setParent(self)
self.controls = Controls(self.player, self.playlist)
self.downloader = SpotifyDownloader(self.sp)
self.downloader.download_complete.connect(self.load_downloaded_playlist)
player_group = QGroupBox("MP3 Player")
player_layout = QVBoxLayout()
player_layout.addWidget(self.playlist)
player_layout.addWidget(self.controls)
player_group.setLayout(player_layout)
downloader_group = QGroupBox("Spotify Downloader")
downloader_layout = QVBoxLayout()
downloader_layout.addWidget(self.downloader)
downloader_group.setLayout(downloader_layout)
main_layout = QVBoxLayout()
main_layout.addWidget(downloader_group)
main_layout.addWidget(player_group)
self.setLayout(main_layout)
def setup_spotify_client(self):
load_dotenv()
client_id = os.getenv("CLIENT_ID")
client_secret = os.getenv("CLIENT_SECRET")
redirect_uri = os.getenv("REDIRECT_URI", "http://localhost:8888/callback")
if not client_id or not client_secret:
client_id, ok1 = QInputDialog.getText(self, "Spotify API", "Enter your Spotify Client ID:")
if not ok1 or not client_id:
QMessageBox.warning(self, "Missing Credentials",
"Client ID is required. Get one from https://developer.spotify.com/dashboard/")
return None
client_secret, ok2 = QInputDialog.getText(self, "Spotify API", "Enter your Spotify Client Secret:")
if not ok2 or not client_secret:
QMessageBox.warning(self, "Missing Credentials",
"Client Secret is required. Get one from https://developer.spotify.com/dashboard/")
return None
save = QMessageBox.question(self, "Save Credentials",
"Do you want to save these credentials for future use?",
QMessageBox.StandardButton.Yes | QMessageBox.StandardButton.No)
if save == QMessageBox.StandardButton.Yes:
with open(".env", "w") as f:
f.write(f"CLIENT_ID={client_id}\n")
f.write(f"CLIENT_SECRET={client_secret}\n")
f.write(f"REDIRECT_URI={redirect_uri}\n")
try:
return spotipy.Spotify(auth_manager=SpotifyOAuth(
client_id=client_id,
client_secret=client_secret,
redirect_uri=redirect_uri,
scope="playlist-read-private"
))
except Exception as e:
QMessageBox.critical(self, "Spotify API Error", f"Failed to initialize Spotify client: {str(e)}")
return None
def load_downloaded_playlist(self, folder_path):
self.playlist.clear_playlist()
mp3_files = []
for file in os.listdir(folder_path):
if file.endswith('.mp3'):
mp3_files.append(os.path.join(folder_path, file))
# Add to playlist
if mp3_files:
self.playlist.add_files_to_playlist(mp3_files)
QMessageBox.information(self, "Playlist Loaded",
f"Successfully loaded {len(mp3_files)} songs to the playlist.")
else:
QMessageBox.warning(self, "No MP3 Files",
"No MP3 files were found in the downloaded folder.")
if __name__ == "__main__":
app = QApplication(sys.argv)
player_app = SpotifyPlayerApp()
player_app.show()
sys.exit(app.exec())