【问题标题】:How do I generate an mp3 file using Python Azure text to speech api如何使用 Python Azure 文本转语音 api 生成 mp3 文件
【发布时间】:2022-01-08 02:42:54
【问题描述】:

我可以使用下面的代码生成“Mary had a little lamb”的 wav 文件。但是当我尝试生成 mp3 时它失败了

#https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-text-to-speech?tabs=script%2Cwindowsinstall&pivots=programming-language-python

import azure.cognitiveservices.speech as speechsdk

languageCode = 'en-US'
ssmlGender = 'MALE'
voicName = 'en-US-JennyNeural'
speakingRate = '-5%'
pitch = '-10%'
voiceStyle = 'newscast'

azureKey = 'FAKE KEY'
azureRegion = 'FAKE REGION'

#############################################################
#audioOuputFile = './audioFiles/test.wav'
audioOuputFile = './audioFiles/test.mp3'
#############################################################

txt = 'Mary had a little lamb it\'s fleece was white as snow.'
txt+= 'And everywhere that Mary went, the lamb was sure to go,'
txt+= 'It followed her to school one day,'
txt+= 'That was against the rule,'
txt+= 'It made the children laugh and play,'
txt+= 'To see a lamb at school.'

head1 = f'<speak xmlns="http://www.w3.org/2001/10/synthesis" xmlns:mstts="http://www.w3.org/2001/mstts" xmlns:emo="http://www.w3.org/2009/10/emotionml" version="1.0" xml:lang="{languageCode}">'
head2 = f'<voice name="{voicName}">'
head3 =f'<mstts:express-as style="{voiceStyle}">'
head4 = f'<prosody rate="{speakingRate}" pitch="{pitch}">'
tail= '</prosody></mstts:express-as></voice></speak>'

ssml = head1 + head2 + head3 + head4 + txt + tail
print('this is the ssml======================================')
print(ssml)
print('end ssml======================================')
print()

speech_config = speechsdk.SpeechConfig(subscription=azureKey, region=azureRegion)
audio_config = speechsdk.AudioConfig(filename=audioOuputFile)

#HERE IS THE PROBLEM
#Without this statement everything works fine
#Can produce a wav file 
speech_config.set_speech_synthesis_output_format(SpeechSynthesisOutputFormat["Audio16Khz128KBitRateMonoMp3"])

synthesizer = speechsdk.SpeechSynthesizer(speech_config=speech_config, audio_config=audio_config)
synthesizer.speak_ssml_async(ssml)

这是控制台输出:

(envo) D:\py_new\tts>python ttsTest3.py 这是ssml======================================= 玛丽有一只小羊羔,它的羊毛像雪一样洁白。玛丽走到哪里,羊羔肯定会去,有一天它跟着她去学校,那是违反规定的,让孩子们笑着玩耍,在学校看到一只小羊。 结束ssml=======================================

Traceback(最近一次调用最后一次): 文件“D:\py_new\tts\ttsTest3.py”,第 45 行,在 Speech_config.set_speech_synthesis_output_format(SpeechSynthesisOutputFormat[“Audio16Khz128KBitRateMonoMp3”]) NameError:未定义名称“SpeechSynthesisOutputFormat”

(envo) D:\py_new\tts>

注意错误: NameError:未定义名称“SpeechSynthesisOutputFormat”

比较: 自定义音频格式

在:

https://docs.microsoft.com/en-us/azure/cognitive-services/speech-service/get-started-text-to-speech?tabs=script%2Cwindowsinstall&pivots=programming-language-python

在 Nodejs 中一切正常。但我也需要能够在 Python 中做到这一点。

【问题讨论】:

    标签: python azure mp3 text-to-speech


    【解决方案1】:

    你需要像这样配置你的音频

    def hindi_text_to_speech_azure(hindi_text):
        speech_config = SpeechConfig(subscription=SPEECH_KEY, region=LOCATION_AREA)
        # Note: if only language is set, the default voice of that language is chosen.
        speech_config.speech_synthesis_language = LANGUAGE_LOCATION_HINDI  # e.g. "de-DE"
        # The voice setting will overwrite language setting.
        # The voice setting will not overwrite the voice element in input SSML.
        speech_config.speech_synthesis_voice_name = MALE_VOICE_NAME_HINDI
    
        audio_config = AudioOutputConfig(
        filename="{name}.mp3".format(name=hindi_text[:30]))
    
        synthesizer = SpeechSynthesizer(
        speech_config=speech_config, audio_config=audio_config)
        synthesizer.speak_text_async(hindi_text)
    

    试试这个。

    但问题是实际上这不是问题,但我坚持将文件保存在本地,但我想在本地存储中即时将其上传到服务器(默认存储)。你知道吗?

    【讨论】:

    • 嗨 Rohit,当你这样做时,你会得到一个带有 mp3 扩展名的文件。但它实际上是一个wav文件。当然,波形文件的问题在于它占用了太多带宽。我想我可能只是使用 ffmpeg 将输出转换为客户想要的任何格式。也许是OGG。
    • 我不知道。谢谢
    • @user3567761 你有没有得到任何答案我还需要我的音频操作也太大了只有 7 秒音频消耗 300kb
    • 嗨罗希特。我走了一条不同的路。我生成 wav 文件,然后使用 ffmpeg 将它们转换为 mp3。对于我运作良好的特定工作流程。
    【解决方案2】:

    试试这个

    speech_config.set_speech_synthesis_output_format(speechsdk.SpeechSynthesisOutputFormat.Audio16Khz32KBitRateMonoMp3)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-12-28
      • 1970-01-01
      • 2018-04-30
      • 2013-03-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-03
      相关资源
      最近更新 更多