0

Hello I want to split an AudioSegment into chunks that are all 10 seconds long. I tried it like this:

for n in range(int(duration/10)):
    start = n*1000*10
    end = (n*10+10)*1000
    audio_segment = audio[start:end]

The problem is now that i want to access these chunks. But when I enter the audio_segment variable into the function i am using the following error pops up: TypeError: a bytes-like object is required, not 'AudioSegment'.

This error obviously tells me that it only accepts a bytes-like object. I can turn the AudioSegment into a bytes-like object by exporting it into a mp3 and then open it up again as rb but this solution takes very long and is in my opinion not good because I really do not need these segments to be saved.

Can anyone help me access the AudioSegment as a bytes-like object without saving it?

  • you could try to use `io.BytesIO` to create file-like object in memory. – furas May 28 '21 at 11:02
  • always put full error message (starting at word "Traceback") in question (not comment) as text (not screenshot, not link to external portal). There are other useful information. – furas May 28 '21 at 11:02
  • maybe `AudioSegment` has som function to get it as bytes-like data? – furas May 28 '21 at 11:03
  • in [source code of AudioSegment](https://github.com/jiaaro/pydub/blob/master/pydub/audio_segment.py) I found function `.raw_data()` and maybe this is your solution. – furas May 28 '21 at 11:06

0 Answers0