标签:secret return ips key span client event https api
代码如下
from aip import AipSpeech import os APP_ID = ‘14940732‘ API_KEY = ‘tm1jAzv8dnDQM5yzFUk0ED9j‘ SECRET_KEY = ‘2WsPOg8k7GHtPnN2PBIsuf9LRpozYK8A‘ client = AipSpeech(APP_ID, API_KEY, SECRET_KEY) def audio_to_text(filename): filename = filename.split(‘.‘)[0] os.system(f"ffmpeg -y -i {filename}.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 {filename}.pcm") with open(f"{filename}.pcm", ‘rb‘) as fp: file_content = fp.read() result = client.asr(file_content, ‘pcm‘, 16000, {‘dev_pid‘: 1536}) print(result) result_str = result.get(‘result‘)[0] return result_str def text_to_audio(result_str): result = client.synthesis(result_str, ‘zh‘, 1, { ‘vol‘: 5, ‘spd‘: 3, ‘pit‘: 8, ‘per‘: 4 }) # 识别正确返回语音二进制 错误则返回dict 参照下面错误码 if not isinstance(result, dict): with open(‘audio.mp3‘, ‘wb‘) as f: f.write(result) os.system(‘audio.mp3‘) a = audio_to_text(‘audio.mp3‘) if ‘你好我的世界‘ in a: text_to_audio(a) else: text_to_audio(f‘你刚才是不是在说, {a}吗?‘)
提前需下载pip install baidu=aip
以及ffmpeg地址如下https://pan.baidu.com/s/1jonSAa_TG2XuaJEy3iTmHg 提取码为w6hk
标签:secret return ips key span client event https api
原文地址:https://www.cnblogs.com/wj12312/p/10021100.html