【发布时间】:2019-05-12 04:33:59
【问题描述】:
音乐播放器“QuodLibet”包含一个插件,可将歌词与当前播放的歌曲同步。它通过解析来自.lrc 文件的数据来实现这一点。然而,这些数据也可以存储在文件元数据中,我想修改它以解决这个问题。
我在电脑上进行了一些挖掘,发现了包含插件的 .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
我会让你决定你从中得到什么。这是另一个sn-p代码,它是拼图的另一部分:
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
这就是让事情变得困难的部分,这就是我现在陷入困境的地方。在我看来,代码期望处理一个文件,而不是一个标签,所以当它进行调用时,它们不起作用。有什么我可以尝试的线索吗?
【问题讨论】:
标签: python-3.x tags audio-player .lrc