forked from Agent-Hellboy/pymusic-player
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
314 lines (263 loc) · 8.63 KB
/
app.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
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
from os import system, name, path, environ
environ['PYGAME_HIDE_SUPPORT_PROMPT'] = "hide"
from mutagen.mp3 import MP3
import pygame
from prompt_toolkit import PromptSession
from prompt_toolkit.key_binding import KeyBindings
from prompt_toolkit import HTML
from prompt_toolkit.completion import WordCompleter
import sys
# Auto completer
completer = WordCompleter(['play',
'exit',
'resume',
'pause',
'stop',
'clear',
'restart',
'length',
'vol',
'setvol',
'progress',
'status'],
ignore_case=True)
# Input session
session = PromptSession(completer=completer)
# Clearing the terminal window depending on platform
def clear():
if name == "nt":
system('cls')
else:
system('clear')
# Start Pygame
pygame.mixer.pre_init(44100, -16, 2, 512)
pygame.mixer.init()
pygame.init()
clear()
# Class for mp3 files using pygame.mixer.music
class MP3Player:
def __init__(self, file):
self.file = file
def play(self):
pygame.mixer.music.load(self.file)
pygame.mixer.music.play()
def stop(self):
pygame.mixer.music.stop()
def unpause(self):
pygame.mixer.music.unpause()
def pause(self):
pygame.mixer.music.pause()
def get_music_length(self):
return round(MP3(self.file).info.length)
def get_pos(self):
return pygame.mixer.music.get_pos()
def restart(self):
pygame.mixer.music.rewind()
def get_volume(self):
return pygame.mixer.music.get_volume()
def set_volume(self, volume):
pygame.mixer.music.set_volume(float(volume))
def queue(self, file):
pygame.mixer.music.queue(file)
# Class for parsing and executing user input
class Terminal:
def __init__(self) -> None:
# MP3 Player
self.player = None
self.help = lambda: 'https://github.com/TheLegendBeacon/pymusic-player/blob/3f38001e40d00e613f57813cf26c0e022a243432/README.md'
self.playing = False
self.paused = False
self.volume = 1.0
# All functions required for operation
self.functions = {
'exit': sys.exit,
'play': self.play,
'resume': self.resume,
'pause': self.pause,
'stop': self.stop,
'restart': self.restart,
'progress': self.progress,
'clear': self.clear,
'length': self.length,
'vol': self.vol,
'setvol': self.set_volume,
'status': self.status,
'help': self.help
}
def clear(self):
clear()
# Status function - Shows essential info
def status(self):
if self.playing:
return f"Playing: {self.player.file}, Paused: {self.paused}\n{self.progress()}"
else:
return "Not playing anything."
# Toolbar
def toolbar_string(self):
return f'''<b>[play]</b> play <b>[stop]</b> stop <b>[pause]</b> pause <b>[exit]</b> exit <b>[resume]</b> resume <b>[clear]</b> clear <b>|</b> volume: {int(self.volume*100)}%'''
# Starts playing the mp3
def play(self, filepath):
if path.isfile(filepath):
if self.playing:
self.stop()
filepath = filepath.replace("\\", "/")
self.player = MP3Player(filepath)
self.player.play()
self.player.set_volume(self.volume)
self.playing = True
self.paused = False
return "Started playing."
else:
return "Error: File Not Found."
# Stops playing the mp3
def stop(self):
if self.playing:
self.player.stop()
self.playing = False
self.paused = False
return "Stopped."
else:
return "Nothing is playing right now."
# Pause the mp3
def pause(self):
if self.playing:
if self.paused:
return "You are already paused."
else:
self.player.pause()
self.paused = True
return "Paused the track."
else:
return "You are not playing anything."
# Resumes playing the mp3
def resume(self):
if self.playing:
if self.paused:
self.player.unpause()
self.paused = False
return "Successfully resumed."
else:
return "You are already playing."
else:
return "Nothing is playing right now."
# Restarts playing the mp3 from the beginning
def restart(self):
if self.playing:
self.player.restart()
return "Restarted the track."
else:
return "Nothing is playing right now."
# returns the length of the mp3
def length(self):
if self.playing:
seclength = self.player.get_music_length()
minlength, seclength = seclength // 60, seclength % 60
hourlength, minlength = minlength // 60, minlength % 60
if hourlength == 0:
return f"{minlength} minutes and {seclength} seconds"
else:
return f"{hourlength} hours, {minlength} minutes and {seclength} seconds"
else:
return "Nothing is playing right now."
# returns the volume of the mp3
def vol(self):
return f"{str(int(self.volume*100))}%"
# Sets the volume of the mp3
def set_volume(self, volume):
volume = volume.replace("%", '')
if int(float(volume)) > 100 and int(float(volume)) > 0:
return r"You need a value between 0% and 100%."
else:
self.volume = int(float(volume)) / 100
pygame.mixer.music.set_volume(self.volume)
return f"Set volume to {int(100*self.volume)}%"
# Returns a progress bar
def progress(self):
if self.playing:
progressBar = ['-' for x in range(50)]
length = self.player.get_music_length()
pos = self.player.get_pos() // 1000
ratio = round(pos / length * 50)
for x in range(ratio):
progressBar[x] = "█"
return f"\n|{''.join(progressBar)}| {pos}/{length} seconds, {length-pos} sec ETA\n"
else:
return "You are not playing anything."
# Parses user input and runs it
def parse(self, inp):
inWords = inp.split()
if not inWords:
return " "
if len(inWords) > 2:
inWords = [inWords[0], " ".join(inWords[1:])]
inWords[0] = inWords[0].lower()
keywords = [
'play',
'exit',
'resume',
'pause',
'stop',
'clear',
'restart',
'length',
'vol',
'setvol',
'progress',
'status',
'help']
if inWords[0] in keywords:
if len(inWords) == 1:
if inWords[0] == 'exit':
sys.exit()
else:
try:
result = self.functions[inWords[0]]()
return result
except BaseException:
return f"Invalid Syntax: {inp}"
else:
try:
result = self.functions[inWords[0]](inWords[1])
return result
except BaseException:
return f"Invalid Syntax: {inp}"
else:
return f"Invalid Syntax: {inp}: Not a valid keyword."
kb = KeyBindings()
terminal = Terminal()
# Short Cuts
@kb.add('c-u')
def _(*args):
if terminal.paused:
terminal.resume()
else:
terminal.pause()
@kb.add('c-x')
def _(*args):
terminal.stop()
@kb.add('c-r')
def _(*args):
terminal.restart()
@kb.add('c-up')
def _(*args):
terminal.set_volume(str(terminal.volume * 100 + 10))
@kb.add('c-down')
def _(*args):
terminal.set_volume(str(terminal.volume * 100 - 10))
# Main function
while True:
try:
inp = session.prompt(
"\n❯ ",
bottom_toolbar=HTML(
terminal.toolbar_string()),
key_bindings=kb)
output = terminal.parse(inp)
output = [output if output is not None else " "][0]
print(output)
except (KeyboardInterrupt, EOFError):
print("\nThanks For Using!")
sys.exit()
except BaseException:
print("There was an error.")
sys.exit()