kokarare1212 / librespot-python

Open Source Spotify Client
http://librespot-python.rtfd.io
Apache License 2.0
229 stars 42 forks source link

[BUG] Audio key error when switching to very high quality #154

Closed mponweiser closed 2 years ago

mponweiser commented 2 years ago

Describe the bug Hi @kokarare1212, I am following the example "Get Music Stream", and depending on the AudioQuality flag, I am getting a RuntimeError:

from librespot.core import Session
from librespot.metadata import TrackId
from librespot.audio.decoders import AudioQuality, VorbisOnlyAudioQuality

from librespot import Version
Version.system_info_string()
# 'librespot-python 0.0.6; Python 3.10.4; Linux'

# family (=premium) account
session = Session.Builder() \
    .user_pass("XXX", "XXX") \
    .create()

track_id = TrackId.from_uri("spotify:track:2o4MCgSayrqYirCNfbx0jD")
stream = session.content_feeder().load(track_id, VorbisOnlyAudioQuality(AudioQuality.VERY_HIGH), False, None)

Error message:

Audio key error, code: 1
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/mpon/.local/lib/python3.10/site-packages/librespot/audio/__init__.py", line 722, in load
    return self.load_track(playable_id, audio_quality_picker, preload,
  File "/home/mpon/.local/lib/python3.10/site-packages/librespot/audio/__init__.py", line 783, in load_track
    return self.load_stream(file, track, None, preload, halt_listener)
  File "/home/mpon/.local/lib/python3.10/site-packages/librespot/audio/__init__.py", line 737, in load_stream
    return CdnFeedHelper.load_track(self.__session, track, file,
  File "/home/mpon/.local/lib/python3.10/site-packages/librespot/audio/__init__.py", line 336, in load_track
    key = session.audio_key().get_audio_key(track.gid, file.file_id)
  File "/home/mpon/.local/lib/python3.10/site-packages/librespot/audio/__init__.py", line 277, in get_audio_key
    return self.get_audio_key(gid, file_id, False)
  File "/home/mpon/.local/lib/python3.10/site-packages/librespot/audio/__init__.py", line 278, in get_audio_key
    raise RuntimeError(
RuntimeError: Failed fetching audio key! gid: XXX, fileId: XXX

I am very certain I have a family account and in the desktop app on Linux can stream on HQ, so I don't see how this error can happen. Would you be willing to look into this?

kokarare1212 commented 2 years ago

Generally, such errors are caused by codecs that are not supported, account issues that prevent playback at the highest audio quality, or music that does not support the highest audio quality. Can you tell me the Spotify URI where the problem occurs?

mponweiser commented 2 years ago

Thank you, this makes it a bit easier to understand what might be the problem. However, what are "account issues"? Is Spotify selectively disabling HQ playback on their side, even for premium users?

As to your question: the song ID is in the bug report, but others are causing the same problem:

track_id = TrackId.from_uri("spotify:track:2o4MCgSayrqYirCNfbx0jD")
stream = session.content_feeder().load(track_id, VorbisOnlyAudioQuality(AudioQuality.VERY_HIGH), False, None)
kokarare1212 commented 2 years ago

Strangely enough, it worked fine when I tried it. I think the possible cause is that the songs do not correspond to the region of the account.

from librespot.audio.decoders import VorbisOnlyAudioQuality, AudioQuality
from librespot.core import Session
from librespot.metadata import TrackId
import logging

logging.basicConfig(level=logging.DEBUG)
session = Session.Builder().stored().create()
track_id = TrackId.from_uri("spotify:track:2o4MCgSayrqYirCNfbx0jD")
stream = session.content_feeder().load(track_id, VorbisOnlyAudioQuality(AudioQuality.VERY_HIGH), False, None)
stream.input_stream.stream().read()
mponweiser commented 2 years ago

OK, so I re-checked everything and it seems I was using an old - free - account, so the error was to be expected. Thanks a lot you for your attention and patience, and of course, your work :-)