Azure长音频API -NODEJS

发布于 2025-02-08 22:41:09 字数 1503 浏览 3 评论 0 原文

我正在尝试向Azure Speech Services API提出帖子请求,以将文本转换为语音。该请求需要发送JSON有效负载和.TXT文件。问题是我正在建立一个nodejs后端,并且文档仅在Python中。

此Python代码的JavaScript等效是什么? (我正在使用node-fetch)

    url = 'some-url'
    header = {
        'Ocp-Apim-Subscription-Key': 'some-key'
    }
    payload = {
        'displayname': 'long audio synthesis sample',
        'description': 'sample description',
        'locale': 'en-US,
        'voices': [{ 'voicename': 'some-voice' }],
        'outputformat': 'riff-16khz-16bit-mono-pcm',
        'concatenateresult': True,
    }

    filename = ntpath.basename('some-path/file.txt')
    files = {
        'script': (filename, open('some-path/file.txt', 'rb'), 'text/plain')
    }

    response = requests.post(url, payload, headers=header, files=files)

已经尝试了以下代码以在请求中包含txt文件,但我在响应“ statustext:'不支持的媒体类型'”中得到了响应。


const   payload = {
            "displayname": 'test',
            "description": 'audio',
            "locale": 'en-US',
            "voices": [{ 'voicename': 'some-voice' }],
            "outputformat": 'riff-16khz-16bit-mono-pcm',
            "concatenateresult": true,
    }

const files = {
            'script': (fs.readFileSync(text), 'text/plain')
    }

await fetch('some-url', {
           method: 'POST',
           files,
           payload: JSON.stringify(payload),
           headers: {
            'Ocp-Apim-Subscription-Key': 'some-key',
            'Content-Type': 'application/json', 
           }
  })

I am trying to make a POST request to Azure speech services API to convert text to speech. The request needs to send a JSON payload and a .txt file. The thing is that I am building a NodeJS backend and the documentation is only in Python.

What would be the JavaScript equivalent for this Python code? (I am using node-fetch)

    url = 'some-url'
    header = {
        'Ocp-Apim-Subscription-Key': 'some-key'
    }
    payload = {
        'displayname': 'long audio synthesis sample',
        'description': 'sample description',
        'locale': 'en-US,
        'voices': [{ 'voicename': 'some-voice' }],
        'outputformat': 'riff-16khz-16bit-mono-pcm',
        'concatenateresult': True,
    }

    filename = ntpath.basename('some-path/file.txt')
    files = {
        'script': (filename, open('some-path/file.txt', 'rb'), 'text/plain')
    }

    response = requests.post(url, payload, headers=header, files=files)

Already tried the following code to include the txt file in the request but I got in response "statusText: 'Unsupported Media Type'"


const   payload = {
            "displayname": 'test',
            "description": 'audio',
            "locale": 'en-US',
            "voices": [{ 'voicename': 'some-voice' }],
            "outputformat": 'riff-16khz-16bit-mono-pcm',
            "concatenateresult": true,
    }

const files = {
            'script': (fs.readFileSync(text), 'text/plain')
    }

await fetch('some-url', {
           method: 'POST',
           files,
           payload: JSON.stringify(payload),
           headers: {
            'Ocp-Apim-Subscription-Key': 'some-key',
            'Content-Type': 'application/json', 
           }
  })

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小傻瓜 2025-02-15 22:41:10

此Python代码的JavaScript等效是什么? (我正在使用node-fetch)

您可以根据文档,在邮政请求中使用 json.stringify()有效载荷转换为json格式化字符串。

   let payload = {
        'displayname': 'long audio synthesis sample',
        'description': 'sample description',
        'locale': 'en-US,
        'voices': [{ 'voicename': 'some-voice' }],
        'outputformat': 'riff-16khz-16bit-mono-pcm',
        'concatenateresult': True,
    };

fetch(''https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis', {
    method: 'POST',
    body: JSON.stringify(payload),
    headers: { 'Content-Type': 'application/json' }
}).then(res => res.json())
  .then(json => console.log(json));

您可以参考带有邮政请求的节点 - fetch问题“ https://coderrocketfuel.com/article/make-an-http-post-post-request-with-node-node-node-fetch-and-node-node-node-js” rel =“ nofollow noreferrer”>与node-fetch&amp&amp&ampshe ; node.js

What would be the JavaScript equivalent for this Python code? (I am using node-fetch)

You can try the following code, based on the documentation, use JSON.stringify() in POST request to convert payload to JSON formatted string.

   let payload = {
        'displayname': 'long audio synthesis sample',
        'description': 'sample description',
        'locale': 'en-US,
        'voices': [{ 'voicename': 'some-voice' }],
        'outputformat': 'riff-16khz-16bit-mono-pcm',
        'concatenateresult': True,
    };

fetch(''https://{}.customvoice.api.speech.microsoft.com/api/texttospeech/v3.0/longaudiosynthesis', {
    method: 'POST',
    body: JSON.stringify(payload),
    headers: { 'Content-Type': 'application/json' }
}).then(res => res.json())
  .then(json => console.log(json));

You can refer to Node-fetch problems with POST requests, Make an HTTP POST Request With Node-Fetch & Node.js and LongRunning - cognitive-services-speech-sdk-js

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文