微软Azure文本转音频,保存成MP3文件【代码python3】


标签: 文本转音频并保存mp3文件; 微软Azure;


微软Azure可以将文本转音频,并保存mp3文件,直接上代码

代码格式:python 3

python 复制代码
import os
import azure.cognitiveservices.speech as speechsdk

# This example requires environment variables named "SPEECH_KEY" and "SPEECH_REGION"
speech_config = speechsdk.SpeechConfig(subscription=os.environ.get('SPEECH_KEY'), region=os.environ.get('SPEECH_REGION'))

# The language of the voice that speaks.
speech_config.speech_synthesis_voice_name='zh-CN-YunjianNeural'   # 这个男声 有 磁性
text = "讲一个笑话:和朋友去饭店吃饭,要了一盘红烧肉,结果发现怎么咬都咬不动,我顿时就火了,把服务员叫过来喊道:你们这肉怎么咬都咬不动,把你们经理叫来。服务员说:叫我们经理干啥啊,你都咬不动,他能咬得动啊!"
speech_config.set_speech_synthesis_output_format(speechsdk.SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3) # 这里配置文件为mp3格式,要保存其它文件格式,修改这里参数
speech_synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=None)

result = speech_synthesizer.speak_text_async(text).get()
stream = speechsdk.AudioDataStream(result)
stream.save_to_wav_file("D:/file.mp3")  # mp3文件保存路径


if result.reason == speechsdk.ResultReason.SynthesizingAudioCompleted:
    print("Speech synthesized Completed,  for text [{}]".format(text))
elif result.reason == speechsdk.ResultReason.Canceled:
    cancellation_details = result.cancellation_details
    print("Speech synthesis canceled: {}".format(cancellation_details.reason))
    if cancellation_details.reason == speechsdk.CancellationReason.Error:
        if cancellation_details.error_details:
            print("Error details: {}".format(cancellation_details.error_details))
            print("Did you set the speech resource key and region values?")

注意:

1,代码运行前,SPEECH_KEY和SPEECH_REGION已生成好,并保存在你的环境变量中。 参考:https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/get-started-text-to-speech?tabs=windows%2Cterminal&pivots=programming-language-javascript#prerequisites

2,python 要求3.7以上。

参考:

微软Azure Python 示例代码
https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/get-started-text-to-speech?tabs=windows%2Cterminal&pivots=programming-language-python#prerequisites

支持不同的语音和声音形式(男声、女声)
https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/language-support?tabs=tts


作者简介:https://shimo.im/docs/rp3OVwxle2fJn7Am/

上海徐汇

2023年10月29日


相关推荐
mit6.82411 小时前
[nanoGPT] ChatGPT 的 LLM 的全栈实现 | 快速上手
python
DKunYu11 小时前
2.1线性回归
pytorch·python·深度学习·1024程序员节
大飞记Python11 小时前
实战分享:一键自动化下载指定版本的Chrome及Chromedriver(附Python源码)
chrome·python·自动化
程序员杰哥13 小时前
如何使用Postman做接口自动化测试及完美的可视化报告?
自动化测试·软件测试·python·测试工具·jenkins·postman·1024程序员节
老歌老听老掉牙13 小时前
参数曲线切向量与叉乘向量的精确计算与分析
python·sympy·1024程序员节
Cherry Zack13 小时前
FastAPI 入门指南 :基础概念与核心特性
开发语言·python·fastapi·1024程序员节
言德斐14 小时前
Python Web框架深度对比:Django vs Flask vs FastAPI(含优缺点与选型策略)
前端·python·django
开心-开心急了14 小时前
Flask入门教程——李辉 第5章: 数据库 关键知识梳理
笔记·后端·python·flask·1024程序员节
CodeCraft Studio15 小时前
国产化Excel开发组件Spire.XLS教程:在Python中将Pandas DataFrame导出到Excel的详细教程
python·excel·pandas
B站_计算机毕业设计之家16 小时前
python舆情分析可视化系统 情感分析 微博 爬虫 scrapy爬虫技术 朴素贝叶斯分类算法大数据 计算机✅
大数据·爬虫·python·scrapy·数据分析·1024程序员节·舆情分析