1 | # google-tts
|
2 |
|
3 | Google TTS (Text-To-Speech) for node.js
|
4 |
|
5 | [![][npm-img]][npm-url]
|
6 |
|
7 | [![][dependency-img]][dependency-url]
|
8 | [![][dependency-dev-img]][dependency-dev-url]
|
9 | [![][install-size-img]][install-size-result]
|
10 |
|
11 | [![][travis-img]][travis-url]
|
12 | [![][coverage-img]][coverage-url]
|
13 |
|
14 | ## Installation
|
15 |
|
16 | ```bash
|
17 | $ npm install --save google-tts-api
|
18 | $ npm install -D typescript @types/node # Only for TypeScript
|
19 | ```
|
20 |
|
21 | ## Change Log
|
22 |
|
23 | Please see [CHANGELOG](https://github.com/zlargon/google-tts/blob/master/CHANGELOG.md).
|
24 |
|
25 | ## Usage
|
26 |
|
27 | | Method | Options (all optional) | Return Type | Handle Long Text |
|
28 | | ------------------- | ----------------------------------------------- | --------------------------------------------------- | :--------------: |
|
29 | | `getAudioUrl` | `lang`, `slow`, `host` | `string` | |
|
30 | | `getAudioBase64` | `lang`, `slow`, `host`, `timeout` | `Promise<string>` | |
|
31 | | `getAllAudioUrls` | `lang`, `slow`, `host`, `splitPunct` | `{ shortText: string; url: string; }[]` | ✅ |
|
32 | | `getAllAudioBase64` | `lang`, `slow`, `host`, `timeout`, `splitPunct` | `Promise<{ shortText: string; base64: string; }[]>` | ✅ |
|
33 |
|
34 | ### Options (all optional)
|
35 |
|
36 | | Option | Type | Default | Description |
|
37 | | ------------ | --------- | ---------------------------- | ------------------------------------------------------------------------------------------------------------------------------ |
|
38 | | `lang` | `string` | en | See all avaiable language code at https://cloud.google.com/speech/docs/languages |
|
39 | | `slow` | `boolean` | false | Use the slow audio speed if set `slow` to `true` |
|
40 | | `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). |
|
41 | | `timeout` | `number` | 10000 (ms) | (Only for `getAudioBase64` and `getAllAudioBase64`) Set timeout for the HTTP request. |
|
42 | | `splitPunct` | `string` | | (Only for `getAllAudioUrls` and `getAllAudioBase64`) Set the punctuation to split the long text to short text. (e.g. ",、。") |
|
43 |
|
44 | ## Examples
|
45 |
|
46 | ### 1. `getAudioUrl(text, [option])`
|
47 |
|
48 | ```js
|
49 | import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
|
50 | const googleTTS = require('google-tts-api'); // CommonJS
|
51 |
|
52 | // get audio URL
|
53 | const url = googleTTS.getAudioUrl('Hello World', {
|
54 | lang: 'en',
|
55 | slow: false,
|
56 | host: 'https://translate.google.com',
|
57 | });
|
58 | console.log(url); // https://translate.google.com/translate_tts?...
|
59 | ```
|
60 |
|
61 | ### 2. `getAudioBase64(text, [option])`
|
62 |
|
63 | ```js
|
64 | import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
|
65 | const googleTTS = require('google-tts-api'); // CommonJS
|
66 |
|
67 | // get base64 text
|
68 | googleTTS
|
69 | .getAudioBase64('Hello World', {
|
70 | lang: 'en',
|
71 | slow: false,
|
72 | host: 'https://translate.google.com',
|
73 | timeout: 10000,
|
74 | })
|
75 | .then(console.log) // base64 text
|
76 | .catch(console.error);
|
77 | ```
|
78 |
|
79 | ### 3. `getAllAudioUrls(text, [option])` (For text longer than 200 characters)
|
80 |
|
81 | ```js
|
82 | import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
|
83 | const googleTTS = require('google-tts-api'); // CommonJS
|
84 |
|
85 | const results = googleTTS.getAllAudioUrls('LONG_TEXT_...', {
|
86 | lang: 'en',
|
87 | slow: false,
|
88 | host: 'https://translate.google.com',
|
89 | splitPunct: ',.?',
|
90 | });
|
91 | console.log(results);
|
92 | // [
|
93 | // { shortText: '...', url: '...' },
|
94 | // { shortText: '...', url: '...' },
|
95 | // ...
|
96 | // ];
|
97 | ```
|
98 |
|
99 | ### 4. `getAllAudioBase64(text, [option])` (For text longer than 200 characters)
|
100 |
|
101 | ```js
|
102 | import * as googleTTS from 'google-tts-api'; // ES6 or TypeScript
|
103 | const googleTTS = require('google-tts-api'); // CommonJS
|
104 |
|
105 | googleTTS
|
106 | .getAllAudioBase64('LONG_TEXT_...', {
|
107 | lang: 'en',
|
108 | slow: false,
|
109 | host: 'https://translate.google.com',
|
110 | timeout: 10000,
|
111 | splitPunct: ',.?',
|
112 | })
|
113 | .then(console.log)
|
114 | // [
|
115 | // { shortText: '...', base64: '...' },
|
116 | // { shortText: '...', base64: '...' },
|
117 | // ...
|
118 | // ];
|
119 | .catch(console.error);
|
120 | ```
|
121 |
|
122 | [More Examples](https://github.com/zlargon/google-tts/tree/master/example)
|
123 |
|
124 | ## License
|
125 |
|
126 | MIT
|
127 |
|
128 | [npm-url]: https://nodei.co/npm/google-tts-api
|
129 | [npm-img]: https://nodei.co/npm/google-tts-api.png
|
130 | [install-size-img]: https://packagephobia.com/badge?p=google-tts-api
|
131 | [install-size-result]: https://packagephobia.com/result?p=google-tts-api
|
132 | [dependency-url]: https://david-dm.org/zlargon/google-tts
|
133 | [dependency-img]: https://img.shields.io/david/zlargon/google-tts.svg
|
134 | [dependency-dev-url]: https://david-dm.org/zlargon/google-tts#info=devDependencies
|
135 | [dependency-dev-img]: https://img.shields.io/david/dev/zlargon/google-tts.svg
|
136 | [travis-url]: https://travis-ci.com/zlargon/google-tts
|
137 | [travis-img]: https://img.shields.io/travis/com/zlargon/google-tts
|
138 | [coverage-url]: https://coveralls.io/github/zlargon/google-tts
|
139 | [coverage-img]: https://img.shields.io/coveralls/github/zlargon/google-tts
|