当前位置: 首页 > news >正文

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


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

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

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_detailsprint("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日


http://www.lryc.cn/news/212240.html

相关文章:

  • 基于单片机的超声波探伤仪设计
  • idea的设置
  • 高等数学啃书汇总重难点(八)向量代数与空间解析几何
  • C#开发DLL,CAPL调用(CAPL>> .NET DLL)
  • 0-1背包问题【穷举法+二维dp数组】
  • nodejs+vue+python+php基于微信小程序的在线学习平台设计与实现-计算机毕业设计
  • Spring学习笔记2 Spring的入门程序
  • 【Linux】虚拟机安装Linux、客户端工具及Linux常用命令(详细教程)
  • Day 47 动态规划 part13
  • 【广州华锐互动】飞机诊断AR远程指导系统为工程师提供更多支持
  • 【贝叶斯回归】【第 2 部分】--推理算法
  • 【深入浅出汇编语言】寄存器精讲第二期
  • 如何保证分布式情况下的幂等性
  • Mybatis特殊SQL的执行
  • MyBatis-Flex(一):快速开始
  • Vue组件化
  • nodejs+python+php+微信小程序-基于安卓android的健身服务应用APP-计算机毕业设计
  • SpringCloud 微服务全栈体系(九)
  • Mybatis 多对一和一对多查询
  • MySQL的数据库操作、数据类型、表操作
  • 音视频技术开发周刊 | 317
  • 【JavaSE专栏58】“Java构造函数:作用、类型、调用顺序和最佳实践“ ⚙️⏱️
  • Ubuntu系统HUSTOJ 用 vim 修改php.ini 重启PHP服务
  • 案例分析真题-信息安全
  • envi5.3处理高分二号影像数据辐射定标大气校正
  • C语言 结构体
  • frp-内网穿透部署-ubuntu22服务器-windows server-详细教程
  • MySQL内存使用的监控开关和使用查看
  • 数据库管理-第113期 Oracle Exadata 04-硬件选择(20231020)
  • 带着问题去分析:Spring Bean 生命周期 | 京东物流技术团队