Download MP3 from Google Translate text to speech
Asked Answered
M

6

22

I found this code from the Internet and it uses the Google translate's text to speech capability using URL. here is the code:

http://translate.google.com/translate_tts?tl=en&q="hello world"

I know how to call this in my vb.net but I don't know how to save the MP3 file from Google Translate. i used the system.speech in vb.net to have this capability but I specifically need to get the speech from google translate. so, does anyone know how to save the sound file from Google Translate using that URL? Thanks.

Misshapen answered 6/2, 2012 at 17:1 Comment(3)
What code do you have so far?Stagger
Did anyone figure this out? I'd like to embed pronunciations in a certain language on a site.Japheth
let me get this straight, you wish to download the mp3 (or wave file) of tts from google and play it in your app, runtime?Scammony
P
21

EDIT 2015-12-26

As of 2015-12-21 this code no longer works following further changes to the Google TTS API. As indicated by @ncpierson a new additional parameter tk is required, and I am having a hard time working out how to calculate it in a shell script. I will revise this answer with a new edit as/when I can.

I'm not sure about Windows, but in Linux this is very easy from the command line. I use a command line script to download English audio of text strings:

#!/bin/bash
# write an English text string as an audio file using Google Translate
# usage: en2audio.sh <text>
wget -q -U Mozilla -O "$*.mp3" "http://translate.google.com/translate_tts?ie=UTF-8&client=t&tl=en&q=$*"

I do the same thing with Chinese (the script is a bit simpler because there are no spaces to parse between words):

#!/bin/bash
# write a Chinese text string as an audio file using Google Translate
# usage: zh2audio.sh <text>
wget -q -U Mozilla -O $1.mp3 "http://translate.google.com/translate_tts?ie=UTF-8&client=t&tl=zh&q=$1"

Most Linux distros include wget as standard, but it can easily be downloaded (see, e.g, this link).

(Thanks to @ncpierson for client=t parameter).

Popularize answered 11/12, 2012 at 16:8 Comment(10)
Now that these services require captcha input no command line method will work. The services are still available free, but you have to type in a captcha code, so a web browser is needed.Popularize
Google Translate changed again. I believe another query param is needed: 'client=t'. However, it also seems to be rate limiting, now.Demonology
@ncpierson Thanks for your comment. I have revised my answer (again...) to accommodate the changes.Popularize
Google Translate has updated yet again. To properly access the API, you now have to do something like this: gist.github.com/ncpierson/eeea9956cb2bc3b290e5. A 'tk' parameter is now required. It's some sort of hash function that I don't recognize, but it serves to verify the query text (salted with the current time).Demonology
@ncpierson Awesome link, but I am still trying to get my head around this. Where/how did you find the API change? Is it published somewhere? I want to work out if/how I can change the scripts in my answer...Popularize
@ncpierson In short, how do I calculate the tk parameter?Popularize
The only way I know how is with that Gist I made. That code is copied almost directly from uglified JS served from translate.google.com. The 'TL' function calculates the 'tk' parameter. Sadly, there is no API docs, as far as I know.Demonology
Google has complicated things further. I was able to sort of get it working, I think, but it caught on to me immediately and started returning captcha requests and not audio. Not sure where to go from here other than paying into their API service.Levator
I forgot to mention this in my last comment, but if you're desperate to get the files, use Firefox. Go into AppData/Mozilla/Profile/xyz/cache2/entries and turn the file modified order to newest first. Then type something in and play the sound file. They are encoded in LAME 3.99 and will be around a starting size of 9KBs. They can be located with a magic header search of FF F3 40 C4 as the first 4 bytes. However I should note, Google's generated sound files have tons of meta data in them to track back to when and who made the files. Strip that and you'll be good.Levator
Hey everyone. Shameless self promotion: I've needed this for my website soundoftext.com. I've found ways around the Google API every time they change it. I'm releasing a new site soon, and plan to have an actual API. So keep watch. Although, even now you can reverse engineer my API. Plenty of people have :shrug:Demonology
C
7

The script that the google translate page ran, when I used your example, produced a file called "translate_tts" with no file extension.

One easy way to use this file is to tell your browser, e.g. Mozilla [under the "TOOLS"/ "OPTIONS"/APPLICATIONS], to save audio files - in this case I believe it is an MP3, even though there is no extension. In any case, select the option under "ACTION" next to audio/wave or audio/Mpg to "SAVE FILE".

