交通局网站建设整改,携程网站官网,网站维护的过程及方法,外贸网站建站赚钱标签#xff1a; 文本转音频并保存mp3文件#xff1b; 微软Azure#xff1b; 微软Azure可以将文本转音频#xff0c;并保存mp3文件#xff0c;直接上代码 代码格式#xff1a;python 3
import os
import azure.cognitiveservices.speech as speechsdk# This example re… 标签 文本转音频并保存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(subscriptionos.environ.get(SPEECH_KEY), regionos.environ.get(SPEECH_REGION))# The language of the voice that speaks.
speech_config.speech_synthesis_voice_namezh-CN-YunjianNeural # 这个男声 有 磁性
text 讲一个笑话和朋友去饭店吃饭要了一盘红烧肉结果发现怎么咬都咬不动我顿时就火了把服务员叫过来喊道你们这肉怎么咬都咬不动把你们经理叫来。服务员说叫我们经理干啥啊你都咬不动他能咬得动啊
speech_config.set_speech_synthesis_output_format(speechsdk.SpeechSynthesisOutputFormat.Audio24Khz160KBitRateMonoMp3) # 这里配置文件为mp3格式要保存其它文件格式修改这里参数
speech_synthesizer speechsdk.SpeechSynthesizer(speech_configspeech_config, audio_configNone)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?tabswindows%2Cterminalpivotsprogramming-language-javascript#prerequisites 2python 要求3.7以上。 参考
微软Azure Python 示例代码 https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/get-started-text-to-speech?tabswindows%2Cterminalpivotsprogramming-language-python#prerequisites 支持不同的语音和声音形式男声、女声 https://learn.microsoft.com/zh-cn/azure/ai-services/speech-service/language-support?tabstts 作者简介https://shimo.im/docs/rp3OVwxle2fJn7Am/ 上海徐汇 2023年10月29日