TestLyric/play_ui.py
2024-11-17 11:18:22 +08:00

75 lines
2.0 KiB
Python

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
from app import MainWindow, QApplication
music = 'credits.mp3'
lyric = 'credits.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 display_lyric(player, lyrics, app):
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]]
app.lyrics.set_lyrics([previous_lrc, lyrics[times[index]]])
sleep(0.01)
def catch():
global player
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
if True:
stopped = False
player.play()
player.current_time = 80
app = QApplication()
window = MainWindow()
window.show()
with open(lyric, encoding='utf-8') as f:
lyrics = parse_lyric(f.read())
Thread(target=display_lyric, args=(player, lyrics, window)).start()
Thread(target=catch).start()
app.exec()
stopped = True
player.stop()