3

I've downloaded a small .mp3 file from the net using Python's requests module, like this:

file_on_disk = open(filename, 'wb')
write(downloaded_file.content)
file_on_disk.close()

Now it works fine on the disk as an MP3 file should. However, I'd like to play it using python-vlc's MediaPlayer while it's still not written to disk, i.e. from memory. However, this doesn't work:

vlc.MediaPlayer(downloaded_file.content).play()

I get a TypeError: a bytes-like object is required, not 'str' message. I checked the type of the content and type(downloaded_file.content) identifies it as <class 'bytes'>, not str. The same MediaPlayer nicely plays the file once it's saved to the disk. What am I missing here, and how can I convert the file in-memory to a format that vlc.MediaPlayer is able to play?

(I'm a beginner so the answer might be obvious, but it keeps eluding me.)

edit: my full code is actually:

import requests, vlc
downloaded_file = requests.get('https://cdn.duden.de/_media_/audio/ID4111733_460321137.mp3')
from vlc import MediaPlayer
MediaPlayer(downloaded_file.content).play()
aed
  • 80
  • 6
  • Try `bytes(downloaded_file.content)`. – martineau Aug 20 '20 at 20:15
  • forgot to say that I actually tried this before posting, and it didn't work. – aed Aug 22 '20 at 20:49
  • It should work — how did it not? What was the error? – martineau Aug 22 '20 at 20:54
  • 1
    @martineau I also find it strange, I try to play `bytes(downloaded_file.content)` and I still get a `TypeError: a bytes-like object is required, not 'str'` error. – aed Aug 22 '20 at 22:10
  • It might work by converting the string into a [`io.ByteIO`](https://docs.python.org/3/library/io.html#io.BytesIO) object and pass that to `vlc` as the "file". – martineau Aug 22 '20 at 22:14
  • @martineau no, in this case I get an `expected str, bytes or os.PathLike object, not _io.BytesIO` error – aed Aug 22 '20 at 22:30
  • That error message sounds like it wants a string (or bytes or PathLike) object that specifies a path to a file to play — which implies something different than the one you mentioned in your question…and may mean that `vlc` only works with files, not streams of data in memory. You may have no choice but to save it to disk first. You should be able to use the `tempfile` module to do that. – martineau Aug 22 '20 at 23:19

1 Answers1

-1

Check out this possibly related question, you need to create a vlc Instance.

IliassA
  • 95
  • 2
  • 8