Skip to content

Commit

Permalink
remove sync tags from lyrics when saving them in uslt tag
Browse files Browse the repository at this point in the history
  • Loading branch information
xnetcat committed Jan 5, 2024
1 parent 62b91dc commit f47d77c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
17 changes: 16 additions & 1 deletion spotdl/utils/lrc.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"""

import logging
import re
from pathlib import Path

from syncedlyrics import search as syncedlyrics_search
Expand All @@ -12,7 +13,7 @@

logger = logging.getLogger(__name__)

__all__ = ["generate_lrc"]
__all__ = ["generate_lrc", "remomve_lrc"]


def generate_lrc(song: Song, output_file: Path):
Expand All @@ -37,3 +38,17 @@ def generate_lrc(song: Song, output_file: Path):
logger.debug("Saved lrc file for %s", song.display_name)
else:
logger.debug("No lrc file found for %s", song.display_name)


def remomve_lrc(lyrics: str) -> str:
"""
Removes lrc tags from lyrics
### Arguments
- lyrics: Lyrics string
### Returns
- Lyrics string without lrc tags
"""

return re.sub(r"\[.*?\]", "", lyrics)
7 changes: 5 additions & 2 deletions spotdl/utils/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
from spotdl.types.song import Song
from spotdl.utils.config import GlobalConfig
from spotdl.utils.formatter import to_ms
from spotdl.utils.lrc import remomve_lrc

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -345,6 +346,7 @@ def embed_lyrics(audio_file, song: Song, encoding: str):
else:
# Lyrics are in lrc format
# Embed them as SYLT id3 tag
clean_lyrics = remomve_lrc(lyrics)
if encoding == "mp3":
lrc_data = []
for line in lyrics.splitlines():
Expand All @@ -364,7 +366,7 @@ def embed_lyrics(audio_file, song: Song, encoding: str):
time = to_ms(min=minute, sec=sec, ms=millisecond)
lrc_data.append((text, time))

audio_file.add(USLT(encoding=3, text=song.lyrics))
audio_file.add(USLT(encoding=3, text=clean_lyrics))
audio_file.add(SYLT(encoding=3, text=lrc_data, format=2, type=1))
else:
audio_file[tag_preset["lyrics"]] = song.lyrics
Expand Down Expand Up @@ -605,6 +607,7 @@ def embed_wav_file(output_file: Path, song: Song):
audio.tags.add(USLT(encoding=Encoding.UTF8, text=song.lyrics)) # type: ignore
else:
lrc_data = []
clean_lyrics = remomve_lrc(song.lyrics)
for line in song.lyrics.splitlines():
time_tag = line.split("]", 1)[0] + "]"
text = line.replace(time_tag, "")
Expand All @@ -622,7 +625,7 @@ def embed_wav_file(output_file: Path, song: Song):
time = to_ms(min=minute, sec=sec, ms=millisecond)
lrc_data.append((text, time))

audio.tags.add(USLT(encoding=3, text=song.lyrics)) # type: ignore
audio.tags.add(USLT(encoding=3, text=clean_lyrics)) # type: ignore
audio.tags.add(SYLT(encoding=3, text=lrc_data, format=2, type=1)) # type: ignore

audio.save()

0 comments on commit f47d77c

Please sign in to comment.