google tts
1.0.0
node.js的Google TTS(文本到语音)
$ npm install --save google-tts-api
$ npm install -D typescript @types/node # Only for TypeScript 请参阅ChangElog。
| 方法 | 选项(所有可选) | 返回类型 | 处理长文字 |
|---|---|---|---|
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; }[]> | ✅ |
| 选项 | 类型 | 默认 | 描述 |
|---|---|---|---|
lang | string | en | 在https://cloud.google.com/speech/docs/languages上查看所有可行的语言代码 |
slow | boolean | 错误的 | 如果设置slow到true ,请使用缓慢的音频速度 |
host | string | https://translate.google.com | 如果默认主机无法在您的地区工作(例如https://translate.google.com.cn),则可以更改host 。 |
timeout | number | 10000(MS) | (仅适用于getAudioBase64和getAllAudioBase64 )为HTTP请求设置超时。 |
splitPunct | string | (仅适用于getAllAudioUrls和getAllAudioBase64 )设置标点符号以将长文本分为短文本。 (例如“,,、。”) |
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?...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 ) ;getAllAudioUrls(text, [option]) (对于超过200个字符的文本) 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: '...' },
// ...
// ];getAllAudioBase64(text, [option]) (对于超过200个字符的文本) 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 ) ;更多例子
麻省理工学院