1

I used pydub to load the audio file just as follows,

audio = AudioSegment.from_mp3(file_path).set_frame_rate(22050).set_channel(1)

but if i get the binary data of the audio file, i use the following way,

def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()
audio = AudioSegment(data=get_file_content(file_path), sample_width=2, frame_rate=22050, channels=1)

However when i try to use the property audio.get_array_of_samples(), i got different array from these two ways.

So i want to ask how to make these two way output the same array in reading the same audio.

Kwoks
  • 33
  • 5

1 Answers1

0

The first code segment suggests that this is an MP3 file. In the second case, you're reading that as 16bit WAV file. That's going to produce garbage.

If you have the MP3 file in a byte array, then use this code:

AudioSegment.from_mp3(BytesIO(mp3_data))

with BytesIO from here: https://docs.python.org/3/library/io.html

Aaron Digulla
  • 321,842
  • 108
  • 597
  • 820
  • @Jiaaro There is no way to pass an MP3 as binary array to AudioSegment? – Aaron Digulla Mar 25 '19 at 09:25
  • you can do: `AudioSegment.from_mp3(BytesIO(mp3_data))` but pydub will write the data to a temp file anyway and then pass the temp file to ffmpeg – you can also pass an open file or any file-like object to `AudioSegment.from_mp3()` – Jiaaro Mar 25 '19 at 21:25
  • 1
    @Jiaaro My guess is that this was the question: How to load data from a byte array. – Aaron Digulla Mar 26 '19 at 09:37