Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Wave file over 4gb: struct.error: 'L' format requires 0 <= number <= 4294967295

Tags:

python

wave

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
like image 515
marcaurele Avatar asked Jan 10 '15 19:01

marcaurele


1 Answers

.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).

like image 153
Simeon Visser Avatar answered Oct 25 '22 18:10

Simeon Visser