简体   繁体   中英

Writing a Text to Speech file as an MP3 - IBM Watson

I'm following the documentation for the Node.JS implementation of the IBM Watson Text-to-Speech API.

I want to output the resultant file into MP3 format. The documentation recommends augmenting the base code but I'm not sure how to do that. My code is rendering unplayable MP3 s.

Here is what it says in the documentation:

textToSpeech.synthesize(synthesizeParams)
  .then(response => {

    // The following line is necessary only for
    // wav formats; otherwise, `response.result`
    // can be directly piped to a file.

    return textToSpeech.repairWavHeaderStream(response.result);
  })
  .then(buffer => {
    fs.writeFileSync('hello_world.wav', buffer);
  })
  .catch(err => {
    console.log('error:', err);
  });

As it says, response.result should be directly piped to a file. This is one of my many attempts (that renders an error).

textToSpeech
  .synthesize(synthesizeParams)  
  .then(response => {
    fs.writeFileSync('Hello.mp3', response.result)
  })
  .catch(err => {
    console.log('error:', err)
  })

How can I output the text-to-speech input as an MP3 ?

Provided your params are requesting an mp3 file, this will be the accept parameter, then your code looks ok. So if the output file isn't being recognised as an audio, then it is most likely a text file containing an error message. This error message will indicate what is wrong, which most likely will be an unauthorised message.

I take it that your catch error block isn't logging anything.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM