I'm trying to create an audio wave file over 4GB in my python script but I get an error. Here is a short script reproducing the problem. Can someone tell me why I got such an error and how to fix it, or if it might be a bug?
I tried with python 2.7 and 3.4, but got the same error with both.
# 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()
The output:
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 files cannot be bigger than 4GB as the wave file format specification prevents that. As explained on Wikipedia:
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.
See also issue 16461 in Python which increases the limit from 2GB to 4GB (but that's it).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With