在处理音视频数据时,我们可以先将音视频数据读取成数组,这样进行剪切、截取、合并等等操作就非常的方便。
封装了一个方读取音频数组数据:
支持从wav、pcm、raw中读取音频数组和采样率等信息
依赖库:os、array、numpy、scipy.io.wavfile
def read_audio_data(audio_file, pcm_rate=16000, chs=1, bit_depth='int16'):
# 读取音频数组
if not os.path.exists(audio_file):
raise IOError
if audio_file.endswith('.wav'):
wave_rate, audio_data = wavfile.read(audio_file)
return audio_data.T, wave_rate
elif audio_file.endswith('.pcm') or audio_file.endswith('.raw'):
data_array = array.array('h')
if bit_depth == 'float32':
data_array = array.array('f')
elif bit_depth == 'int16':
data_array = array.array('h')
with open(audio_file, 'rb') as f:
data_array.