python wave模块可以接受StringIO对象吗
我正在尝试使用 wave 模块在 python 中读取 wav 文件。
我的应用程序的不典型之处在于,我不使用文件或文件名来读取 wav 文件,而是将 wav 文件放在缓冲区中。
这就是我正在做的事情
import StringIO
buffer = StringIO.StringIO()
buffer.output(wav_buffer)
file = wave.open(buffer, 'r')
,但是当我运行它时,我收到了 EOFError
...
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 493, in open
return Wave_read(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 163, in __init__
self.initfp(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 128, in initfp
self._file = Chunk(file, bigendian = 0)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/chunk.py", line 63, in __init__
raise EOFError
我知道 StringIO
的东西可以用于创建 wav 文件,我尝试了以下并且有效
import StringIO
buffer = StringIO.StringIO()
audio_out = wave.open(buffer, 'w')
audio_out.setframerate(m.getRate())
audio_out.setsampwidth(2)
audio_out.setcomptype('NONE', 'not compressed')
audio_out.setnchannels(1)
audio_out.writeframes(raw_audio)
audio_out.close()
buffer.flush()
# these lines do not work...
# buffer.output(wav_buffer)
# file = wave.open(buffer, 'r')
# this file plays out fine in VLC
file = open(FILE_NAME + ".wav", 'w')
file.write(buffer.getvalue())
file.close()
buffer.close()
i'm trying to use the wave module to read wav files in python.
whats not typical of my applications is that I'm NOT using a file or a filename to read the wav file, but instead i have the wav file in a buffer.
And here's what i'm doing
import StringIO
buffer = StringIO.StringIO()
buffer.output(wav_buffer)
file = wave.open(buffer, 'r')
but i'm getting a EOFError
when i run it...
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 493, in open
return Wave_read(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 163, in __init__
self.initfp(f)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/wave.py", line 128, in initfp
self._file = Chunk(file, bigendian = 0)
File "/System/Library/Frameworks/Python.framework/Versions/2.5/lib/python2.5/chunk.py", line 63, in __init__
raise EOFError
i know the StringIO
stuff works for creation of wav file and i tried the following and it works
import StringIO
buffer = StringIO.StringIO()
audio_out = wave.open(buffer, 'w')
audio_out.setframerate(m.getRate())
audio_out.setsampwidth(2)
audio_out.setcomptype('NONE', 'not compressed')
audio_out.setnchannels(1)
audio_out.writeframes(raw_audio)
audio_out.close()
buffer.flush()
# these lines do not work...
# buffer.output(wav_buffer)
# file = wave.open(buffer, 'r')
# this file plays out fine in VLC
file = open(FILE_NAME + ".wav", 'w')
file.write(buffer.getvalue())
file.close()
buffer.close()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
试试这个:
try this:
StringIO 不是这样工作的。它不是一个连接到自身的管道:当您
read()
时,您不会收到之前传递给write()
的数据。 (不用介意output()
,我猜这是一个错误,因为没有这样的方法。)相反,它充当单独的读管道和写管道。
read()
将返回的内容通过构造函数传入:并且从
write()
收集的任何内容都可以通过getvalue()
读取。(我使用了二进制读取模式,因为这就是正在发生的情况,尽管
wave
模块无论如何都会默默地将r
模式转换为rb
。)A StringIO doesn't work like that. It's not a pipe that's connected to itself: when you
read()
, you don't receive data that you previously passed towrite()
. (Never mindoutput()
which I'm guessing is a mispaste as there is no such method.)Instead it acts as a separate read-pipe and write-pipe. The content that
read()
will return is passed in with the constructor:And any content collected from
write()
is readable throughgetvalue()
.(I used binary read mode because this is what's happening, though the
wave
module will silently convertr
mode torb
anyway.)