mirror of
https://github.com/unshackle-dl/unshackle.git
synced 2026-12-03 06:25:10 +00:00
feat(music): add shared LRC and SRT lyric helpers to core music
This commit is contained in:
@@ -199,9 +199,10 @@ Each subclass adds its own required fields on top of `id_` and `service`:
|
|||||||
read: `album_artist`, `release_type`, `total_tracks`, `total_discs`, `genre`,
|
read: `album_artist`, `release_type`, `total_tracks`, `total_discs`, `genre`,
|
||||||
`explicit`, `isrc`, `upc`, `copyright`, `label`, `lyrics`, `synced_lyrics`,
|
`explicit`, `isrc`, `upc`, `copyright`, `label`, `lyrics`, `synced_lyrics`,
|
||||||
`lyrics_suffix`, and `artwork_url`. Read the class for the current list. Give `lyrics` as
|
`lyrics_suffix`, and `artwork_url`. Read the class for the current list. Give `lyrics` as
|
||||||
plain text: the tagger embeds it as is. With `synced_lyrics` (timed LRC or TTML) and
|
plain text: the tagger embeds it as is. With `synced_lyrics` (timed LRC, SRT or TTML) and
|
||||||
`lyrics_suffix` (`".lrc"`, `".ttml"`), unshackle also saves the timed lyrics beside the
|
`lyrics_suffix` (`".lrc"`, `".srt"`, `".ttml"`), unshackle also saves the timed lyrics beside the
|
||||||
final audio file under that suffix.
|
final audio file under that suffix. To make LRC or SRT text from millisecond times, use
|
||||||
|
`lrc_stamp`, `cues_to_lrc`, `lrc_to_cues` and `cues_to_srt` from `unshackle.core.music`.
|
||||||
You can set these fields in `get_tracks()` too, and a remote client gets them back.
|
You can set these fields in `get_tracks()` too, and a remote client gets them back.
|
||||||
|
|
||||||
For a playlist whose songs keep the tags of their own album, give `position` (a positive integer,
|
For a playlist whose songs keep the tags of their own album, give `position` (a positive integer,
|
||||||
|
|||||||
@@ -0,0 +1,72 @@
|
|||||||
|
"""The shared lyric helpers; each expected value is recorded from the service copies that they replace."""
|
||||||
|
|
||||||
|
import pytest
|
||||||
|
|
||||||
|
from unshackle.core.music import cues_to_lrc, cues_to_srt, lrc_stamp, lrc_to_cues
|
||||||
|
|
||||||
|
LRC = (
|
||||||
|
"[00:05.00]third line\r\n"
|
||||||
|
"[00:01.00] first line \n"
|
||||||
|
"[00:02.50]\n"
|
||||||
|
"not a lyric line\n"
|
||||||
|
"[00:01.80]second line\n"
|
||||||
|
" [00:03.00]indented \n"
|
||||||
|
"[00:04.001]inexact\n"
|
||||||
|
"[00:20]no fraction, gap\n"
|
||||||
|
"[00:30.00]same start\n"
|
||||||
|
"[00:30.00]same start again\n"
|
||||||
|
"[120:00.125]over ninety-nine minutes\n"
|
||||||
|
)
|
||||||
|
CUES = [
|
||||||
|
(1000, 1800, "first line"),
|
||||||
|
(1800, 2500, "second line"),
|
||||||
|
(3000, 4001, "indented"),
|
||||||
|
(4001, 5000, "inexact"),
|
||||||
|
(5000, 8500, "third line"),
|
||||||
|
(20000, 23500, "no fraction, gap"),
|
||||||
|
(30000, 33000, "same start"),
|
||||||
|
(30000, 33500, "same start again"),
|
||||||
|
(7200125, 7203125, "over ninety-nine minutes"),
|
||||||
|
]
|
||||||
|
|
||||||
|
|
||||||
|
@pytest.mark.parametrize(
|
||||||
|
("ms", "stamp"),
|
||||||
|
[
|
||||||
|
(0, "00:00.00"),
|
||||||
|
(-1, "00:00.00"),
|
||||||
|
(-5000, "00:00.00"),
|
||||||
|
(5, "00:00.00"),
|
||||||
|
(15, "00:00.02"),
|
||||||
|
(25, "00:00.02"),
|
||||||
|
(995, "00:01.00"),
|
||||||
|
(59994, "00:59.99"),
|
||||||
|
(59995, "01:00.00"),
|
||||||
|
(59996, "01:00.00"),
|
||||||
|
(60000, "01:00.00"),
|
||||||
|
(3599990, "59:59.99"),
|
||||||
|
(5999994, "99:59.99"),
|
||||||
|
(5999996, "100:00.00"),
|
||||||
|
(123456789, "2057:36.79"),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
def test_lrc_stamp(ms: int, stamp: str) -> None:
|
||||||
|
assert lrc_stamp(ms) == stamp
|
||||||
|
|
||||||
|
|
||||||
|
def test_cues_to_lrc() -> None:
|
||||||
|
cues = [(-20, 0, "before zero"), (0, 3000, "first"), (59996, 62000, "one minute"), (6000000, 6003000, "hundred")]
|
||||||
|
assert cues_to_lrc(cues) == "[00:00.00]before zero\n[00:00.00]first\n[01:00.00]one minute\n[100:00.00]hundred"
|
||||||
|
assert cues_to_lrc([]) == ""
|
||||||
|
|
||||||
|
|
||||||
|
def test_lrc_to_cues() -> None:
|
||||||
|
assert lrc_to_cues(LRC) == CUES
|
||||||
|
assert lrc_to_cues("") == []
|
||||||
|
|
||||||
|
|
||||||
|
def test_cues_to_srt() -> None:
|
||||||
|
assert cues_to_srt(CUES[:2]) == (
|
||||||
|
"1\n00:00:01,000 --> 00:00:01,800\nfirst line\n\n2\n00:00:01,800 --> 00:00:02,500\nsecond line\n\n"
|
||||||
|
)
|
||||||
|
assert cues_to_srt([]) == ""
|
||||||
@@ -8,6 +8,7 @@ from .extract import (
|
|||||||
format_names,
|
format_names,
|
||||||
year_from_value,
|
year_from_value,
|
||||||
)
|
)
|
||||||
|
from .lyrics import cues_to_lrc, cues_to_srt, lrc_stamp, lrc_to_cues
|
||||||
from .renderer import MusicRenderer
|
from .renderer import MusicRenderer
|
||||||
from .tagger import MusicMetadataResult, write_music_metadata
|
from .tagger import MusicMetadataResult, write_music_metadata
|
||||||
|
|
||||||
@@ -16,11 +17,15 @@ __all__ = (
|
|||||||
"MusicRenderer",
|
"MusicRenderer",
|
||||||
"build_music_from_songs",
|
"build_music_from_songs",
|
||||||
"classify_release_kind",
|
"classify_release_kind",
|
||||||
|
"cues_to_lrc",
|
||||||
|
"cues_to_srt",
|
||||||
"duration_seconds",
|
"duration_seconds",
|
||||||
"first_number",
|
"first_number",
|
||||||
"first_text",
|
"first_text",
|
||||||
"format_duration",
|
"format_duration",
|
||||||
"format_names",
|
"format_names",
|
||||||
|
"lrc_stamp",
|
||||||
|
"lrc_to_cues",
|
||||||
"write_music_metadata",
|
"write_music_metadata",
|
||||||
"year_from_value",
|
"year_from_value",
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,46 @@
|
|||||||
|
"""Timed lyrics: LRC stamps, LRC text to cues, and cues to LRC or SRT. A cue is (start_ms, end_ms, text)."""
|
||||||
|
|
||||||
|
import re
|
||||||
|
|
||||||
|
import pysubs2
|
||||||
|
|
||||||
|
LRC_LINE = re.compile(r"^\[(?P<min>\d+):(?P<sec>\d+(?:\.\d+)?)\]\s*(?P<text>.*)$")
|
||||||
|
|
||||||
|
|
||||||
|
def lrc_stamp(ms: int) -> str:
|
||||||
|
"""An LRC ``mm:ss.xx`` time from milliseconds, rounded to centiseconds before the split (59996 is 01:00.00)."""
|
||||||
|
cs = round(max(ms, 0) / 10)
|
||||||
|
return f"{cs // 6000:02d}:{cs // 100 % 60:02d}.{cs % 100:02d}"
|
||||||
|
|
||||||
|
|
||||||
|
def cues_to_lrc(cues: list[tuple[int, int, str]]) -> str:
|
||||||
|
"""LRC text with one stamped line per cue; the end times are not used."""
|
||||||
|
return "\n".join(f"[{lrc_stamp(start)}]{text}" for start, _, text in cues)
|
||||||
|
|
||||||
|
|
||||||
|
def lrc_to_cues(lrc_text: str) -> list[tuple[int, int, str]]:
|
||||||
|
"""Cues from LRC lines, sorted by start: a cue ends at the next later start, at most 3.5 s on.
|
||||||
|
|
||||||
|
The last cue, and a cue that shares its start with the next line, get 3 s. A blank LRC line gives no cue.
|
||||||
|
"""
|
||||||
|
timed = []
|
||||||
|
for line in lrc_text.splitlines():
|
||||||
|
m = LRC_LINE.match(line.strip())
|
||||||
|
if m:
|
||||||
|
start_ms = round((int(m.group("min")) * 60 + float(m.group("sec"))) * 1000)
|
||||||
|
timed.append((start_ms, m.group("text")))
|
||||||
|
timed.sort(key=lambda cue: cue[0])
|
||||||
|
cues = []
|
||||||
|
for i, (start, text) in enumerate(timed):
|
||||||
|
next_start = timed[i + 1][0] if i + 1 < len(timed) else start
|
||||||
|
end = min(start + 3500, next_start) if next_start > start else start + 3000
|
||||||
|
if text:
|
||||||
|
cues.append((start, end, text))
|
||||||
|
return cues
|
||||||
|
|
||||||
|
|
||||||
|
def cues_to_srt(cues: list[tuple[int, int, str]]) -> str:
|
||||||
|
"""SRT text with one subtitle per cue."""
|
||||||
|
subs = pysubs2.SSAFile()
|
||||||
|
subs.events = [pysubs2.SSAEvent(start=start, end=end, text=text) for start, end, text in cues]
|
||||||
|
return subs.to_string("srt")
|
||||||
@@ -128,7 +128,7 @@ class Song(Title):
|
|||||||
|
|
||||||
`position` is the number in a playlist: it replaces `track` in the listing and in `-w`, not in tags or names.
|
`position` is the number in a playlist: it replaces `track` in the listing and in `-w`, not in tags or names.
|
||||||
|
|
||||||
`synced_lyrics` (timed LRC or TTML) is saved beside the audio file with `lyrics_suffix` (".lrc", ".ttml").
|
`synced_lyrics` (timed LRC, SRT or TTML) is saved beside the audio file with `lyrics_suffix` (".lrc", ".srt", ".ttml").
|
||||||
"""
|
"""
|
||||||
super().__init__(id_, service, language, data)
|
super().__init__(id_, service, language, data)
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user