Home>

I am using Watson Text-to-Speech in Python.

import requests
url = "https://stream.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_EmiV3Voice"
auth = ("apikey", "*******************************")
headers = {
        "Content-Type": "audio/mp3"
}
params = {
        "text": "Aiueo"
}
r = requests.post (url, auth = auth, params = params)


In this way I was able to get garbled characters that I do not understand well.

�% N�5O��A (`ި z �U f�H $q * 6�Ug50) �� [�% T��<��� �� ^ �a�� إ 4} o�x��� ��F_���a�� g��V7��O! ���<9���8��ԇ��� j�w/��eE3h�̹����Ί 杤 ���� {3�K���Z���X�.%] � ��, ������k�F4� JS�lC����0�} @ �% ��F�9�� z: ���� Western g����m�Wc����V�!;%
;��`��M������za����w���l�] ��&�� ժ!>�- X�<�Q�? "�h6� @ Os # � : �L� [: WK���kK5W�X���v�'Z��C ݂ j | ��� | �B�U3�fJj�N��

Encoding? Decoding? I think I have to do it, but I don't know how to do it.
I want to make this an mp3 file, what should I do?

Python 3.8.0
Latest Watson TTS
Windows10 Pro

  • Answer # 1

    I tried it because it looks interesting.

    According to https://cloud.ibm.com/docs/services/text-to-speech?topic=text-to-speech-audioFormats&locale=en

    The default format isaudio/ogg;codecs = opus

    If you want to specify the format, specify it in theAcceptrequest header oracceptquery parameter

    Since it was

    , when I tried it like this, mp3 data was returned.

    import requests
    # The URL is slightly different. I wonder what if
    url = "https://gateway-tok.watsonplatform.net/text-to-speech/api/v1/synthesize?voice=en-US_EmiV3Voice"
    auth = ("apikey", "*******************************")
    # headers = {
    # "Content-Type": "audio/mp3"
    #} Erase because I don't use it
    params = {
            "accept": "audio/mp3", # added this
            "text": "Aiueo"
    }
    r = requests.post (url, auth = auth, params = params)
    # Store in binary file
    with open ('EmiV3Voice_aiueo.mp3', mode = 'wb') as f:
        f.write (r.content)