-
Notifications
You must be signed in to change notification settings - Fork 0
/
lrc.py
49 lines (45 loc) · 1.98 KB
/
lrc.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
def parse_lrc(lrc: str):
parsed_lyrics = {}
for i, line in enumerate(lrc.splitlines(), start=0): # Use enumerate to keep track of line numbers
if ']' not in line:
continue # Skip lines without ']'
parts = line.strip().split(']')
for part in parts[:-1]:
time_str = part[1:]
if line.split(']', 1)[1].strip() == '':
continue # Skip lines with empty lyric content
try:
time_parts = time_str.split(':')
minutes = int(time_parts[0])
seconds = float(time_parts[1])
total_seconds = minutes * 60 + seconds
if total_seconds not in parsed_lyrics:
parsed_lyrics[total_seconds] = []
parsed_lyrics[total_seconds].append(i) # Append line number instead of lyric text
except ValueError:
# Ignore lines with incorrect time format
pass
return parsed_lyrics
def time_to_seconds(time_str: str):
components = time_str.split(':')
if len(components) == 1:
return float(components[0])
elif len(components) == 2:
minutes = int(components[0])
seconds = float(components[1])
return minutes * 60 + seconds
elif len(components) == 3:
hours = int(components[0])
minutes = int(components[1])
seconds = float(components[2])
return hours * 3600 + minutes * 60 + seconds
else:
return None
def get_lyric_at_time(lyrics, time_point):
filtered_times = [t for t in lyrics.keys() if
t <= time_point] # Filter out times that are not before or at the given time
if filtered_times: # Check if there are any times left after filtering
closest_time = max(filtered_times) # Choose the closest time that is not ahead of the given time
return lyrics.get(closest_time)
else:
return [0] # Return None if there are no lyrics before or at the given time