简体   繁体   中英

How to make a Python requests.Response object byte-like?

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, ie 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()

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM