from just_playback import Playback from time import sleep, time from lyric_parse import parse_lyric from threading import Thread from datetime import datetime from wcwidth import wcswidth music = 'music.mp3' lyric = 'lrc.lrc' player = Playback(music) def time_convert(raw): if isinstance(raw, float): minutes = int(raw // 60) seconds = int(raw % 60) microsec = raw % 1 return datetime.strptime(f'{minutes}:{seconds}.{("%.6f" % microsec)[2:]}', '%M:%S.%f') elif isinstance(raw, str): return datetime.strptime(raw, '%M:%S.%f') else: return datetime.strptime('00', '%S') def print_lyric(player, lyrics): global stopped lyrics['59:59.999999'] = '' index = 1 times = list(lyrics.keys()) previous_lrc = '' while index < len(lyrics) and not stopped: if index < (len(lyrics) - 1) and time_convert(times[index]) < time_convert(player.curr_pos): index += 1 continue if index >= 1 and time_convert(times[index - 1]) > time_convert(player.curr_pos): index -= 1 continue if previous_lrc != lyrics[times[index - 1]]: previous_lrc = lyrics[times[index - 1]] print('\r' + previous_lrc + '.' * ((40 - wcswidth(previous_lrc)) if 40 > len(previous_lrc) else 0), end='') sleep(0.01) if True: stopped = False player.play() player.current_time = 80 with open(lyric, encoding='utf-8') as f: lyrics = parse_lyric(f.read()) Thread(target=print_lyric, args=(player, lyrics)).start() try: while True: #print_lyric(player, lyrics) #print(player.current_time) r = input() delta = r.count('k') * 5 - (len(r) - r.count('k')) * 5 player.seek(player.curr_pos + delta) except KeyboardInterrupt: pass stopped = True player.stop()