简体   繁体   English

如何制作 Python requests.Response object 字节状?

[英]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:我使用 Python 的 requests 模块从网上下载了一个 small.mp3 文件,如下所示:

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.现在它可以像 MP3 文件一样在磁盘上正常工作。 However, I'd like to play it using python-vlc's MediaPlayer while it's still not written to disk, ie from memory.但是,我想使用 python-vlc 的 MediaPlayer 播放它,而它还没有写入磁盘,即来自 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.我得到一个TypeError: a bytes-like object is required, not 'str'消息。 I checked the type of the content and type(downloaded_file.content) identifies it as <class 'bytes'>, not str.我检查了内容的类型, type(downloaded_file.content)将其标识为 <class 'bytes'>,而不是 str。 The same MediaPlayer nicely plays the file once it's saved to the disk.将文件保存到磁盘后,同一个 MediaPlayer 可以很好地播放文件。 What am I missing here, and how can I convert the file in-memory to a format that vlc.MediaPlayer is able to play?我在这里缺少什么,如何将内存中的文件转换为 vlc.MediaPlayer 能够播放的格式?

(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 .查看这个可能相关的问题,您需要创建一个 vlc Instance

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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