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

相关推荐
AI 嗯啦30 分钟前
SQL详细语法教程(三)mysql的函数知识
android·开发语言·数据库·python·sql·mysql
databook38 分钟前
把数学对象画出来:Manim Mobject类库速查手册
python·数学·动效
图灵学术计算机论文辅导1 小时前
傅里叶变换+attention机制,深耕深度学习领域
人工智能·python·深度学习·计算机网络·考研·机器学习·计算机视觉
ruleslol2 小时前
python30-正则表达式
python·正则表达式
vincent_hahaha2 小时前
关于simplifyweibo_4_moods数据集的分类问题
python
三年呀3 小时前
**标题:发散创新之力,探索隐私计算的未来**隐私计算,作为当下数字化时代的热门话题,正受
python
R-G-B3 小时前
OpenCV Python——报错AttributeError: module ‘cv2‘ has no attribute ‘bgsegm‘,解决办法
人工智能·python·opencv·opencv python·attributeerror·module ‘cv2‘·no attribute
DavieLau4 小时前
C#项目WCF接口暴露调用及SOAP接口请求测试(Python版)
xml·服务器·开发语言·python·c#
白露与泡影4 小时前
Spring容器初始化源码解析
java·python·spring
码界筑梦坊5 小时前
98-基于Python的网上厨房美食推荐系统
开发语言·python·美食