赞
踩
# -*- coding:utf-8 -*- # -*- author:zzZ_CMing CSDN address:https://blog.csdn.net/zzZ_CMing # -*- 2018/07/12; 15:19 # -*- python3.5 import pyaudio import wave input_filename = "input.wav" # 麦克风采集的语音输入 input_filepath = "音频存储位置" # 输入文件的path in_path = input_filepath + input_filename def get_audio(filepath): aa = str(input("是否开始录音? (是/否)")) if aa == str("是") : CHUNK = 256 FORMAT = pyaudio.paInt16 CHANNELS = 1 # 声道数 RATE = 11025 # 采样率 RECORD_SECONDS = 5 WAVE_OUTPUT_FILENAME = filepath p = pyaudio.PyAudio() stream = p.open(format=FORMAT, channels=CHANNELS, rate=RATE, input=True, frames_per_buffer=CHUNK) print("*"*10, "开始录音:请在5秒内输入语音") frames = [] for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)): data = stream.read(CHUNK) frames.append(data) print("*"*10, "录音结束\n") stream.stop_stream() stream.close() p.terminate() wf = wave.open(WAVE_OUTPUT_FILENAME, 'wb') wf.setnchannels(CHANNELS) wf.setsampwidth(p.get_sample_size(FORMAT)) wf.setframerate(RATE) wf.writeframes(b''.join(frames)) wf.close() elif aa == str("否"): exit() else: print("无效输入,请重新选择") get_audio(in_path) get_audio(in_path)
说明:
input.wav
,所以在你保存音频的地址下永远只生成一个音频文件(后一个会覆盖前一个);# 采集音频
get_audio(in_path)
sys.stdout.write("you ask>> ")
# 科大语音听写:语音转文本输出 请参考上一篇博客
input_seqs = keda_API.XF_text(in_path, audio_rate)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。