1

I am trying to split large podcasts mp3 files into smaller 5 minute chunks using python and the pydub library. This is my code:

folder = r"C:\temp"
filename = r"p967.mp3"

from pydub import AudioSegment

sound = AudioSegment.from_mp3(folder + "\\" + filename)

This works fine for small files but for the large podcasts i am interested in 100mb +. This returns the following error.

Traceback (most recent call last):    
  File "C:\temp\mp3split.py", line 6, in <module>
sound = AudioSegment.from_mp3(folder + "\\" + filename)
  File "C:\Python27\lib\site-packages\pydub\audio_segment.py", line 522, in from_mp3
return cls.from_file(file, 'mp3', parameters)
  File "C:\Python27\lib\site-packages\pydub\audio_segment.py", line 511, in from_file
obj = cls._from_safe_wav(output)
  File "C:\Python27\lib\site-packages\pydub\audio_segment.py", line 544, in _from_safe_wav
return cls(data=file)
  File "C:\Python27\lib\site-packages\pydub\audio_segment.py", line 146, in __init__
data = data if isinstance(data, (basestring, bytes)) else data.read()
MemoryError

Is this a limitation of the library? should i be using an alternative approach to achieve this?


If i add the follwoing code to check the memory status at the point of running.

import psutil
print psutil.virtual_memory()

This prints:

svmem(total=8476975104L, available=5342715904L, percent=37.0 used=3134259200L, free=5342715904L)

This suggests to me that there is plenty of memory at the start of the operation, though I am happy to be proven wrong.

CodeCupboard
  • 1,507
  • 3
  • 17
  • 26
  • Thanks for the update. For others, `svmem` reports available virtual memory. This shows about 5Gb available, 50x what's needed. – Prune Aug 11 '17 at 22:53

1 Answers1

2

Yes, the most likely cause is that you've simply run out of available memory. Do you know how much memory you have available before you execute that statement? Consider inserting a system call (see the os package) just before the failing statement.

Prune
  • 76,765
  • 14
  • 60
  • 81
  • Hi thank you for taking the time to offer some help. I have added more to my question showing the output when i request memory usage just before running the line of code that causes the error. Does this seem about right? – CodeCupboard Aug 11 '17 at 22:43
  • That's just what I was looking for. Is it possible that the object requires 100Mb of *physical memory, and you don't have that much? I've had that problem a couple of times. – Prune Aug 11 '17 at 22:55