Google TTS (Text-To-Speech) for node.js
$ npm install --save google-tts-api
$ npm install -D typescript @types/node # Only for TypeScriptPlease see CHANGELOG.
| Method | Options (all optional) | Return Type | Handle Long Text | 
|---|---|---|---|
| getAudioUrl | lang,slow,host | string | |
| getAudioBase64 | lang,slow,host,timeout | Promise<string> | |
| getAllAudioUrls | lang,slow,host,splitPunct | { shortText: string; url: string; }[] | ✅ | 
| getAllAudioBase64 | lang,slow,host,timeout,splitPunct | Promise<{ shortText: string; base64: string; }[]> | ✅ | 
| Option | Type | Default | Description | 
|---|---|---|---|
| lang | string | en | See all avaiable language code at https://cloud.google.com/speech/docs/languages | 
| slow | boolean | false | Use the slow audio speed if set slowtotrue | 
| host | string | https://translate.google.com | You can change the hostif the default host could not work in your region (e.g. https://translate.google.com.cn). | 
| timeout | number | 10000 (ms) | (Only for getAudioBase64andgetAllAudioBase64) Set timeout for the HTTP request. | 
| splitPunct | string | (Only for getAllAudioUrlsandgetAllAudioBase64) Set the punctuation to split the long text to short text. (e.g. ",、。") | 
import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
const googleTTS = require('google-tts-api'); // CommonJS
// get audio URL
const url = googleTTS.getAudioUrl('Hello World', {
  lang: 'en',
  slow: false,
  host: 'https://translate.google.com',
});
console.log(url); // https://translate.google.com/translate_tts?...import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
const googleTTS = require('google-tts-api'); // CommonJS
// get base64 text
googleTTS
  .getAudioBase64('Hello World', {
    lang: 'en',
    slow: false,
    host: 'https://translate.google.com',
    timeout: 10000,
  })
  .then(console.log) // base64 text
  .catch(console.error);import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
const googleTTS = require('google-tts-api'); // CommonJS
const results = googleTTS.getAllAudioUrls('LONG_TEXT_...', {
  lang: 'en',
  slow: false,
  host: 'https://translate.google.com',
  splitPunct: ',.?',
});
console.log(results);
// [
//   { shortText: '...', url: '...' },
//   { shortText: '...', url: '...' },
//   ...
// ];import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
const googleTTS = require('google-tts-api'); // CommonJS
googleTTS
  .getAllAudioBase64('LONG_TEXT_...', {
    lang: 'en',
    slow: false,
    host: 'https://translate.google.com',
    timeout: 10000,
    splitPunct: ',.?',
  })
  .then(console.log)
  // [
  //   { shortText: '...', base64: '...' },
  //   { shortText: '...', base64: '...' },
  //   ...
  // ];
  .catch(console.error);MIT
