Python VLC 模块在没有标签的情况下无法工作
Python VLC module not working with no tags
我正在使用 Python 中的 VLC 模块播放一些使用 YouTube 到 MP3 转换器下载的音乐,用于一个项目。问题是,我发现这些文件不喜欢合作,Python 最终向我抛出一大堆红色。我相信这是因为 MP3 文件没有标签,所以当模块检查它们并且它们不存在时,它会向我抛出错误。这是我的代码:
import vlc
import os
import time
def playtrack(fileid, songname):
filename = fileid + ".mp3"
print('Now playing: ' + songname)
music = vlc.MediaPlayer(filename)
music.play()
print(music.get_state())
time.sleep(60)
print("Finished.")
#os.remove(filename)
playtrack("tVj0ZTS4WF4", "foo")
这是输出 "Very long...":
Now playing: foo
State.NothingSpecial
Warning: option --plugin-path no longer exists.
Warning: option --plugin-path no longer exists.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
还有比这更多的错误,但 Stack Overflow 限制了我把它们都放上去。反正都是一样的。
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- Invalid MPEG layer bits.
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- Invalid bit rate.
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Header::parse() -- Invalid bit rate.
TagLib: MPEG::Header::parse() -- Could not read the next frame header.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Properties::read() -- Could not find a valid first MPEG frame in the stream.
Finished.
Process finished with exit code 0
这是下载器,如果需要的话:
import youtube_dl
import os
class InvalidURL(Exception):
pass
class SongExists(Exception):
pass
def download(url):
try:
options = {
'format': 'bestaudio/best',
'extractaudio': True, # only keep the audio
'audioformat': "mp3", # convert to wav
'outtmpl': '%(id)s.mp3', # name the file the ID of the video
'noplaylist': True, # only download single song, not playlist
}
with youtube_dl.YoutubeDL(options) as ydl:
r = ydl.extract_info(url, download=False)
if os.path.isfile(str(r["id"])):
raise SongExists('This song has already been requested.')
print("Downloading" + str(r["title"]))
ydl.download([url])
print("Downloaded" + str(r["title"]))
return r["title"], r["id"]
except youtube_dl.utils.DownloadError:
raise InvalidURL('This URL is invalid.')
if __name__ == "__main__":
download("https://www.youtube.com/watch?v=tVj0ZTS4WF4")
我已经在我的机器上试过你的代码并且它有效。我认为您使用的是过时的版本或过时的库。所以请尝试下面的代码,如果您找到更可靠的解决方案,请告诉我....
sudo pip uninstall vlc
sudo pip uninstall python-vlc
sudo pip install python-vlc
我正在使用 Python 中的 VLC 模块播放一些使用 YouTube 到 MP3 转换器下载的音乐,用于一个项目。问题是,我发现这些文件不喜欢合作,Python 最终向我抛出一大堆红色。我相信这是因为 MP3 文件没有标签,所以当模块检查它们并且它们不存在时,它会向我抛出错误。这是我的代码:
import vlc
import os
import time
def playtrack(fileid, songname):
filename = fileid + ".mp3"
print('Now playing: ' + songname)
music = vlc.MediaPlayer(filename)
music.play()
print(music.get_state())
time.sleep(60)
print("Finished.")
#os.remove(filename)
playtrack("tVj0ZTS4WF4", "foo")
这是输出 "Very long...":
Now playing: foo
State.NothingSpecial
Warning: option --plugin-path no longer exists.
Warning: option --plugin-path no longer exists.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
还有比这更多的错误,但 Stack Overflow 限制了我把它们都放上去。反正都是一样的。
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- Invalid MPEG layer bits.
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- Invalid bit rate.
TagLib: MPEG::Header::parse() -- Invalid MPEG version bits.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Header::parse() -- Invalid bit rate.
TagLib: MPEG::Header::parse() -- Could not read the next frame header.
TagLib: MPEG::Header::parse() -- The next frame was not consistent with this frame.
TagLib: MPEG::Properties::read() -- Could not find a valid first MPEG frame in the stream.
Finished.
Process finished with exit code 0
这是下载器,如果需要的话:
import youtube_dl
import os
class InvalidURL(Exception):
pass
class SongExists(Exception):
pass
def download(url):
try:
options = {
'format': 'bestaudio/best',
'extractaudio': True, # only keep the audio
'audioformat': "mp3", # convert to wav
'outtmpl': '%(id)s.mp3', # name the file the ID of the video
'noplaylist': True, # only download single song, not playlist
}
with youtube_dl.YoutubeDL(options) as ydl:
r = ydl.extract_info(url, download=False)
if os.path.isfile(str(r["id"])):
raise SongExists('This song has already been requested.')
print("Downloading" + str(r["title"]))
ydl.download([url])
print("Downloaded" + str(r["title"]))
return r["title"], r["id"]
except youtube_dl.utils.DownloadError:
raise InvalidURL('This URL is invalid.')
if __name__ == "__main__":
download("https://www.youtube.com/watch?v=tVj0ZTS4WF4")
我已经在我的机器上试过你的代码并且它有效。我认为您使用的是过时的版本或过时的库。所以请尝试下面的代码,如果您找到更可靠的解决方案,请告诉我....
sudo pip uninstall vlc
sudo pip uninstall python-vlc
sudo pip install python-vlc