0

I have mp3 files that I'm trying to convert to 8, 24 or 32 bits, I was trying to do it using AudioSegment module using the following:

audio = AudioSegment.from_mp3('audio.mp3')
audio.resample(sample_width=1)

which returns an error: AttributeError: 'AudioSegment' object has no attribute 'resample' (I found resample(sample_rate_Hz=None, sample_width=None, channels=None, console_output=False) in AudioSegment documentation.)

I tried to convert mp3 files to wav and then change sample width using the following function:

def quantify(audio,k): 
    #audio: wave object \ k : new sample width
    sw = audio.getsampwidth()
    typ = {1:np.int8, 2:np.int16, 4:np.int32}
    data = get_data(audio) #returns numpy array 
    newdata = data.astype(typ.get(k))
    #I can then write this new data to a new wav file
    return newdata

But using this way I think it's added work to convert from mp3 to wav to mp3 again, also using this method I can't convert to 24 bits since Numpy has no data type for that from what I've read (I don't know if it's possible with AudioSegment either). Any Suggestions?

Zack
  • 13
  • 4
  • Take a look at `PyAV` or the older `PyMedia`. They are some of the libraries you can use with these types of conversion. – MYousefi Nov 18 '22 at 18:18
  • MP3 encoded audio has no inherent bits per sample. The source material could have been 8 bit, 24 bit, who knows. It's best to configure the codec when decoding, to output the sample format you want, with your desired bit depth. – Brad Nov 20 '22 at 04:38

0 Answers0