player.py (1834B)
1 from python_mpv_jsonipc import MPV 2 from subclient import helpers 3 4 5 class Player: 6 def __init__(self, origin): 7 self.mpv = MPV(ipc_socket='/tmp/subclient-mpv-socket', 8 audio_display='no') 9 self.orig = origin 10 11 def play(self, playlist): 12 self.stop() 13 for s in playlist: 14 song_url = self.orig.get_song_stream_url(s) 15 self.mpv.loadfile(song_url, 'append-play') 16 17 def exit(self): 18 self.mpv.terminate() 19 20 def is_paused(self): 21 return self.mpv.pause 22 23 def is_idle(self): 24 return self.mpv.core_idle 25 26 def set_pause(self, state): 27 self.mpv.pause = state 28 29 def stop(self): 30 self.mpv.command('stop') 31 32 def play_prev(self): 33 self.mpv.command('playlist-prev') 34 35 def play_next(self): 36 self.mpv.command('playlist-next') 37 38 def seek(self, duration='+5'): 39 self.mpv.command('seek', duration) 40 41 def get_song_info(self): 42 title = self.mpv.media_title 43 if title is None: 44 return ' ' 45 metadata = self.mpv.metadata 46 artist = ' ' 47 if metadata is not None: 48 artist = self.mpv.metadata.get('album_artist', ' ') 49 return f'{artist} :: {title}' 50 51 def get_list_info(self): 52 playlist_total = self.mpv.playlist_count 53 playlist_pos = self.mpv.playlist_pos_1 54 if (playlist_total is None or playlist_pos is None 55 or playlist_pos == -1): 56 return ' ' 57 return f'{playlist_pos} of {playlist_total}' 58 59 def get_duration_info(self): 60 raw_duration = self.mpv.duration 61 if raw_duration is None: 62 return ' ' 63 duration = helpers.format_duration(raw_duration) 64 time_pos = helpers.format_duration(self.mpv.time_pos) 65 return f'{time_pos} / {duration}'