jiaaro / pydub

Manipulate audio with a simple and easy high level interface
http://pydub.com
MIT License
8.86k stars 1.04k forks source link

MemoryError on Raspberry PI 4 #479

Open MaxSox64 opened 4 years ago

MaxSox64 commented 4 years ago

I'm using pydub on a RPi 4 model B with 4GB RAM, running Raspbian Buster Lite. Reading a flac or ape file larger than 200-220MB raises a MemoryError exception, always at the same line of code.

Steps to reproduce

`import sys import os from pydub import AudioSegment

print ('Reading audio file:', sys.argv[1]) full_wav = AudioSegment.from_file(sys.argv[1], os.path.splitext(sys.argv[1])[1][1:])
print ("Done!")`

Expected behavior

'Done!' should be printed

Actual behavior

pi@raspberrypi:~/flask_app $ python3 testpydub.py "/mnt/musiclib/Bach, J.S/L'Arte della fuga/Bach, J. S. - L'arte della Fuga BWV 1080 - Glenn Gould/CDImage.ape" ape.mp3 Reading audio file: /mnt/musiclib/Bach, J.S/L'Arte della fuga/Bach, J. S. - L'arte della Fuga BWV 1080 - Glenn Gould/CDImage.ape Traceback (most recent call last): File "testpydub.py", line 6, in <module> full_wav = AudioSegment.from_file(sys.argv[1], os.path.splitext(sys.argv[1])[1][1:]) File "/home/pi/.local/lib/python3.7/site-packages/pydub/audio_segment.py", line 729, in from_file obj = cls._from_safe_wav(BytesIO(p_out)) File "/home/pi/.local/lib/python3.7/site-packages/pydub/audio_segment.py", line 761, in _from_safe_wav obj = cls(data=file) File "/home/pi/.local/lib/python3.7/site-packages/pydub/audio_segment.py", line 222, in __init__ wav_data = read_wav_audio(data) File "/home/pi/.local/lib/python3.7/site-packages/pydub/audio_segment.py", line 132, in read_wav_audio data[pos:pos + data_hdr.size]) MemoryError

Your System configuration

mhurliman commented 4 years ago

I'm getting this error as well on the Raspberry Pi Zero W, which only has 512 MB RAM. I'm trying to load/decompress ~5 MB mp3 files, which decompress to ~50-60 MB.

This callstack duplicates the raw audio data several times. This spikes the Python instance's memory usage, maxes out the 512 MB, hits swap memory causing the program to slow to a crawl, then finally hits the MemoryError.

If some of these memory copies could be eliminated we'd be all gucci.

Harrison88 commented 4 years ago

I've got a pull request open (https://github.com/jiaaro/pydub/pull/490) that eliminates some of those copies, reducing memory usage by about half.