获取edge-tts的voice列表的python代码
时间: 2024-12-22 12:18:53 浏览: 15
获取阿里云边缘语音服务(Edge TTS,Text-to-Speech)的语音列表通常涉及调用API并解析返回的数据。阿里云的语音库需要通过官方SDK来访问。以下是一个简单的Python示例,使用了阿里云的`aliyun-python-sdk-core`和`aliyun-python-sdk-ess`库:
```python
import json
from aliyunsdkcore.client import AcsClient
from aliyunsdkecss.request.v20200819.GetVoicesRequest import GetVoicesRequest
# 初始化AcsClient,替换为您自己的AccessKeyId、AccessKeySecret和RegionId
access_key_id = 'your_access_key_id'
access_key_secret = 'your_access_key_secret'
region_id = 'your_region_id'
client = AcsClient(
access_key_id,
access_key_secret,
region_id
)
def get_voice_list():
# 创建GetVoices请求对象
request = GetVoicesRequest()
try:
# 发起请求
response = client.do_action_with_exception(request)
# 解析JSON响应
result = json.loads(response)
voices = result.get('Voices', [])
# 返回语音列表
return voices
except Exception as e:
print(f"Error occurred: {e}")
return []
voices = get_voice_list()
for voice in voices:
print(f"{voice['VoiceCode']} - {voice['Language']}")
阅读全文