1
我在Python中使用VLC模塊來播放一些音樂下載與YouTube到MP3轉換器的項目。問題是,我發現這些文件不喜歡合作,python最終會對我產生負面影響。我相信這是因爲mp3文件沒有標籤,所以當模塊檢查它們並且它們不存在時,它會向我發出錯誤。這裏是我的代碼:Python的VLC模塊不工作,沒有標籤
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")
這裏是輸出「很長......」:
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.
沒有比這種方式更多的錯誤,但StackOverflow上限制了我從把他們所有。無論如何,它們都是一樣的。
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")
我通過使用WAV文件,而不是MP3文件解決了問題。現在都很好,不過謝謝。 –
@GamingWithAltitude歡迎您。 –