1

When I try to export long audio segments to mp3, I get an error OSError: [Errno 22] Invalid argument. The maximal acceptable duration seems to be somewhere between 202 and 205 minutes. The error and the code are below.

Is there any way to be able to export mp3s longer than 205 minutes?

compilation_audio.export(mp3_path, format = "mp3")   
File "/usr/local/lib/python3.6/site-packages/pydub/audio_segment.py", line 612, 
 in export wave_data.writeframesraw(self._data)
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 422, 
 in writeframesraw self._file.write(data)   
File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/tempfile.py", line 483,
 in func_wrapper return func(*args, **kwargs) 
OSError: [Errno 22] Invalid argument

This is the code. I have used "Fur Elise" from here as the initial mp3 file.

202 minutes segment ("Fur Elise" repeated 69 times) saves ok, 205 minutes segment ("Fur Elise" repeated 70 times) export fails.

import os, pydub
audio_name = os.path.expanduser('~/Downloads/Fur Elise.mp3') # just under 3 min long
audio = pydub.AudioSegment.from_mp3(audio_name)
for compilation_audio in (audio * 69, audio * 70):
    length_music = len(compilation_audio) / 60000.0
    print("compilation_audio duration (min): " + str((length_music)))
    mp3_path = os.path.expanduser('~/Downloads/compilation' + str(int(length_music)) + '.mp3')
    compilation_audio.export(mp3_path, format = "mp3")

If there is an attempt to save a 1000+ min long mp3 (Fur Elise * 370), there is another error message:

    compilation_audio.export(mp3_path, format = "mp3")
  File "/usr/local/lib/python3.6/site-packages/pydub/audio_segment.py", line 612, in export
    wave_data.writeframesraw(self._data)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 416, in writeframesraw
    self._ensure_header_written(len(data))
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 457, in _ensure_header_written
    self._write_header(datasize)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 474, in _write_header
    self._sampwidth * 8, b'data'))
struct.error: 'L' format requires 0 <= number <= 4294967295
Exception ignored in: <bound method Wave_write.__del__ of <wave.Wave_write object at 0x1177e7cf8>>
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 316, in __del__
    self.close()
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 434, in close
    self._ensure_header_written(0)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 457, in _ensure_header_written
    self._write_header(datasize)
  File "/usr/local/Cellar/python3/3.6.2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/wave.py", line 474, in _write_header
    self._sampwidth * 8, b'data'))
struct.error: 'L' format requires 0 <= number <= 4294967295
Yulia V
  • 3,507
  • 10
  • 31
  • 64

1 Answers1

1

I think this may be a bug in Python!

There's a related issue with no solution in the pydub repo.

We ran into this problem as well.

There was this issue in PyTorch with a similar stack trace. They identified the problem being with lseek(). I didn't understand the fix, but I was able to find this issue in the Python bug tracker which does not appear to have been resolved. The problem appears to be with a signed integer wrapping around, making seeking in a file larger than 2^63 under certain circumstances impossible.

David Eyk
  • 12,171
  • 11
  • 63
  • 103