使用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等视频制作

相关推荐
maxruan1 分钟前
PyTorch学习
人工智能·pytorch·python·学习
唐古乌梁海22 分钟前
【python】在Django中,执行原生SQL查询
python·sql·django
程序员大雄学编程1 小时前
「用Python来学微积分」5. 曲线的极坐标方程
开发语言·python·微积分
一位代码2 小时前
python | requests爬虫如何正确获取网页编码?
开发语言·爬虫·python
可触的未来,发芽的智生2 小时前
新奇特:神经网络速比器,小镇债务清零的算法奇缘
javascript·人工智能·python
mortimer2 小时前
还在被 Windows 路径的大小写和正反斜杠坑?是时候让 pathlib 拯救你的代码了!
人工智能·python
std860212 小时前
Rust 与 Python – 这是未来的语言吗?
开发语言·python·rust
鄃鳕3 小时前
Flask【python】
后端·python·flask
weixin_46683 小时前
Python编程之面向对象
开发语言·人工智能·python
Lynnxiaowen3 小时前
今天我们学习python编程常用模块与面向对象
运维·python·学习·云计算