Google TTS (Text-To-Speech)でテキストを読み上げた音声ファイルをする方法です。
google-tts-apiのインストール
google-tts-apiをインストールします。
npm install google-tts-api --save
音声ファイルのURLを取得する
引数に読み上げる文字列と、言語、読み上げ速度を指定すると、音声ファイルのURLを取得できます。
const googleTTS = require('google-tts-api');
// 読み上げる文字列
const text = 'こんにちは';
// 言語設定
const lang = 'ja-JP';
// 読み上げ速度
const speed = 1;
googleTTS(text, lang, speed).then(function (url) {
console.log(url);
}).catch(function (err) {
console.error(err.stack);
});
表示されたURLからmp3の音声ファイルをダウンロードできます。