This article describes how to convert text to speech with Cloud Text-to-Speech from Google Cloud Platform (GCP) in Node.js. The related resources are shown below.
The workflow is shown below.
- Coding preparation
- Coding
- Operation check
Run the following commands to prepare for coding.
mkdir gcp-text-to-speech
cd gcp-text-to-speech
npm init -y
npm install --save @google-cloud/text-to-speech
touch input.txt main.mjs
Open main.mjs in your editor and enter the following content.
Open index.txt in your editor and enter the content of the text you want to convert to speech. An example is shown below.
Click to go to input.example.txt
Run the following command to access the GCP API.
gcloud auth application-default login
Run the following command to convert the text to speech.
node main.mjs
I tried to use Google Text-to-Speech to generate the audio to use in the video when creating The demo video of how to record a video with JavaScript. But I was a little surprised to find that it was not available from the CLI (gcloud command) or GUI (web console). While there is a document to create audio from text by using the command line, but it seemed like it would take a lot of work to execute the curl command and send an HTTP request to the API endpoint. Therefore, I thought it would be quicker to use the Node.js client library, so I created the source code. This article was born based on the source code created at that time.