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

相关推荐
HappRobot6 分钟前
python类和对象
开发语言·python
盼哥PyAI实验室20 分钟前
Python YAML配置管理:12306项目的灵活配置方案
开发语言·python
Github掘金计划1 小时前
开发者狂喜!GitHub 官方开源:支持 Copilot/Cursor,规范即代码,27k Star 封神!
java·python·kafka·github·copilot
shenzhenNBA1 小时前
python用openpyxl操作excel-单元格样式操作
python·excel·openpyxl·单元格样式
岁月宁静1 小时前
多模态 Agent 技术全景解析 — 从模型能力、Agent 架构到工程化与商业落地
python·agent
试着1 小时前
【VSCode+AI+测试】连接ai大模型
ide·人工智能·vscode·python·学习·编辑器·ai-test
零小陈上(shouhou6668889)1 小时前
YOLOv8+PyQt5海洋船只检测(可以重新训练,yolov8模型,从图像、视频和摄像头三种路径识别检测)
开发语言·python·yolo
znhy_231 小时前
day36打卡
python
gf13211112 小时前
python_字幕文本、音频、视频一键组合
python·音视频·swift
我的xiaodoujiao2 小时前
使用 Python 语言 从 0 到 1 搭建完整 Web UI自动化测试学习系列 32--开源电商商城系统项目实战--如何区分登录状态
python·学习·测试工具·pytest