CosyVoice TTS实时语音合成语音克隆流式api接口,requests请求使用案例

参考:

https://blog.csdn.net/weixin_42357472/article/details/140321056
https://blog.csdn.net/weixin_42357472/article/details/137239085
https://www.bilibili.com/video/BV1BE421A7CG

主要改动app.py 、cosyvoice.py两个文件,这里用的CosyVoice docker环境测试

CosyVoice 语音合成流式api接口

api接口,放在/opt/CosyVoice/CosyVoice下
在这里插入图片描述

app.py

import time
import io, os, sys
from flask_cors import CORS
ROOT_DIR =
以下是使用科大讯飞API进行语音识别和合成的Python代码示例: 语音识别: ```python import requests import json url = "http://api.xfyun.cn/v1/service/v1/iat" APPID = "YOUR_APPID" # 替换为自己的APPID API_KEY = "YOUR_API_KEY" # 替换为自己的API_KEY audio_file = "test.wav" # 需要识别的音频文件路径 with open(audio_file, "rb") as f: audio_data = f.read() param = { "engine_type": "sms16k", # 引擎类型,此处使用16k采样率的普通话识别 "aue": "raw", # 音频编码格式,此处为未压缩的音频数据 } headers = { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", "X-Appid": APPID, "X-CurTime": str(int(time.time())), "X-Param": base64.b64encode(json.dumps(param).replace(' ', '').encode('utf-8')).decode('utf-8'), "X-CheckSum": hashlib.md5((API_KEY + str(int(time.time())) + base64.b64encode(audio_data).decode('utf-8')) \ .encode('utf-8')).hexdigest(), } response = requests.post(url, headers=headers, data=audio_data) result = json.loads(response.text) if result["code"] == "0": print("识别结果:", result["data"]) else: print("识别失败:", result["desc"]) ``` 语音合成: ```python import requests import json import time import hashlib import base64 url = "http://api.xfyun.cn/v1/service/v1/tts" APPID = "YOUR_APPID" # 替换为自己的APPID API_KEY = "YOUR_API_KEY" # 替换为自己的API_KEY text = "科大讯飞提供全球领先的人工智能技术、产品和服务,致力于让机器具备智能的思维和感官能力。" # 需要合成的文本内容 param = { "auf": "audio/L16;rate=16000", # 音频采样率和编码格式 "aue": "lame", # 音频编码格式,此处使用mp3 "voice_name": "xiaoyan", # 合成的发音人 "speed": "50", # 合成语速 "volume": "50", # 合成音量 "pitch": "50", # 合成音调 "engine_type": "intp65", # 引擎类型 "text": text, # 合成的文本内容 } headers = { "Content-Type": "application/x-www-form-urlencoded; charset=utf-8", "X-Appid": APPID, "X-CurTime": str(int(time.time())), "X-Param": base64.b64encode(json.dumps(param).replace(' ', '').encode('utf-8')).decode('utf-8'), "X-CheckSum": hashlib.md5((API_KEY + str(int(time.time())) + base64.b64encode(text.encode('utf-8')).decode('utf-8')) \ .encode('utf-8')).hexdigest(), } response = requests.post(url, headers=headers) result = json.loads(response.text) if result["code"] == "0": audio_data = base64.b64decode(result["data"]) audio_file = "output.mp3" # 合成的音频保存路径 with open(audio_file, "wb") as f: f.write(audio_data) print("合成成功,音频文件保存在:", audio_file) else: print("合成失败:", result["desc"]) ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

loong_XL

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值