Node.js Express Text to Speech Audio in Mp3 in Various Languages Using GTTS Library Full Tutorial 2020

Node.js Express Text to Speech Audio in Mp3 in Various Languages Using GTTS Library Full Tutorial 2020

 

 

 

 

Live Demo

 

 

You can see the live demo of the web app here

 

 

Get Started

 

In order to get started you need to install three dependencies given below

 

npm i express

 

npm i nodemon

 

npm i gtts

 

 

After installing this make the html of the application shown below

 

 

<div class="col-md-4 col-sm-6 col-xs-12">
    <div class="content colour-1">
      <h3 class="text-center">Text To Speech Demo</h3>
      <div class="text-center">
        <a class="btn btn-danger" href="/texttospeechdemo">Convert Now</a>
      </div>
    </div>
  </div>

 

 

<!DOCTYPE html>
<html>
  <head>
    <title>Text to Speech Demo</title>
    <link
      rel="stylesheet"
      href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css"
    />
  </head>
  <body>
      <div class="container">
          <h1 class="text-center">
              Text to Speech Demo
          </h1>
          <form action="/texttospeechdemo" method="post">
            <div class="form-group">
                <label for="text">Text:</label>
                <textarea class="form-control" name="text" id="" cols="30" rows="10"></textarea>
            </div>
            <div class="form-group">
                <label for="format">Select Language of Voice:</label>
                <select class="form-control" name="language" id="">
                    <option value="af" selected>Afrikaans</option>
                    <option value="sq">Albanian</option>
                    <option value="ar">Arabic</option>
                    <option value="hy">Armenian</option>
                    <option value="ca">Catalan</option>
                    <option value="zh">Chinese</option>
                    <option value="zh-cn">Chinese (Mandarin/China)</option>
                    <option value="zh-tw">Chinese (Mandarin/Taiwan)</option>
                    <option value="zh-yue">Chinese (Cantonese)</option>
                    <option value="hr">Croatian</option>
                    <option value="cs">Czech</option>
                    <option value="da">Danish</option>
                    <option value="nl">Dutch</option>
                    <option value="en" selected>English</option>
                    <option value="en-au">English (Australia)</option>
                    <option value="en-uk">English (United Kingdom)</option>
                    <option value="en-us">English (United States)</option>
                    <option value="eo">Esperanto</option>
                    <option value="fi">Finnish</option>
                    <option value="fr">French</option>
                    <option value="de">German</option>
                    <option value="el">Greek</option>
                    <option value="ht">Haitian Creole</option>
                    <option value="hi">Hindi</option>
                    <option value="hu">Hungarian</option>
                    <option value="is">Icelandic</option>
                    <option value="id">Indonesian</option>
                    <option value="it">Italian</option>
                    <option value="ja">Japanese</option>
                    <option value="ko">Korean</option>
                    <option value="la">Latin</option>
                    <option value="lv">Latvian</option>
                    <option value="mk">Macedonian</option>
                    <option value="no">Norwegian</option>
                    <option value="pl">Polish</option>
                    <option value="pt">Portuguese</option>
                    <option value="pt-br">Portuguese (Brazil)</option>
                    <option value="ro">Romanian</option>
                    <option value="ru">Russian</option>
                    <option value="sr">Serbian</option>
                    <option value="sk">Slovak</option>
                    <option value="es">Spanish</option>
                    <option value="es-es">Spanish (Spain)</option>
                    <option value="es-us">Spanish (United States)</option>
                    <option value="sw">Swahili</option>
                    <option value="sv">Swedish</option>
                    <option value="ta">Tamil</option>
                    <option value="th">Thai</option>
                    <option value="tr">Turkish</option>
                    <option value="vi">Vietnamese</option>
                    <option value="cy">Welsh</option>
                </select>
            </div>
            <div class="form-group">
                <button class="btn btn-danger btn-block">
                    Download Audio File
                </button>
            </div>

        </form>
      </div>
</body>
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</html>

 

See also  jsPDF Tutorial to Rotate Text at Different Angles or Directions in PDF Document Using Javascript Full Project For Beginners

 

 

const express = require("express");

const bodyParser = require("body-parser");

const gtts = require('gtts')

const fs = require("fs");


const app = express();

app.use(bodyParser.urlencoded({ extended: false }));
app.use(bodyParser.json());

const PORT = process.env.PORT || 5000;

app.get('/texttospeechdemo',(req,res) => {
  res.render('texttospeechdemo',{title:"Text to Speech Demo - Free Media Tools"})
})

app.post('/texttospeechdemo',(req,res) => {
  var text = req.body.text

  var language = req.body.language

  outputFilePath = Date.now() + "output.mp3"

  var voice = new gtts(text,language)

  voice.save(outputFilePath,function(err,result){
    if(err){
      fs.unlinkSync(outputFilePath)
      res.send("Unable to convert to audio")
    }
    res.download(outputFilePath,(err) => {
      if(err){
        fs.unlinkSync(outputFilePath)
        res.send("Unable to download the file")
      }

      fs.unlinkSync(outputFilePath)
    })
  })
})

app.listen(PORT, () => {
  console.log(`App is listening on Port ${PORT}`);
});

 

 

 

 

 

 

Leave a Reply