Important: This documentation covers Yarn 1 (Classic).
For Yarn 2+ docs and migration guide, see yarnpkg.com.

Package detail

google-tts-api

zlargon222.9kMIT2.0.2TypeScript support: included

Google TTS (Text-To-Speech) for node.js

google, google-tts, text-to-speech

readme

google-tts

Google TTS (Text-To-Speech) for node.js

Installation

$ npm install --save google-tts-api
$ npm install -D typescript @types/node # Only for TypeScript

Change Log

Please see CHANGELOG.

Usage

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; }[]>

Options (all optional)

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 slow to true
host string https://translate.google.com You can change the host if the default host could not work in your region (e.g. https://translate.google.com.cn).
timeout number 10000 (ms) (Only for getAudioBase64 and getAllAudioBase64) Set timeout for the HTTP request.
splitPunct string | (Only for getAllAudioUrls and getAllAudioBase64) Set the punctuation to split the long text to short text. (e.g. ",、。")

Examples

1. getAudioUrl(text, [option])

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?...

2. getAudioBase64(text, [option])

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);

3. getAllAudioUrls(text, [option]) (For text longer than 200 characters)

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: '...' },
//   ...
// ];

4. getAllAudioBase64(text, [option]) (For text longer than 200 characters)

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);

More Examples

License

MIT

changelog

2.0.2 (Mar 20, 2021)

  • Change default language to en (#45)
  • Typescript: Remove type Language since API doesn't fully support language codes listed in the document

2.0.1 (Jan 6, 2021)

  • Fix the vulnerabilities by upgrading the dependencies (#42, #44)

2.0.0 (Dec 8, 2020)

  • Add new APIs (Please see the Break Change below)

    | Method | Options (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; }[]> | ✅ |

  • Support new Google TTS API to get audio Base64 text (#35)

  • Support long text input: getAllAudioUrls and getAllAudioBase64 (#30)
  • Support changing the host in option (#16)
  • Support Typescript
  • Add dependency axios

Break Change from 0.x.x to 2.x.x

googleTTS() is changed to googleTTS.getAudioUrl().

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

// Before version 0.0.6
// Original googleTTS is a promise function
const url = await googleTTS('Hello World', 'en', 1);

// After version 2.0.0
// Now googleTTS is an object with 4 new methods (getAudioUrl, getAudioBase64, getAllAudioUrls, getAllAudioBase64)
// googleTTS.getAudioUrl is a non-promise function
const url = googleTTS.getAudioUrl('Hello World', {
  lang: 'en-US',
  slow: false, // speed (number) is changed to slow (boolean)
  host: 'https://translate.google.com', // allow to change the host
});

0.0.6 (Dec 5, 2020)

  • timeout parameter is deprecated.
  • Remove dependency isomorphic-fetch.
  • Fix the change of Google Translate API (@freddiefujiwara in #37). Read more in #35

0.0.5 (Nov 8, 2020)

  • Upgrade the dependencies and fix the vulnerability. (#32)
  • Add retry mechanism to prevent fetching token key failed too frequently. (#33)

0.0.4 (Nov 29, 2018)

0.0.3 (Sep 21, 2018)

  • Add package-lock.lock file
  • Fix the change of Google Translate API (@ncpierson in #14)

0.0.2 (Aug 25, 2017)

  • Add yarn.lock file
  • If length of input text is over than 200 characters, throw a RangeError with error messsage. (#5)