我试图在python脚本中创建一个超过4GB的音频波形文件,但是我得到了一个错误。下面是一个重现问题的简短脚本。有人能告诉我为什么会有这样的错误吗?如何修复它,或者它是否可能是一个错误?
我尝试使用python2.7和3.4,但两者都有相同的错误。
# script for python 2
import wave
# create a large (>4gb) file
wf = wave.open("foo.wav", "w")
wf.setnchannels(2)
wf.setsampwidth(2)
wf.setframerate(44100)
text = 'a' * 1024**2
for i in xrange(5 * 1024):
print i
wf.writeframes(text)
wf.close()产出:
4088
4089
4090
4091
4092
4093
4094
4095
Traceback (most recent call last):
File "test.py", line 16, in <module>
wf.writeframes(text)
File "/usr/lib/python2.7/wave.py", line 444, in writeframes
self._patchheader()
File "/usr/lib/python2.7/wave.py", line 496, in _patchheader
self._file.write(struct.pack('<L', 36 + self._datawritten))
struct.error: 'L' format requires 0 <= number <= 4294967295
Exception struct.error: "'L' format requires 0 <= number <= 4294967295" in <bound method Wave_write.__del__ of <wave.Wave_write instance at 0x7fed4ed58908>> ignored发布于 2015-01-10 19:33:40
.wav文件不能大于4GB,因为波形文件格式规范阻止了这一点。正如维基百科所解释的那样:
WAV格式仅限于小于4GB的文件,因为它使用32位无符号整数来记录文件大小头。
请参见Python中的第16461期,它将限制从2GB提高到4GB (但仅此而已)。
https://stackoverflow.com/questions/27880302
复制相似问题