DEV Community

Cover image for Generating Text-to-Speech Audio Files Using Google Text-to-Speech API in Node.js
FredAbod
FredAbod

Posted on

6

Generating Text-to-Speech Audio Files Using Google Text-to-Speech API in Node.js

In this article, we'll explore how to convert text into speech and save it as an audio file using the Google Text-to-Speech (TTS) API in a Node.js environment. We'll leverage the google-tts-api package along with Axios for making HTTP requests and fs for file system operations.

Before we begin, make sure you have Node.js installed on your machine. You'll also need to set up a new Node.js project and install the required packages:

npm init -y
npm install google-tts-api axios
Enter fullscreen mode Exit fullscreen mode

Let's dive right in

Diving

Now let's break down the code you'll add to your file

const fs = require('fs');
const tts = require('google-tts-api');
const axios = require('axios');

// Function to convert text to speech and save as an audio file
async function textToSpeech(text, language, outputFile) {
  try {
    const url = await tts.getAudioUrl(text, {
      lang: language || 'en',
      slow: false,
      host: 'https://translate.google.com',
    });

    const response = await axios.get(url, { responseType: 'arraybuffer' });

    fs.writeFileSync(outputFile, Buffer.from(response.data));
    console.log(`Audio file saved to: ${outputFile}`);
  } catch (error) {
    console.error('Error converting text to speech:', error);
  }
}

// Example usage
const textToConvert = 'Hello, this is a test.'

const outputFileName = 'output.mp3';
const languageCode = 'en'; // Change this to the desired language code

textToSpeech(textToConvert, languageCode, outputFileName);
Enter fullscreen mode Exit fullscreen mode
  • We import the necessary modules: fs for file system operations, google-tts-api for accessing the Google TTS API, and axios for making HTTP requests.
  • The textToSpeech function takes three parameters: the text to convert, the language code (defaulting to English if not provided), and the output file name.
  • Inside the function, we generate the audio URL using tts.getAudioUrl, specifying language and speed parameters.
  • We then use axios to fetch the audio data from the generated URL and write it to the output file using fs.writeFileSync.
  • Any errors that occur during the process are caught and logged.
  • This code snippet converts the text "Hello, this is a test." into speech and saves it as an MP3 file named "output.mp3" in the specified language.

Now run your code and your output.mp3 file should appear
Do not forget to like and follow

Bow

Sentry blog image

Identify what makes your TTFB high so you can fix it

In the past few years in the web dev world, we’ve seen a significant push towards rendering our websites on the server. Doing so is better for SEO and performs better on low-powered devices, but one thing we had to sacrifice is TTFB.

Read more

Top comments (0)

Cloudinary image

Video API: manage, encode, and optimize for any device, channel or network condition. Deliver branded video experiences in minutes and get deep engagement insights.

Learn more

👋 Kindness is contagious

Immerse yourself in a wealth of knowledge with this piece, supported by the inclusive DEV Community—every developer, no matter where they are in their journey, is invited to contribute to our collective wisdom.

A simple “thank you” goes a long way—express your gratitude below in the comments!

Gathering insights enriches our journey on DEV and fortifies our community ties. Did you find this article valuable? Taking a moment to thank the author can have a significant impact.

Okay