Azure Speech的Python调用代码
Azure配音可写个简单的Python脚本批量调用。核心就三样:Azure Key、地区(如eastasia)、音色名(如zh-CN-XiaoxiaoNeural)。一次请求能转2000字符,想批量就跑循环。
import requests
import time
KEY = "你的Azure语音服务密钥"
REGION = "eastasia"
def text_to_speech(text, output_file, voice="zh-CN-XiaoxiaoNeural"):
url = f"https://{REGION}.tts.speech.microsoft.com/cognitiveservices/v1"
headers = {
"Ocp-Apim-Subscription-Key": KEY,
"Content-Type": "application/ssml+xml",
"X-Microsoft-OutputFormat": "audio-16khz-128kbitrate-mono-mp3"
}
ssml = f"""
"""
resp = requests.post(url, headers=headers, data=ssml.encode('utf-8'))
with open(output_file, 'wb') as f:
f.write(resp.content)
print(f"已生成: {output_file}")
# 使用
text_to_speech("你好,这是微软AI配音的测试。", "output.mp3")
写代码不是必须的。Azure门户里有个"语音工作室"——网页版可视化界面,直接粘贴文本点播放就能预览和下载。对于不会代码的人来说够用了。