Python - Wave file over 4gb: struct.error: 'L' format requires 0 <= number <= 4294967295
Python - Wave file over 4gb: struct.error: 'L' format requires 0 <= number <= 4294967295
我试图在我的 python 脚本中创建一个超过 4GB 的音频波形文件,但出现错误。这是一个重现问题的简短脚本。谁能告诉我为什么会出现这样的错误以及如何修复它,或者它是否可能是一个错误?
我尝试使用 python 2.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
.wav 文件不能大于 4GB,因为 wave 文件格式规范不允许这样做。正如维基百科上的解释:
The WAV format is limited to files that are less than 4 GB, because of its use of a 32-bit unsigned integer to record the file size header.
另请参阅 Python 中的 issue 16461,它将限制从 2GB 增加到 4GB(仅此而已)。
我试图在我的 python 脚本中创建一个超过 4GB 的音频波形文件,但出现错误。这是一个重现问题的简短脚本。谁能告诉我为什么会出现这样的错误以及如何修复它,或者它是否可能是一个错误?
我尝试使用 python 2.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
.wav 文件不能大于 4GB,因为 wave 文件格式规范不允许这样做。正如维基百科上的解释:
The WAV format is limited to files that are less than 4 GB, because of its use of a 32-bit unsigned integer to record the file size header.
另请参阅 Python 中的 issue 16461,它将限制从 2GB 增加到 4GB(仅此而已)。