Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions beets/util/lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ class Lyrics:

ORIGINAL_PAT = re.compile(r"[^\n]+ / ")
TRANSLATION_PAT = re.compile(r" / [^\n]+")
LINE_PARTS_PAT = re.compile(r"^(\[\d\d:\d\d\.\d\d\]|) *(.*)$")
LRC_TIMESTAMP_PAT = re.compile(r"\[(\d{2}):(\d{2})\.(\d{2})\]")
LINE_PARTS_PAT = re.compile(r"^(\[\d\d:\d\d\.\d{2,3}\]|) *(.*)$")
LRC_TIMESTAMP_PAT = re.compile(r"\[(\d{2}):(\d{2})\.(\d{2,3})\]")

text: str
backend: str | None = None
Expand Down Expand Up @@ -148,8 +148,9 @@ def sylt(self) -> list[tuple[str, int]]:
continue
ts_m = self.LRC_TIMESTAMP_PAT.match(ts)
if ts_m:
m, s, cs = map(int, ts_m.groups())
Comment thread
mlandry marked this conversation as resolved.
Resolved
ms = (m * 60 + s) * 1000 + cs * 10
m, s = map(int, ts_m.groups()[:2])
frac = ts_m.group(3)
ms = (m * 60 + s) * 1000 + int(frac.ljust(3, "0"))
result.append((text, ms))
return result

Expand Down
10 changes: 7 additions & 3 deletions docs/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,13 @@ Unreleased
New features
~~~~~~~~~~~~

..
Bug fixes
~~~~~~~~~
Bug fixes
~~~~~~~~~

- :doc:`plugins/lyrics`: Support 3-decimal millisecond timestamps (e.g.
``[mm:ss.xxx]``) in LRC parsing, fixing an issue where synced lyrics with
millisecond precision were erroneously rejected and fell back to plain lyrics.
:bug:`7001`

..
For plugin developers
Expand Down
9 changes: 9 additions & 0 deletions test/plugins/test_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -639,6 +639,15 @@ def test_null_plain_lyrics_falls_back_to_synced(self, fetch_lyrics):
[
pytest.param([], None, id="handle non-matching lyrics"),
pytest.param([lyrics_match()], SYNCED, id="synced when available"),
pytest.param(
[
lyrics_match(
syncedLyrics="[00:01.234] 3-decimal synced lyrics"
)
],
"[00:01.234] 3-decimal synced lyrics",
id="synced with 3-decimal millisecond timestamp",
),
pytest.param(
[lyrics_match(duration=1)], None, id="none: duration too short"
),
Expand Down
29 changes: 29 additions & 0 deletions test/util/test_lyrics.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,3 +52,32 @@ def test_from_legacy_text(self, is_importable):
"FR" if langdetect_available else None
)
assert not lyrics.instrumental

def test_timestamp_precision_and_sylt(self):
text = textwrap.dedent("""
[00:00.05] Centisecond line
[00:01.50] Two decimals
[00:02.123] Three decimals
[01:00.005] Five milliseconds
""").strip()
lyrics = Lyrics(text)

assert lyrics.synced
assert lyrics.timestamps == [
"[00:00.05]",
"[00:01.50]",
"[00:02.123]",
"[01:00.005]",
]
assert lyrics.text_lines == [
"Centisecond line",
"Two decimals",
"Three decimals",
"Five milliseconds",
]
assert lyrics.sylt == [
("Centisecond line", 50),
("Two decimals", 1500),
("Three decimals", 2123),
("Five milliseconds", 60005),
]
Loading