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 ) ;更多例子
麻省理工學院