Script: text to speech (Google) from the terminal

Today I share with you a very useful script to convert text to speech using Google's speech engine. The main advantage of this script is that it "circumvents" the 100-byte limit imposed by Google. Actually, to be honest, it respects this limit but sends all the lines automatically, which means that this limitation goes unnoticed by the user. The most demanding ones will notice, however, that the conversion is not of the same quality as the one that we can obtain in Google Translate, mainly due to this limit and that it is not possible to pass the entire text in one go. As a positive point, it should be added that this script also allows you to specify the language of the text to be converted.

Having clarified, let's see what are the steps to follow.

Steps to follow

1.- Create a file with the name t2s.sh.

2.- Copy the following content:

#! / bin / bash ################################# # Speech Script by Dan Fountain # # Modified by UsemosLinux # # TalkToDanF@gmail.com # ################################# INPUT = $ * STRINGNUM = 0 ary = ($ {INPUT: 2}) echo "---------------------------" echo "Speech Script by Dan Fountain" echo "TalkToDanF @ gmail.com "echo" --------------------------- "for key in" $ {! ary [@]} "do SHORTTMP [ $ STRINGNUM] = "$ {SHORTTMP [$ STRINGNUM]} $ {ary [$ key]}" LENGTH = $ (echo $ {# SHORTTMP [$ STRINGNUM]}) #echo "word: $ key, $ {ary [$ key]} "#echo" adding to: $ STRINGNUM "if [[" $ LENGTH "-lt" 100 "]]; then #echo starting new line SHORT [$ STRINGNUM] = $ {SHORTTMP [$ STRINGNUM]} else STRINGNUM = $ (($ STRINGNUM + 1)) SHORTTMP [$ STRINGNUM] = "$ {ary [$ key]}" SHORT [ $ STRINGNUM] = "$ {ary [$ key]}" fi done for key in "$ {! SHORT [@]}" do #echo "line: $ key is: $ {SHORT [$ key]}" echo " Playing line: $ (($ key + 1)) of $ (($ STRINGNUM + 1)) "mplayer" http://translate.google.com/translate_tts? ie = UTF-8 &tl = $ {1} & q = $ {SHORT [$ key]} "done

3.- Grant execute permissions to the script:

sudo chmod + x t2s.sh

4.- Execute the script passing as parameters: a) the code of the language corresponding to the text, b) the text to be converted. For example:

./t2s.sh en This is very interesting text to convert.
Tip: Some terminal emulators allow you to paste text by just dragging it. So it is possible to select a text in Firefox and drag it to the terminal.

That's it, hope you enjoy it.


37 comments, leave yours

Leave a Comment