When the browser loads your URL, it downloads the file called "translate_tts", no matter what the data is. So, in your case I would change the file name to "Hello-World.mp3". That way you now have the file on your hard drive, containing the audio you want, which can be played by any audio player, preferably VLC media player since it will play almost any format.

Of course if you want a different audio , e.g. "Goodbye-World", you just change your URL to

http://translate.google.com/translate_tts?tl=en&q="Goodbye-World"

and repeat the above steps to save the file as "Goodbye-World.mp3".

Cavil answered 26/12, 2012 at 3:55 Comment(1)
This simply works. Although you need to change the tl-en to the target language.Katleen
P
4

If you want to do it manually, right click 'save as...' will do the trick. Example: test your own example.

I have no idea of vb.net, but I am going to try to download several bits of information through bash script in linux. I was going to recommend using wget until I saw the vb.net tag. Check this thread, it might give you some idea. You basically want to download the page of the link, which is an mp3.

Plafond answered 29/9, 2012 at 22:47 Comment(2)
Downvoted because it just downloads web pages / javacript, no media files.Burdelle
Maybe now, but not when this reply was written back in 2012 :)Plafond
D
2

Here's a solution written in Java:

URL url = new URL("http://translate.google.com/translate_tts?tl=en&q=\"hello world\"");
HttpURLConnection httpcon = (HttpURLConnection) url.openConnection();
httpcon.addRequestProperty("User-Agent", "anything");
IOUtils.copy(httpcon.getInputStream(), new FileOutputStream("output.mp3"));

Exception handling omitted. IOUtils comes from Apache Commons IO.

Downbeat answered 17/7, 2013 at 16:18 Comment(0)
F
2

I was was trying to write a function that does exactly what yours does, plus a little extra, and after some searching I was able to produce the following code. It does four things:

  1. Does a web request to get the MP3 file from google TTS
  2. Saves the MP3 to a file (I put it 1 layer deeper into the running directory)
  3. Plays the file using the windows media player COM API (can be included as a COM reference in your project)
  4. Stores a history of previously uttered phrases so it doesn't have to hit the API again when a repeated phrase appears (the internet isn't great where I plan on installing this).

    Imports System.Net
    Imports System.IO
    Imports System.Text
    
    Dim MP As New MediaPlayer.MediaPlayer
    
    Private Sub SaySomething(TTS As String)
    If Not TTS = "" Then
        If Not System.IO.File.Exists(Environment.CurrentDirectory.ToString() + "/TTS/" + TTS + ".mp3") Then
            Dim WR As HttpWebRequest = HttpWebRequest.Create("http://translate.google.com/translate_tts?tl=en&q=""" + TTS + """")
    
            Dim response As HttpWebResponse = CType(WR.GetResponse(), HttpWebResponse)
    
            Dim receiveStream As Stream = response.GetResponseStream()
    
            Dim readStream As New StreamReader(receiveStream, Encoding.UTF8)
    
            If Not System.IO.Directory.Exists(Environment.CurrentDirectory.ToString() + "/TTS") Then
                System.IO.Directory.CreateDirectory(Environment.CurrentDirectory.ToString() + "/TTS")
            End If
    
            Dim fs As New FileStream(Environment.CurrentDirectory.ToString() + "/TTS/" + TTS + ".mp3", FileMode.Create)
    
            readStream.BaseStream.CopyTo(fs)
    
            fs.Close()
            fs.Dispose()
            readStream.Dispose()
            receiveStream.Dispose()
        End If
    
        MP.Open(Environment.CurrentDirectory.ToString() + "/TTS/" + TTS + ".mp3")
    End If
    End Sub
    
Felipe answered 29/12, 2014 at 19:58 Comment(0)
L
0

I have produced a semi-automated way to acquire the generated speech files in 2017 and strip all of Google's metadata. It's for Firefox and it's written in C#. So it's close to what you're trying to do, but still not 100%. I tried to obtain the files directly, but Google has implemented some pretty fancy security features that I couldn't seem to get around at this time. When I thought I got close, it sent me back a ReCaptcha.

I've open sourced it here: https://github.com/Goodlookinguy/FFMediaCacheGrabber and provided a how-to video as well.

Not gonna lie, I don't feel great about this answer as it's not what you're looking for, but it's all I could do at least for now to help people.

Levator answered 17/6, 2017 at 9:12 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.