音乐播放器"QuodLibet“包含一个插件,可以将歌词与当前播放的歌曲同步。它通过解析.lrc文件中的数据来实现这一点。然而,这些数据也可以存储在文件元数据中,我想修改它来说明这一点。
我在pc上做了一些调查,发现了包含插件的.py文件。我看了一下代码,希望它会很简单,但我从来没有用python编程,有一些代码我不懂。它看起来并不复杂,所以我在想你们能不能破译它。
def _build_data(self):
self.text_buffer.set_text("")
if app.player.song is not None:
# check in same location as track
track_name = app.player.song.get("~filename")
new_lrc = os.path.splitext(track_name)[0] + ".lrc"
print_d("Checking for lyrics file %s" % new_lrc)
if self._current_lrc != new_lrc:
self._lines = []
if os.path.exists(new_lrc):
print_d("Found lyrics file: %s" % new_lrc)
self._parse_lrc_file(new_lrc)
self._current_lrc = new_lrc它似乎将数据存储在new_lrc中,所以我尝试更改该行,尝试从标记中获取数据:
new_lrc = os.path.splitext(track_name)[0] + ".lrc"更改为:
new_lrc = app.player.song.get("~lyrics")我验证了~lyrics确实是引用标记的正确方式,因为它在代码的其他部分中也是这样使用的。
这在某种程度上起到了作用。与之前的测试相比,这是一个改进,因为它没有告诉我有一些未定义的东西,以下是程序在我启动时告诉我的:
TypeError: stat: path should be string, bytes, os.PathLike or integer not NoneType
------
Traceback (most recent call last):
File "/usr/lib/python3/dist-packages/quodlibet/plugins/events.py", line 141, in __invoke
handler(*args)
File "/usr/lib/python3/dist-packages/quodlibet/ext/events/synchronizedlyrics.py", line 282, in plugin_on_song_started
self._build_data()
File "/usr/lib/python3/dist-packages/quodlibet/ext/events/synchronizedlyrics.py", line 195, in _build_data
if os.path.exists(new_lrc):
File "/usr/lib/python3.6/genericpath.py", line 19, in exists
os.stat(path)
TypeError: stat: path should be string, bytes, os.PathLike or integer, not NoneType我会让你决定你从中得到了什么。下面是另一段代码,它是拼图的另一部分:
def _parse_lrc_file(self, filename):
with open(filename, 'r', encoding="utf-8") as f:
raw_file = f.read()
raw_file = raw_file.replace("\n", "")
begin = 0
keep_reading = len(raw_file) != 0
tmp_dict = {}
compressed = []
while keep_reading:
next_find = raw_file.find("[", begin + 1)
if next_find == -1:
keep_reading = False
line = raw_file[begin:]
else:
line = raw_file[begin:next_find]
begin = next_find
# parse lyricsLine
if len(line) < 2 or not line[1].isdigit():
continue
close_bracket = line.find("]")
t = datetime.strptime(line[1:close_bracket], '%M:%S.%f')
timestamp = (t.minute * 60000 + t.second * 1000 +
t.microsecond / 1000)
words = line[close_bracket + 1:]
if not words:
compressed.append(timestamp)
else:
tmp_dict[timestamp] = words
for t in compressed:
tmp_dict[t] = words
compressed = []
keys = list(tmp_dict.keys())
keys.sort()
for key in keys:
self._lines.append((key, tmp_dict[key]))
del keys
del tmp_dict这就是让事情变得困难的部分,也是我现在陷入困境的地方。在我看来,代码期望处理文件,而不是标签,所以当它调用时,它们就不工作了。有什么我可以尝试的线索吗?
发布于 2019-05-13 04:17:38
不要紧,伙计们,我已经自己修改过了,它现在就像我想要的那样工作。这里有一个链接,你可以下载它,如果你想要的话,可以查看它:GitHub issue
https://stackoverflow.com/questions/56096257
复制相似问题