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

相关推荐
小宁爱Python1 分钟前
基于 Django+Vue3 的 AI 海报生成平台开发(海报模块专项)
人工智能·python·django
红豆怪怪8 分钟前
[LeetCode 热题 100] 32. 最长有效括号
数据结构·python·算法·leetcode·动态规划·代理模式
大嘴带你水论文1 小时前
震惊!仅用10张照片就能随意编辑3D人脸?韩国KAIST最新黑科技FFaceNeRF解析!
论文阅读·人工智能·python·科技·计算机视觉·3d·transformer
CodeCraft Studio1 小时前
国产化PDF处理控件Spire.PDF教程:如何在 Java 中通过模板生成 PDF
java·python·pdf·spire.pdf·java创建pdf·从html创建pdf
摆烂z2 小时前
Jupyter Notebook的交互式开发环境方便py开发
ide·python·jupyter
一乐小哥3 小时前
一口气同步10年豆瓣记录———豆瓣书影音同步 Notion分享 🚀
后端·python
华研前沿标杆游学3 小时前
华为在国内搞的研发基地有多野?标杆游学带你解锁“研发界顶流”
python
小胖墩有点瘦3 小时前
【基于深度学习的中草药识别系统】
人工智能·python·深度学习·课程设计·计算机毕业设计·中草药识别
正在走向自律4 小时前
Ubuntu系统下Python连接国产KingbaseES数据库实现增删改查
开发语言·数据库·python·ubuntu·kingbasees·ksycopg2
Calihen的学习日志4 小时前
【Pandas】3.1-数据预处理:列的基本操作
python·pandas