python语音读取

python语音读取

文章目录

  • python语音读取
    • 导入必要的库
    • 读取音频文件
    • 通过时间点截取音频
    • 存储音频文件

导入必要的库

import os
import numpy as np
import soundfile as sf

读取音频文件

def read_audio(audio_path):
	"""
	:param audio_path:
	:return: audio[语音信号 np.array] samplerate[采样率] duaration[时间长度(秒)]
	"""
	audio, samplerate = sf.read(audio_path)
	shape = np.shape(audio)
	if len(shape) == 1:
		print("Mono audio.")
	else:
		print("Two channel or more.")
		audio1 = audio[:, 0]
		audio2 = audio[:, 1]
		audio = audio1 * 0.5 + audio2 * 0.5
	duration = len(audio) * 1.0 / samplerate  # 单位 s
	return audio, samplerate, duration

通过时间点截取音频

def extract_audio_section_by_time_interval(audio, samplerate, start_second, end_second):
	start_point = start_second * samplerate
	end_point = end_second * samplerate
	audio_section = audio[start_point:end_point]
	return audio_section

存储音频文件

def save_audio(save_path, audio, samplerate=16000):
	"""
	:param save_path:
	:param audio:
	:param samplerate:
	:return:
	"""
	sf.write(open(save_path, "wb"), audio, samplerate=samplerate)

你可能感兴趣的:(语音识别)