lyric.py
751 Bytes
import re
_TIMESTAMP_RE = re.compile(r'\[\d{2}:\d{2}\.\d{2,3}\]')
_META_TAG_RE = re.compile(r'\[[a-zA-Z]+:[^\]]*\]')
def ensure_newlines(text: str | None) -> str:
"""确保歌词每行之间有换行符,保留时间戳等原始内容"""
if not text:
return ''
# 将字面量 \\n / \\r 转为真正换行符
text = text.replace('\\n', '\n').replace('\\r', '')
lines = [line.strip() for line in text.splitlines() if line.strip()]
return '\n'.join(lines)
def strip_timestamps(text: str | None) -> str:
if not text:
return ''
text = _META_TAG_RE.sub('', text)
text = _TIMESTAMP_RE.sub('', text)
lines = [line.strip() for line in text.splitlines() if line.strip()]
return '\n'.join(lines)