使用Azure合成语音,解放嘴巴(bushi)

首先去微软申请以下azure语音的api

不同的语音风格可在参考此处:
https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/language-support?tabs=tts

pip install azure-cognitiveservices-speech

python 复制代码
import azure.cognitiveservices.speech as speechsdk
import os
# 设置你的Azure语音服务的密钥和区域
speech_key = "xxx"
service_region = "eastasia"

def synthesize_speech(text):
    output_dir = "./speech"
    os.makedirs(output_dir, exist_ok=True)
    
    # 生成文件路径
    output_file = os.path.join(output_dir, f"{text}.mp3")
    # 创建语音配置对象
    speech_config = speechsdk.SpeechConfig(subscription=speech_key, region=service_region)
    speech_config.speech_synthesis_voice_name = "zh-CN-YunjianNeural" # 选择不同的角色
    
    # 设置输出音频文件
    audio_output = speechsdk.audio.AudioOutputConfig(filename=output_file)
    
    # 创建语音合成器对象
    speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_output)
    
    # 合成语音
    result = speech_synthesizer.speak_text_async(text).get()
    
    # 检查结果
    if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
        print(f"语音合成成功,已保存至 {output_file}")
    elif result.reason == speechsdk.ResultReason.Canceled:
        cancellation_details = result.cancellation_details
        print(f"语音合成失败: {cancellation_details.reason}")
        if cancellation_details.reason == speechsdk.CancellationReason.Error:
            print(f"错误详情: {cancellation_details.error_details}")
            
    return f"./speech/{text}.mp3"

这样就生成mp3文件。

可用于manim, janim等视频制作

相关推荐
F_D_Z4 小时前
【解决办法】报错Found dtype Long but expected Float
人工智能·python
fanjinhong_85215 小时前
屏幕捕捉工具 (Screen Capture Tool)
python·github
避避风港6 小时前
Java 抽象类
java·开发语言·python
武子康7 小时前
Java-168 Neo4j CQL 实战:WHERE、DELETE/DETACH、SET、排序与分页
java·开发语言·数据库·python·sql·nosql·neo4j
天地之于壹炁兮7 小时前
元素认证的智慧:Python分支魔法的艺术与禁忌
windows·python
闲人编程7 小时前
Python与大数据:使用PySpark处理海量数据
大数据·开发语言·分布式·python·spark·codecapsule·大规模
daqinzl8 小时前
Ubuntu 使用 Python 启动 HTTP 服务
python·ubuntu·http server 服务
qunshankeji8 小时前
交通事故自动识别_YOLO11分割_DRB实现
python
小呀小萝卜儿8 小时前
2025-11-14 学习记录--Python-特征归一化方法(Min-Max或StandardScaler)
开发语言·python·学习
顾安r8 小时前
11.14 脚本网页 青蛙过河
服务器·前端·python·游戏·html