zlargon / google-tts

Google TTS (Text-To-Speech) for node.js
https://www.npmjs.com/package/google-tts-api
MIT License
279 stars 56 forks source link

Update for the new TTS API #35

Closed chayleaf closed 3 years ago

chayleaf commented 3 years ago

https://github.com/Boudewijn26/gTTS-token/blob/master/docs/november-2020-translate-changes.md

florabtw commented 3 years ago

It looks like this change went 100% today. All requests are failing for me. Solving this will solve #33 though I don't know how difficult it will be.

freddiefujiwara commented 3 years ago

@ncpierson If you are in a hurry please use the following package instead of this one https://github.com/freddiefujiwara/google-tts/tree/feature/fix-dec-1

in your package.json

"dependencies": {
        ...
        "google-tts-api": "https://github.com/freddiefujiwara/google-tts#feature/fix-dec-1",
       ...
    },
florabtw commented 3 years ago

I was going to contribute to this repo but I decided to just do a rewrite since so much code would have to change: https://github.com/ncpierson/google-translate-tts

Can use with:

yarn add google-translate-tts

then

const tts = require('google-translate-tts');
const buffer = await tts.synthesize({ text: 'Hello, world!', voice: 'en-US' });
fs.writeFileSync('hello-world.mp3', buffer);

It's not perfect, but it's all I needed for soundoftext.com, and I'll keep working on it.

zlargon commented 3 years ago

New Google TTS API has been implemented in version 2.0.0, and there are some breaking changes.

Please see more details in README and CHANGELOG Thanks.

const fs = require('fs');
const googleTTS = require('google-tts-api');

// get base64 text
googleTTS
  .getAudioBase64('Hello World', { lang: 'en-US', slow: false })
  .then((base64) => {
    console.log({ base64 });

    // save the audio file
    const buffer = Buffer.from(base64, 'base64');
    fs.writeFileSync('hello-world-english.mp3', buffer, { encoding: 'base64' });
  })
  .catch(console.error);