Your email address will not be published. Required fields are marked with *

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   Yoyo said

    How cool, thank you very much 😉

    And what a beautiful voice the girl has, I think I fell in love 😛

    1.    darkguzz said

      Excuse me, can you help me with Chinese? It does not detect Chinese characters, only pinyin.

  2.   Shadow said

    This is good as for a script that checks the mail or for a personalized alarm that when waking up tells you the forecast and pending messages xD

  3.   Yoyo said

    Here I made a test video 😛 https://www.youtube.com/watch?v=O3IeK7PjA_0

    1.    let's use linux said

      Good! Thank you!

  4.   bentrox said

    It sounds very good, I only had to install mpg123 🙂

  5.   porter said

    I had to install mpg123 but it was cool, so if it doesn't accept some words, it says a right tick letter etc.

    EH EH

  6.   paul honorato said

    The virus database has been updated (?)

    1.    jalbrhcp said

      apt update && ./t2.sh en The virus database has been updated

  7.   Alexander said

    Hahaha Excellent, I already have some uses for it, sensual voice without a doubt =), thanks for sharing.

  8.   Jorge Moratilla said

    As a Mac OSX user, it has not been possible for me to use the Script since I do not have mpg123 on, so by modifying the Script and using VLC, we can leave it working correctly on Mac OS X.

    http://pastebin.com/C2Mkp1Qy

    1.    Rolo said

      the issue is that vlc surely remains on and does not close when it finishes executing and you could use cvlc to prevent the graphical interface from opening

      1.    Rolo said

        it could be put
        cvlc –play-and-exit «https://translate.google.com/translate_tts?tl=$ái1-lex.europa.eu&q=$[SHORT[$key]}»
        so that it closes at the end of the reproduction

  9.   neysonv said

    Hi guys I get this error, let's see if someone tells me what the problem is
    Playing line: 1 of 1
    HTTP request failed: 404 Not Found
    [mpg123.c: 610] error: Access to http resource http://translate.google.com/translate_tts?tl=hola&q= the failed.

    1.    neysonv said

      [code] Playing line: 1 of 1
      HTTP request failed: 404 Not Found
      [mpg123.c: 610] error: Access to http resource http://translate.google.com/translate_tts?tl=hola&q= the failed. [/ code]

    2.    let's use linux said

      The problem is that you are using the script wrong.
      When you run it, you have to pass the language of the text as the first parameter, as shown in the example in the post.
      I give you another example:
      ./t2s.sh in This is a simple example.
      Notice that after ts2.sh it follows "en" (which means "english", that is, English). For Spanish, use "es" and then the phrase in that language you want to convert.
      Hug! Paul.

  10.   anonymous said

    you know the speak command no? hehe

    espeak -v is-the «hello»

    1.    let's use linux said

      Yes but not the same. This voice is so much better. 🙂

      1.    anonymous note said

        we agree!
        Regards!

  11.   Sara said

    For those who have problems with mpg123 -q or do not like it, you can switch to mplayer

    mpg123 -q "http://translate.google.com/translate_tts?tl=$[1-lex.europa.eu&q=$[SHORT[$key]}"

    mplayer «http://translate.google.com/translate_tts?tl=$[1[&q=$[SHORT[$key]}»

    1.    Guido said

      Better "mplayer -really-quiet" so it doesn't fill the output with text

  12.   Jonathan said

    A very good idea to use google for my applications suits me like a glove.
    http://github.com/alfa30/t2v

  13.   darkguzz said

    One more question; Could someone tell me how to record the output audio ???

    1.    Jose GDF said

      I answer you myself, because I just got it. Using mpg123, I have modified line 38 of the script, looking like this:

      mpg123 -q -w audio.wav «http://translate.google.com/translate_tts?tl=$[1[&q=$[SHORT[$key]}»

      I have added -w audio.wav. This creates a wav file with the audio of the phrase, but as I have left it, it is not heard. If you also want to hear the phrase, you would have to add another line. You would both look like this:

      mpg123 -q -w audio.wav «http://translate.google.com/translate_tts?tl=$[1-lex.europa.eu&q=$[SHORT[$key]}» &
      mpg123 -q "http://translate.google.com/translate_tts?tl=$[1-lex.europa.eu&q=$[SHORT[$key]}"

      Surely it could be done in another more efficient and clean way, but at least it works for me.

      1.    darkguzz said

        Excellent José GDF, I tried your method with very good results, greetings.

      2.    soymicmic said

        To save traffic in the second execution you could reproduce the .wav that you generated in the first

      3.    Jose GDF said

        Well yes, using a player that we have installed. For example, with VLC Player it would be:

        vlc-audio.wav

        Well, who says vlc, says any other player, including those that work by terminal.

        And to avoid overwriting the file every time the script is used, a variable should be used for the name. Name that the user would put (read command) before starting ... But that would be curling the curl 😀

  14.   drako said

    I did it in PHP (to create audios for an Asterisk phone system *)

    <?php
    $ url = 'http://translate.google.com/translate_tts?ie=UTF-8&q={query}% 0A & tl = is & prev = input ';

    if (count ($ argv) <= 1) {
    $ name = basename ($ argv [0]);
    die ($ name. 'usage:'. $ name. '
    }
    array_shift ($ argv);
    $ query = implode ('', $ argv);
    $ filename = str_replace (array ('', ',', '.'), '-', $ query);
    $ filename = str_replace ('-', '-', $ filename);

    $ url = str_replace ('{query}', rawurlencode ($ query), $ url);
    $ ch = curl_init ();
    Curl_setopt ($ ch, CURLOPT_URL, $ url);
    curl_setopt ($ ch, CURLOPT_HEADER, 0);
    curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true);
    $ result = curl_exec ($ ch);
    curl_close ($ ch);

    $ path = getcwd (). '/'. $ filename;
    file_put_contents ($ path. '. mp3', $ result);
    @exec ("sox {$ path} .mp3 -b 16 -r 8000 -t wavpcm {$ path} .wav");
    @unlink ($ path. '. mp3');

    1.    darkguzz said

      How about I have already managed to record the output audio with this command:
      arecord ~ ​​/ zhongwen.mp3 & ./t2s.sh zh ni hao; fg
      and at the end ctrl + c to stop recording.

      I had never used php
      But when running:
      php5 text-to-speech.php is Hello
      the konsola hosts this:
      sox FAIL formats: no handler for file extension `mp3 ′

      Any contribution is greatly appreciated, I will try again later.

  15.   Kirian said

    2 apps for google language translator + text to speech translation:
    http://www.linuxhispano.net/2014/05/29/traductor-de-google-voz/

  16.   José said

    I think it is the best way to convert, the voice is not as bad as the other options for Linux> _
    Is there a way to make it read a plain text file?
    It would be very useful to convert entire books to audio

  17.   chuflas said

    Well, it doesn't work for me, I don't know why, this comes out:

    xxxxx: ~ / Documents / Linux $ ./t2s.sh is hello

    ---------
    Speech Script by Dan Fountain
    TalkToDanF@gmail.com
    ---------
    Playing line: 1 of 1
    http_open: HTTP / 1.0 400 Bad Request
    http://translate.google.com: No such file or directory

    I have the mpg123 installed and I have done little tests and nothing…. Thanks in advance

  18.   Ignacio Cruz Martinez said

    Hey, this script has served me very well, both to listen to the sound and to record the words of the translator.

    In fact, I have modified the script to adapt it to a need that I have: Transform a list of words in text (lemario) to speech; save my voice in wav, convert to ogg and name them according to the word read.

    I did this in KDE using Kdialog to specify the path where the file with the word list is and the directory where I want the voice audio to be saved.

    When the conversion from WAV to OGG is finished, it creates two folders where the wav on one side and the ogg on the other are saved.

    At first it made me angry because it did not read the words with accents correctly, but I found that you have to add "es & ie = UTF8" in the code. For a moment I forgot the character encoding.

    I leave you the code of the script that I made in case it helps you:

    ################################################ # #####################

    #! / Bin / bash

    ##################################
    # Text-to-speech lecture #
    # Adapted by Ignacio Cruz Martínez #
    # the mail is secret xD #
    ##################################

    #KDE dialog box to specify path and name of lemmary
    soyunarchivo = $ (kdialog –inputbox «Enter the path and name of the file (with an extension if it has one):»)

    #KDE dialog box to specify destination folder for voice files
    path = $ (kdialog –inputbox «Enter the path to save the voice audio:»)

    # through cat all lines of the text file are read, each line consists of a single word
    for i in $ (cat $ soyunfile)
    do
    echo $ i

    # converting to speech using google translate, don't forget the encoding so it reads accents
    mpg123 -q -w "$ path" $ i.wav "http://translate.google.com/translate_tts?tl=es&ie=UTF8&q=$i"
    mpg123 -q "http://translate.google.com/translate_tts?tl=es&ie=UTF8&q=$i"

    # using ffmpeg to convert wav files to ogg
    ffmpeg -i "$ path" $ i.wav -acodec libvorbis "$ path" $ i.ogg
    done

    #with these lines the folders are created to organize the voice files in WAV or OGG
    mkdir "$ path" WAV
    mv "$ path" *. wav "$ path" WAV

    mkdir "$ path" OGG
    mv "$ path" *. ogg "$ path" OGG

    #KDE dialog box to indicate job finished
    kdialog –msgbox 'Process terminated'

    ################################################## ###################

    Now, do any of you have knowledge about the license to use the generated voice files?

    Limitation for copyright and related things. This interests me because I plan to use some of these voice files to support people who are going to become literate, basically it is a non-profit project.

    And it is that this tool has the best intonation for the Spanish language, unfortunately in Linux there is nothing similar. Mbrola has given me a lot of trouble to set up together with Gespeaker.

    Thanks and see you.

    1.    let's use linux said

      No idea ... would have to look at the Google Translator licenses.

  19.   Armando said

    The Script did not work for me, apparently it is a problem related to the URL, for those who are interested this is the correct URL:
    http://translate.google.com/translate_tts?tl=es&q=Hola

  20.   lol said

    Hello! I have been doing tests with your script and when executing it, it told me that it could not find the audio, so I have entered the URL and it seems that Mr. Google has put a captcha ...

  21.   ' said

    update the script