正如许多关于阅读MP3的文章中所建议的那样,调用
ffmpeg和手动解析它
stdout是一项繁琐的任务(在许多特殊情况下,因为可能会使用不同数量的通道,等等),因此这是一个使用
pydub(您需要
pipinstall pydub首先使用)的可行解决方案。
此代码允许
使用与以下类似的APIscipy.io.wavfile.read/write
读取MP3到numpy数组/将numpy数组写入MP3文件:
import pydub import numpy as npdef read(f, normalized=False): """MP3 to numpy array""" a = pydub.AudioSegment.from_mp3(f) y = np.array(a.get_array_of_samples()) if a.channels == 2: y = y.reshape((-1, 2)) if normalized: return a.frame_rate, np.float32(y) / 2**15 else: return a.frame_rate, ydef write(f, sr, x, normalized=False): """numpy array to MP3""" channels = 2 if (x.ndim == 2 and x.shape[1] == 2) else 1 if normalized: # normalized array - each item should be a float in [-1, 1) y = np.int16(x * 2 ** 15) else: y = np.int16(x) song = pydub.AudioSegment(y.tobytes(), frame_rate=sr, sample_width=2, channels=channels) song.export(f, format="mp3", bitrate="320k")
笔记:
- 目前仅适用于16位文件(即使24位WAV文件非常普遍,我也很少见过24位MP3文件…是否存在?)
normalized=True
允许使用浮点数组([-1,1中的每个项目)
用法示例:
sr, x = read('test.mp3')print(x)#[[-225 707]# [-234 782]# [-205 755]# ..., # [ 303 89]# [ 337 69]# [ 274 89]]write('out2.mp3', sr, x)


