lemony-lrc-parser 0.2.1__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- lemony_lrc_parser-0.2.1/.gitignore +217 -0
- lemony_lrc_parser-0.2.1/LICENSE +21 -0
- lemony_lrc_parser-0.2.1/PKG-INFO +229 -0
- lemony_lrc_parser-0.2.1/README.md +200 -0
- lemony_lrc_parser-0.2.1/pyproject.toml +60 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/__init__.py +79 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/exceptions.py +6 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/models.py +184 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/parser.py +291 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/py.typed +0 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/regex.py +159 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/serializer.py +215 -0
- lemony_lrc_parser-0.2.1/src/lemony_lrc_parser/timetag.py +86 -0
- lemony_lrc_parser-0.2.1/tests/test_construct_lrc_offset.py +147 -0
- lemony_lrc_parser-0.2.1/tests/test_init_api.py +113 -0
- lemony_lrc_parser-0.2.1/tests/test_line_parser.py +232 -0
- lemony_lrc_parser-0.2.1/tests/test_models.py +212 -0
- lemony_lrc_parser-0.2.1/tests/test_parser_full.py +196 -0
- lemony_lrc_parser-0.2.1/tests/test_serializer_full.py +188 -0
- lemony_lrc_parser-0.2.1/tests/test_timetag.py +83 -0
- lemony_lrc_parser-0.2.1/uv.lock +724 -0
|
@@ -0,0 +1,217 @@
|
|
|
1
|
+
# Byte-compiled / optimized / DLL files
|
|
2
|
+
__pycache__/
|
|
3
|
+
*.py[codz]
|
|
4
|
+
*$py.class
|
|
5
|
+
|
|
6
|
+
# C extensions
|
|
7
|
+
*.so
|
|
8
|
+
|
|
9
|
+
# Distribution / packaging
|
|
10
|
+
.Python
|
|
11
|
+
build/
|
|
12
|
+
develop-eggs/
|
|
13
|
+
dist/
|
|
14
|
+
downloads/
|
|
15
|
+
eggs/
|
|
16
|
+
.eggs/
|
|
17
|
+
lib/
|
|
18
|
+
lib64/
|
|
19
|
+
parts/
|
|
20
|
+
sdist/
|
|
21
|
+
var/
|
|
22
|
+
wheels/
|
|
23
|
+
share/python-wheels/
|
|
24
|
+
*.egg-info/
|
|
25
|
+
.installed.cfg
|
|
26
|
+
*.egg
|
|
27
|
+
MANIFEST
|
|
28
|
+
|
|
29
|
+
# PyInstaller
|
|
30
|
+
# Usually these files are written by a python script from a template
|
|
31
|
+
# before PyInstaller builds the exe, so as to inject date/other infos into it.
|
|
32
|
+
*.manifest
|
|
33
|
+
*.spec
|
|
34
|
+
|
|
35
|
+
# Installer logs
|
|
36
|
+
pip-log.txt
|
|
37
|
+
pip-delete-this-directory.txt
|
|
38
|
+
|
|
39
|
+
# Unit test / coverage reports
|
|
40
|
+
htmlcov/
|
|
41
|
+
.tox/
|
|
42
|
+
.nox/
|
|
43
|
+
.coverage
|
|
44
|
+
.coverage.*
|
|
45
|
+
.cache
|
|
46
|
+
nosetests.xml
|
|
47
|
+
coverage.xml
|
|
48
|
+
*.cover
|
|
49
|
+
*.py.cover
|
|
50
|
+
.hypothesis/
|
|
51
|
+
.pytest_cache/
|
|
52
|
+
cover/
|
|
53
|
+
|
|
54
|
+
# Translations
|
|
55
|
+
*.mo
|
|
56
|
+
*.pot
|
|
57
|
+
|
|
58
|
+
# Django stuff:
|
|
59
|
+
*.log
|
|
60
|
+
local_settings.py
|
|
61
|
+
db.sqlite3
|
|
62
|
+
db.sqlite3-journal
|
|
63
|
+
|
|
64
|
+
# Flask stuff:
|
|
65
|
+
instance/
|
|
66
|
+
.webassets-cache
|
|
67
|
+
|
|
68
|
+
# Scrapy stuff:
|
|
69
|
+
.scrapy
|
|
70
|
+
|
|
71
|
+
# Sphinx documentation
|
|
72
|
+
docs/_build/
|
|
73
|
+
|
|
74
|
+
# PyBuilder
|
|
75
|
+
.pybuilder/
|
|
76
|
+
target/
|
|
77
|
+
|
|
78
|
+
# Jupyter Notebook
|
|
79
|
+
.ipynb_checkpoints
|
|
80
|
+
|
|
81
|
+
# IPython
|
|
82
|
+
profile_default/
|
|
83
|
+
ipython_config.py
|
|
84
|
+
|
|
85
|
+
# pyenv
|
|
86
|
+
# For a library or package, you might want to ignore these files since the code is
|
|
87
|
+
# intended to run in multiple environments; otherwise, check them in:
|
|
88
|
+
# .python-version
|
|
89
|
+
|
|
90
|
+
# pipenv
|
|
91
|
+
# According to pypa/pipenv#598, it is recommended to include Pipfile.lock in version control.
|
|
92
|
+
# However, in case of collaboration, if having platform-specific dependencies or dependencies
|
|
93
|
+
# having no cross-platform support, pipenv may install dependencies that don't work, or not
|
|
94
|
+
# install all needed dependencies.
|
|
95
|
+
# Pipfile.lock
|
|
96
|
+
|
|
97
|
+
# UV
|
|
98
|
+
# Similar to Pipfile.lock, it is generally recommended to include uv.lock in version control.
|
|
99
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
100
|
+
# commonly ignored for libraries.
|
|
101
|
+
# uv.lock
|
|
102
|
+
|
|
103
|
+
# poetry
|
|
104
|
+
# Similar to Pipfile.lock, it is generally recommended to include poetry.lock in version control.
|
|
105
|
+
# This is especially recommended for binary packages to ensure reproducibility, and is more
|
|
106
|
+
# commonly ignored for libraries.
|
|
107
|
+
# https://python-poetry.org/docs/basic-usage/#commit-your-poetrylock-file-to-version-control
|
|
108
|
+
# poetry.lock
|
|
109
|
+
# poetry.toml
|
|
110
|
+
|
|
111
|
+
# pdm
|
|
112
|
+
# Similar to Pipfile.lock, it is generally recommended to include pdm.lock in version control.
|
|
113
|
+
# pdm recommends including project-wide configuration in pdm.toml, but excluding .pdm-python.
|
|
114
|
+
# https://pdm-project.org/en/latest/usage/project/#working-with-version-control
|
|
115
|
+
# pdm.lock
|
|
116
|
+
# pdm.toml
|
|
117
|
+
.pdm-python
|
|
118
|
+
.pdm-build/
|
|
119
|
+
|
|
120
|
+
# pixi
|
|
121
|
+
# Similar to Pipfile.lock, it is generally recommended to include pixi.lock in version control.
|
|
122
|
+
# pixi.lock
|
|
123
|
+
# Pixi creates a virtual environment in the .pixi directory, just like venv module creates one
|
|
124
|
+
# in the .venv directory. It is recommended not to include this directory in version control.
|
|
125
|
+
.pixi
|
|
126
|
+
|
|
127
|
+
# PEP 582; used by e.g. github.com/David-OConnor/pyflow and github.com/pdm-project/pdm
|
|
128
|
+
__pypackages__/
|
|
129
|
+
|
|
130
|
+
# Celery stuff
|
|
131
|
+
celerybeat-schedule
|
|
132
|
+
celerybeat.pid
|
|
133
|
+
|
|
134
|
+
# Redis
|
|
135
|
+
*.rdb
|
|
136
|
+
*.aof
|
|
137
|
+
*.pid
|
|
138
|
+
|
|
139
|
+
# RabbitMQ
|
|
140
|
+
mnesia/
|
|
141
|
+
rabbitmq/
|
|
142
|
+
rabbitmq-data/
|
|
143
|
+
|
|
144
|
+
# ActiveMQ
|
|
145
|
+
activemq-data/
|
|
146
|
+
|
|
147
|
+
# SageMath parsed files
|
|
148
|
+
*.sage.py
|
|
149
|
+
|
|
150
|
+
# Environments
|
|
151
|
+
.env
|
|
152
|
+
.envrc
|
|
153
|
+
.venv
|
|
154
|
+
env/
|
|
155
|
+
venv/
|
|
156
|
+
ENV/
|
|
157
|
+
env.bak/
|
|
158
|
+
venv.bak/
|
|
159
|
+
|
|
160
|
+
# Spyder project settings
|
|
161
|
+
.spyderproject
|
|
162
|
+
.spyproject
|
|
163
|
+
|
|
164
|
+
# Rope project settings
|
|
165
|
+
.ropeproject
|
|
166
|
+
|
|
167
|
+
# mkdocs documentation
|
|
168
|
+
/site
|
|
169
|
+
|
|
170
|
+
# mypy
|
|
171
|
+
.mypy_cache/
|
|
172
|
+
.dmypy.json
|
|
173
|
+
dmypy.json
|
|
174
|
+
|
|
175
|
+
# Pyre type checker
|
|
176
|
+
.pyre/
|
|
177
|
+
|
|
178
|
+
# pytype static type analyzer
|
|
179
|
+
.pytype/
|
|
180
|
+
|
|
181
|
+
# Cython debug symbols
|
|
182
|
+
cython_debug/
|
|
183
|
+
|
|
184
|
+
# PyCharm
|
|
185
|
+
# JetBrains specific template is maintained in a separate JetBrains.gitignore that can
|
|
186
|
+
# be found at https://github.com/github/gitignore/blob/main/Global/JetBrains.gitignore
|
|
187
|
+
# and can be added to the global gitignore or merged into this file. For a more nuclear
|
|
188
|
+
# option (not recommended) you can uncomment the following to ignore the entire idea folder.
|
|
189
|
+
# .idea/
|
|
190
|
+
|
|
191
|
+
# Abstra
|
|
192
|
+
# Abstra is an AI-powered process automation framework.
|
|
193
|
+
# Ignore directories containing user credentials, local state, and settings.
|
|
194
|
+
# Learn more at https://abstra.io/docs
|
|
195
|
+
.abstra/
|
|
196
|
+
|
|
197
|
+
# Visual Studio Code
|
|
198
|
+
# Visual Studio Code specific template is maintained in a separate VisualStudioCode.gitignore
|
|
199
|
+
# that can be found at https://github.com/github/gitignore/blob/main/Global/VisualStudioCode.gitignore
|
|
200
|
+
# and can be added to the global gitignore or merged into this file. However, if you prefer,
|
|
201
|
+
# you could uncomment the following to ignore the entire vscode folder
|
|
202
|
+
.vscode/
|
|
203
|
+
|
|
204
|
+
# Ruff stuff:
|
|
205
|
+
.ruff_cache/
|
|
206
|
+
|
|
207
|
+
# PyPI configuration file
|
|
208
|
+
.pypirc
|
|
209
|
+
|
|
210
|
+
# Marimo
|
|
211
|
+
marimo/_static/
|
|
212
|
+
marimo/_lsp/
|
|
213
|
+
__marimo__/
|
|
214
|
+
|
|
215
|
+
# Streamlit
|
|
216
|
+
.streamlit/secrets.toml
|
|
217
|
+
.python-version
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 LemonekoUwU
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: lemony-lrc-parser
|
|
3
|
+
Version: 0.2.1
|
|
4
|
+
Summary: A simple LRC parser for Python.
|
|
5
|
+
Project-URL: Homepage, https://github.com/NingmengLemon/lemony-lrc-parser
|
|
6
|
+
Project-URL: Repository, https://github.com/NingmengLemon/lemony-lrc-parser.git
|
|
7
|
+
Project-URL: Issues, https://github.com/NingmengLemon/lemony-lrc-parser/issues
|
|
8
|
+
Author-email: LemonekoUwU <lemoneko233uwu@gmail.com>
|
|
9
|
+
License-Expression: MIT
|
|
10
|
+
License-File: LICENSE
|
|
11
|
+
Keywords: Enhanced LRC,LRC,SPL,lyrics,parser,serializer
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
15
|
+
Classifier: Operating System :: OS Independent
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
22
|
+
Classifier: Programming Language :: Python :: 3.14
|
|
23
|
+
Classifier: Programming Language :: Python :: 3.15
|
|
24
|
+
Classifier: Topic :: Multimedia :: Sound/Audio
|
|
25
|
+
Classifier: Topic :: Software Development :: Libraries :: Python Modules
|
|
26
|
+
Classifier: Typing :: Typed
|
|
27
|
+
Requires-Python: <3.16,>=3.9
|
|
28
|
+
Description-Content-Type: text/markdown
|
|
29
|
+
|
|
30
|
+
# Lemony LRC Parser
|
|
31
|
+
|
|
32
|
+
[](https://www.python.org/)
|
|
33
|
+
[](https://opensource.org/licenses/MIT)
|
|
34
|
+
[](https://pypi.org/project/lemony-lrc-parser/)
|
|
35
|
+
|
|
36
|
+
柠檬味的 Python LRC 歌词解析器.
|
|
37
|
+
|
|
38
|
+
Lemon-flavored LRC Parser for Python.
|
|
39
|
+
|
|
40
|
+
## Features
|
|
41
|
+
|
|
42
|
+
- 解析标准 LRC 歌词文件
|
|
43
|
+
- 支持 EnhancedLRC / SPL 的逐字歌词标签
|
|
44
|
+
- 支持 metadata 标签
|
|
45
|
+
- 支持折叠时间标签
|
|
46
|
+
- 支持参照行
|
|
47
|
+
- 支持歌词合并
|
|
48
|
+
- 完整的类型注解
|
|
49
|
+
|
|
50
|
+
## Installation
|
|
51
|
+
|
|
52
|
+
推荐使用 [uv](https://docs.astral.sh/uv/).
|
|
53
|
+
|
|
54
|
+
It's recommend to use [uv](https://docs.astral.sh/uv/).
|
|
55
|
+
|
|
56
|
+
```bash
|
|
57
|
+
uv add lemony-lrc-parser
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
用 pip 也行.
|
|
61
|
+
|
|
62
|
+
It's okay to use pip.
|
|
63
|
+
|
|
64
|
+
```bash
|
|
65
|
+
pip install lemony-lrc-parser
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
## Usage
|
|
69
|
+
|
|
70
|
+
### Quick Start
|
|
71
|
+
|
|
72
|
+
```python
|
|
73
|
+
import lemony_lrc_parser as llp
|
|
74
|
+
|
|
75
|
+
lrc_text = """[ti: Never Gonna Give You Up]
|
|
76
|
+
[ar: Rick Astley]
|
|
77
|
+
|
|
78
|
+
[00:18.684]We're no strangers to love
|
|
79
|
+
[00:18.684]我们都是情场老手
|
|
80
|
+
|
|
81
|
+
[00:22.657]You know the rules and so do I
|
|
82
|
+
[00:22.657]你和我都知道爱情的规则
|
|
83
|
+
|
|
84
|
+
[00:27.070]A full commitment's what I'm thinking of
|
|
85
|
+
[00:27.070]我在想的正是一份实打实的承诺
|
|
86
|
+
|
|
87
|
+
[00:31.459]You wouldn't get this from any other guy
|
|
88
|
+
[00:31.459]你从其他人那里得不到的
|
|
89
|
+
"""
|
|
90
|
+
|
|
91
|
+
# 解析
|
|
92
|
+
lyrics = llp.loads(lrc_text)
|
|
93
|
+
|
|
94
|
+
# 访问 metadata
|
|
95
|
+
print(lyrics.metadata["ti"]) # "Never Gonna Give You Up"
|
|
96
|
+
print(lyrics.metadata["ar"]) # "Rick Astley"
|
|
97
|
+
|
|
98
|
+
# 遍历歌词行
|
|
99
|
+
for line in lyrics:
|
|
100
|
+
print(f"{line.start}ms: {line.text}")
|
|
101
|
+
|
|
102
|
+
# 序列化回 LRC 文本
|
|
103
|
+
output = llp.dumps(lyrics)
|
|
104
|
+
```
|
|
105
|
+
|
|
106
|
+
### OOP Interface
|
|
107
|
+
|
|
108
|
+
`Lyrics` 类提供面向对象的解析和序列化入口:
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
from lemony_lrc_parser import Lyrics
|
|
112
|
+
|
|
113
|
+
lyrics = Lyrics.loads(lrc_text)
|
|
114
|
+
|
|
115
|
+
# Lyrics 同时是序列容器
|
|
116
|
+
print(len(lyrics)) # 行数
|
|
117
|
+
print(lyrics[0].text) # 第一行文本
|
|
118
|
+
print(lyrics[-1].text)
|
|
119
|
+
|
|
120
|
+
# 切片访问
|
|
121
|
+
first_three = lyrics[0:3]
|
|
122
|
+
|
|
123
|
+
# 序列化
|
|
124
|
+
lrc_output = lyrics.dumps()
|
|
125
|
+
|
|
126
|
+
# __str__ 等价于 dumps()
|
|
127
|
+
print(lyrics)
|
|
128
|
+
```
|
|
129
|
+
|
|
130
|
+
### Word-level Lyrics
|
|
131
|
+
|
|
132
|
+
解析逐字 (Enhanced LRC / SPL) 歌词:
|
|
133
|
+
|
|
134
|
+
```python
|
|
135
|
+
lrc_text = "[00:01.000]<00:01.000>Never <00:01.500>gonna <00:02.000>give <00:02.500>you <00:03.000>up[00:03.500]"
|
|
136
|
+
|
|
137
|
+
lyrics = llp.loads(lrc_text)
|
|
138
|
+
line = lyrics[0]
|
|
139
|
+
|
|
140
|
+
for word in line.content:
|
|
141
|
+
print(f" [{word.start} -> {word.end}] {word.content!r}")
|
|
142
|
+
# [1000 -> 1500] 'Never '
|
|
143
|
+
# [1500 -> 2000] 'gonna '
|
|
144
|
+
# [2000 -> 2500] 'give '
|
|
145
|
+
# [2500 -> 3000] 'you '
|
|
146
|
+
# [3000 -> None] 'up'
|
|
147
|
+
|
|
148
|
+
# 行级时间: line.start=1000, line.end=3500
|
|
149
|
+
```
|
|
150
|
+
|
|
151
|
+
### Reference Lines (Translation / Transliteration)
|
|
152
|
+
|
|
153
|
+
LRC 文件中, 紧跟在带时间标签行后面的无标签行, 或与主行的时间戳相同的行, 会被解析为参考行, 常用于存放翻译或音译:
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
lrc_text = """[00:01.000]Hello
|
|
157
|
+
你好
|
|
158
|
+
[00:02.000]World
|
|
159
|
+
[00:02.000]世界
|
|
160
|
+
"""
|
|
161
|
+
|
|
162
|
+
lyrics = llp.loads(lrc_text)
|
|
163
|
+
|
|
164
|
+
line = lyrics[0]
|
|
165
|
+
print(line.text) # "Hello"
|
|
166
|
+
print(line.reference_lines[0][0].content) # "你好"
|
|
167
|
+
```
|
|
168
|
+
|
|
169
|
+
### Combining Lyrics
|
|
170
|
+
|
|
171
|
+
将两份歌词 (如原文和翻译) 按时间标签合并:
|
|
172
|
+
|
|
173
|
+
```python
|
|
174
|
+
main = llp.loads("[00:01.000]Hello\n[00:02.000]World\n")
|
|
175
|
+
translation = llp.loads("[00:01.000]你好\n[00:02.000]世界\n")
|
|
176
|
+
|
|
177
|
+
# combine 方法: 翻译行挂到同时间点的 reference_lines 中
|
|
178
|
+
combined = main.combine(translation)
|
|
179
|
+
|
|
180
|
+
# 也可以用 + 运算符
|
|
181
|
+
combined = main + translation
|
|
182
|
+
|
|
183
|
+
for line in combined:
|
|
184
|
+
print(line.text) # 主歌词
|
|
185
|
+
for ref in line.reference_lines:
|
|
186
|
+
ref_text = "".join(w.content for w in ref)
|
|
187
|
+
print(f" -> {ref_text}") # 参考行
|
|
188
|
+
|
|
189
|
+
# other_as_refline_only=False 时, 翻译中找不到对应时间点的行会作为新行保留
|
|
190
|
+
combined = main.combine(translation, other_as_refline_only=False)
|
|
191
|
+
```
|
|
192
|
+
|
|
193
|
+
### Implicit Line End
|
|
194
|
+
|
|
195
|
+
当歌词行没有显式结束时间时, 可以自动用下一行的开始时间填充:
|
|
196
|
+
|
|
197
|
+
```python
|
|
198
|
+
lyrics = llp.loads(lrc_text, fill_implicit_line_end=True)
|
|
199
|
+
|
|
200
|
+
# lyrics[0].end == lyrics[1].start
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
### Serialization Options
|
|
204
|
+
|
|
205
|
+
`dumps` 支持以下选项:
|
|
206
|
+
|
|
207
|
+
```python
|
|
208
|
+
output = lyrics.dumps(
|
|
209
|
+
with_metadata=True, # 是否输出 metadata 段
|
|
210
|
+
use_bracket_for_byword_tag=False, # 逐字标签使用 [...] 还是 <...> (默认)
|
|
211
|
+
apply_offset_from_metadata=False, # 是否读取并应用 metadata 中的 offset
|
|
212
|
+
)
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
`apply_offset_from_metadata` 的行为: 正 offset 会让歌词显示提前 (`display_time = tag_time - offset`) . 如果应用 offset 会导致时间戳变负, 则只应用安全的部分, 剩余写回 `metadata["offset"]`.
|
|
216
|
+
|
|
217
|
+
## References
|
|
218
|
+
|
|
219
|
+
[LRC Wikipedia](https://en.wikipedia.org/wiki/LRC_(file_format))
|
|
220
|
+
|
|
221
|
+
[SPL Specification](https://moriafly.com/standards/spl.html)
|
|
222
|
+
|
|
223
|
+
## UwU?
|
|
224
|
+
|
|
225
|
+
UwU!
|
|
226
|
+
|
|
227
|
+
## License
|
|
228
|
+
|
|
229
|
+
MIT License
|
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
# Lemony LRC Parser
|
|
2
|
+
|
|
3
|
+
[](https://www.python.org/)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
[](https://pypi.org/project/lemony-lrc-parser/)
|
|
6
|
+
|
|
7
|
+
柠檬味的 Python LRC 歌词解析器.
|
|
8
|
+
|
|
9
|
+
Lemon-flavored LRC Parser for Python.
|
|
10
|
+
|
|
11
|
+
## Features
|
|
12
|
+
|
|
13
|
+
- 解析标准 LRC 歌词文件
|
|
14
|
+
- 支持 EnhancedLRC / SPL 的逐字歌词标签
|
|
15
|
+
- 支持 metadata 标签
|
|
16
|
+
- 支持折叠时间标签
|
|
17
|
+
- 支持参照行
|
|
18
|
+
- 支持歌词合并
|
|
19
|
+
- 完整的类型注解
|
|
20
|
+
|
|
21
|
+
## Installation
|
|
22
|
+
|
|
23
|
+
推荐使用 [uv](https://docs.astral.sh/uv/).
|
|
24
|
+
|
|
25
|
+
It's recommend to use [uv](https://docs.astral.sh/uv/).
|
|
26
|
+
|
|
27
|
+
```bash
|
|
28
|
+
uv add lemony-lrc-parser
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
用 pip 也行.
|
|
32
|
+
|
|
33
|
+
It's okay to use pip.
|
|
34
|
+
|
|
35
|
+
```bash
|
|
36
|
+
pip install lemony-lrc-parser
|
|
37
|
+
```
|
|
38
|
+
|
|
39
|
+
## Usage
|
|
40
|
+
|
|
41
|
+
### Quick Start
|
|
42
|
+
|
|
43
|
+
```python
|
|
44
|
+
import lemony_lrc_parser as llp
|
|
45
|
+
|
|
46
|
+
lrc_text = """[ti: Never Gonna Give You Up]
|
|
47
|
+
[ar: Rick Astley]
|
|
48
|
+
|
|
49
|
+
[00:18.684]We're no strangers to love
|
|
50
|
+
[00:18.684]我们都是情场老手
|
|
51
|
+
|
|
52
|
+
[00:22.657]You know the rules and so do I
|
|
53
|
+
[00:22.657]你和我都知道爱情的规则
|
|
54
|
+
|
|
55
|
+
[00:27.070]A full commitment's what I'm thinking of
|
|
56
|
+
[00:27.070]我在想的正是一份实打实的承诺
|
|
57
|
+
|
|
58
|
+
[00:31.459]You wouldn't get this from any other guy
|
|
59
|
+
[00:31.459]你从其他人那里得不到的
|
|
60
|
+
"""
|
|
61
|
+
|
|
62
|
+
# 解析
|
|
63
|
+
lyrics = llp.loads(lrc_text)
|
|
64
|
+
|
|
65
|
+
# 访问 metadata
|
|
66
|
+
print(lyrics.metadata["ti"]) # "Never Gonna Give You Up"
|
|
67
|
+
print(lyrics.metadata["ar"]) # "Rick Astley"
|
|
68
|
+
|
|
69
|
+
# 遍历歌词行
|
|
70
|
+
for line in lyrics:
|
|
71
|
+
print(f"{line.start}ms: {line.text}")
|
|
72
|
+
|
|
73
|
+
# 序列化回 LRC 文本
|
|
74
|
+
output = llp.dumps(lyrics)
|
|
75
|
+
```
|
|
76
|
+
|
|
77
|
+
### OOP Interface
|
|
78
|
+
|
|
79
|
+
`Lyrics` 类提供面向对象的解析和序列化入口:
|
|
80
|
+
|
|
81
|
+
```python
|
|
82
|
+
from lemony_lrc_parser import Lyrics
|
|
83
|
+
|
|
84
|
+
lyrics = Lyrics.loads(lrc_text)
|
|
85
|
+
|
|
86
|
+
# Lyrics 同时是序列容器
|
|
87
|
+
print(len(lyrics)) # 行数
|
|
88
|
+
print(lyrics[0].text) # 第一行文本
|
|
89
|
+
print(lyrics[-1].text)
|
|
90
|
+
|
|
91
|
+
# 切片访问
|
|
92
|
+
first_three = lyrics[0:3]
|
|
93
|
+
|
|
94
|
+
# 序列化
|
|
95
|
+
lrc_output = lyrics.dumps()
|
|
96
|
+
|
|
97
|
+
# __str__ 等价于 dumps()
|
|
98
|
+
print(lyrics)
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
### Word-level Lyrics
|
|
102
|
+
|
|
103
|
+
解析逐字 (Enhanced LRC / SPL) 歌词:
|
|
104
|
+
|
|
105
|
+
```python
|
|
106
|
+
lrc_text = "[00:01.000]<00:01.000>Never <00:01.500>gonna <00:02.000>give <00:02.500>you <00:03.000>up[00:03.500]"
|
|
107
|
+
|
|
108
|
+
lyrics = llp.loads(lrc_text)
|
|
109
|
+
line = lyrics[0]
|
|
110
|
+
|
|
111
|
+
for word in line.content:
|
|
112
|
+
print(f" [{word.start} -> {word.end}] {word.content!r}")
|
|
113
|
+
# [1000 -> 1500] 'Never '
|
|
114
|
+
# [1500 -> 2000] 'gonna '
|
|
115
|
+
# [2000 -> 2500] 'give '
|
|
116
|
+
# [2500 -> 3000] 'you '
|
|
117
|
+
# [3000 -> None] 'up'
|
|
118
|
+
|
|
119
|
+
# 行级时间: line.start=1000, line.end=3500
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
### Reference Lines (Translation / Transliteration)
|
|
123
|
+
|
|
124
|
+
LRC 文件中, 紧跟在带时间标签行后面的无标签行, 或与主行的时间戳相同的行, 会被解析为参考行, 常用于存放翻译或音译:
|
|
125
|
+
|
|
126
|
+
```python
|
|
127
|
+
lrc_text = """[00:01.000]Hello
|
|
128
|
+
你好
|
|
129
|
+
[00:02.000]World
|
|
130
|
+
[00:02.000]世界
|
|
131
|
+
"""
|
|
132
|
+
|
|
133
|
+
lyrics = llp.loads(lrc_text)
|
|
134
|
+
|
|
135
|
+
line = lyrics[0]
|
|
136
|
+
print(line.text) # "Hello"
|
|
137
|
+
print(line.reference_lines[0][0].content) # "你好"
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Combining Lyrics
|
|
141
|
+
|
|
142
|
+
将两份歌词 (如原文和翻译) 按时间标签合并:
|
|
143
|
+
|
|
144
|
+
```python
|
|
145
|
+
main = llp.loads("[00:01.000]Hello\n[00:02.000]World\n")
|
|
146
|
+
translation = llp.loads("[00:01.000]你好\n[00:02.000]世界\n")
|
|
147
|
+
|
|
148
|
+
# combine 方法: 翻译行挂到同时间点的 reference_lines 中
|
|
149
|
+
combined = main.combine(translation)
|
|
150
|
+
|
|
151
|
+
# 也可以用 + 运算符
|
|
152
|
+
combined = main + translation
|
|
153
|
+
|
|
154
|
+
for line in combined:
|
|
155
|
+
print(line.text) # 主歌词
|
|
156
|
+
for ref in line.reference_lines:
|
|
157
|
+
ref_text = "".join(w.content for w in ref)
|
|
158
|
+
print(f" -> {ref_text}") # 参考行
|
|
159
|
+
|
|
160
|
+
# other_as_refline_only=False 时, 翻译中找不到对应时间点的行会作为新行保留
|
|
161
|
+
combined = main.combine(translation, other_as_refline_only=False)
|
|
162
|
+
```
|
|
163
|
+
|
|
164
|
+
### Implicit Line End
|
|
165
|
+
|
|
166
|
+
当歌词行没有显式结束时间时, 可以自动用下一行的开始时间填充:
|
|
167
|
+
|
|
168
|
+
```python
|
|
169
|
+
lyrics = llp.loads(lrc_text, fill_implicit_line_end=True)
|
|
170
|
+
|
|
171
|
+
# lyrics[0].end == lyrics[1].start
|
|
172
|
+
```
|
|
173
|
+
|
|
174
|
+
### Serialization Options
|
|
175
|
+
|
|
176
|
+
`dumps` 支持以下选项:
|
|
177
|
+
|
|
178
|
+
```python
|
|
179
|
+
output = lyrics.dumps(
|
|
180
|
+
with_metadata=True, # 是否输出 metadata 段
|
|
181
|
+
use_bracket_for_byword_tag=False, # 逐字标签使用 [...] 还是 <...> (默认)
|
|
182
|
+
apply_offset_from_metadata=False, # 是否读取并应用 metadata 中的 offset
|
|
183
|
+
)
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
`apply_offset_from_metadata` 的行为: 正 offset 会让歌词显示提前 (`display_time = tag_time - offset`) . 如果应用 offset 会导致时间戳变负, 则只应用安全的部分, 剩余写回 `metadata["offset"]`.
|
|
187
|
+
|
|
188
|
+
## References
|
|
189
|
+
|
|
190
|
+
[LRC Wikipedia](https://en.wikipedia.org/wiki/LRC_(file_format))
|
|
191
|
+
|
|
192
|
+
[SPL Specification](https://moriafly.com/standards/spl.html)
|
|
193
|
+
|
|
194
|
+
## UwU?
|
|
195
|
+
|
|
196
|
+
UwU!
|
|
197
|
+
|
|
198
|
+
## License
|
|
199
|
+
|
|
200
|
+
MIT License
|