import pylrc
import sys
import time
import vlc
import pathlib
import signal
import argparse
import tkinter
# Check that the user has specified the .lrc file
if (len(sys.argv) != 2):
exit(0)
parser = argparse.ArgumentParser()
parser.add_argument('input_lrc', help='')
args = parser.parse_args()
# Parse the .lrc file using pylrc
fp = open(args.input_lrc,'r')
lrc_string = ''.join(fp.readlines())
fp.close()
window = tkinter.Tk()
window.title("Scores")
window.geometry("800x1000")
# Creating our text widget.
sample_text = tkinter.Entry(window)
sample_text.pack()
subs = pylrc.parse(lrc_string)
# Generate the .mp3 filename
# an alternative is https://stackoverflow.com/questions/678236/how-to-get-the-filename-without-the-extension-from-a-path-in-python
# filename = pathlib.PurePosixPath(sys.argv[1]).stem
filename = args.input_lrc.split('\\')[-1].split('.')[0]
mp3_file = filename + '.mp3'
def SongFinished(event):
global song_has_finished
print("Event reports - finished")
song_has_finished = True
# Show the cursor
sys.stdout.write("\033[?25h")
song_has_finished = False
# Prepare VLC
instance = vlc.Instance()
player = instance.media_player_new()
media = instance.media_new_path(mp3_file) #Your audio file here
player.set_media(media)
events = player.event_manager()
events.event_attach(vlc.EventType.MediaPlayerEndReached, SongFinished)
# handle ctrl-c
def sigint_handler(signum, frame):
player.stop()
# Show cursor
sys.stdout.write("\033[?25h")
exit(0)
signal.signal(signal.SIGINT, sigint_handler)
# Start playing the song
print('Playing "' + subs.title + '" by "' + subs.artist + '"')
player.play()
# Hide the cursor
sys.stdout.write("\033[?25l")
line = 0
num_lines = len(subs)
line_printed = False
# wait for the song to finish
while song_has_finished == False:
sec = player.get_time() / 1000
# should we show the next lyrics?
if line+1 == num_lines or sec < subs[line+1].time:
# make sure that we only show the lyric once
if line_printed == False:
print("\r" + subs[line].text.rstrip() + " " * (60 - len(subs[line].text)), end='', flush=True)
sample_text.insert(0,"\r" + subs[line].text.rstrip() + " " * (60 - len(subs[line].text)))
# Creating the function to set the text
# with the help of button
line_printed = True
else:
line += 1
line_printed = False
# try to reduce the CPU usage a bit...
time.sleep(0.1)
window.mainloop()
So as shown above I have created an textbox to show the lyrics. The timed lyric have been created and saved as lrc. It works when the tkinter interface is not in use and shows the output as needed in cmd.
I have shifted the position of the mainloop and the windows to get different output. The output I need is the printing of the timed lyric according to time in the tkinter interface without no user intervention. (Press of a button etc)