vb.net 从 Google 下载 MP3 翻译文本到语音

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9163988/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 15:55:38  来源:igfitidea点击:

Download MP3 from Google Translate text to speech

vb.netmp3google-translate

提问by user1169690

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

我从 Internet 上找到了这段代码,它使用 Google 翻译的文本到语音功能使用 URL。这是代码:

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.

我知道如何在我的 vb.net 中调用它,但我不知道如何从 Google Translate 保存 MP3 文件。我在 vb.net 中使用了 system.speech 来拥有此功能,但我特别需要从谷歌翻译中获取语音。那么,有谁知道如何使用该 URL 从 Google 翻译中保存声音文件?谢谢。

回答by Bobble

EDIT 2015-12-26

编辑 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 tkis 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.

自 2015 年 12 月 21 日起,此代码在对 Google TTS API 进行进一步更改后不再有效。正如@ncpierson 所指出的,需要一个新的附加参数tk,我很难弄清楚如何在 shell 脚本中计算它。我将尽可能/在可能的情况下通过新的编辑修改此答案。

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:

我不确定 Windows,但在 Linux 中,从命令行这很容易。我使用命令行脚本下载文本字符串的英文音频:

#!/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 .mp3 "http://translate.google.com/translate_tts?ie=UTF-8&client=t&tl=zh&q="

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

大多数 Linux 发行版都包含 wget 作为标准,但它可以轻松下载(参见,例如,此链接)。

(Thanks to @ncpierson for client=tparameter).

(感谢@ncpierson 提供client=t参数)。

回答by JCCbama

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

谷歌翻译页面运行的脚本,当我使用你的例子时,生成了一个名为“translate_tts”的文件,没有文件扩展名。

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".

使用此文件的一种简单方法是告诉您的浏览器,例如 Mozilla [在“工具”/“选项”/应用程序下],保存音频文件 - 在这种情况下,我相信它是 MP3,即使没有扩展名. 在任何情况下,选择音频/波形或音频/Mpg 旁边的“操作”下的选项到“保存文件”。

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.

当浏览器加载您的 URL 时,它会下载名为“translate_tts”的文件,无论数据是什么。因此,在您的情况下,我会将文件名更改为“Hello-World.mp3”。这样,您现在的硬盘驱动器上就有了包含您想要的音频的文件,该文件可以由任何音频播放器播放,最好是 VLC 媒体播放器,因为它几乎可以播放任何格式。

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

当然,如果您想要不同的音频,例如“Goodbye-World”,您只需将 URL 更改为

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

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

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

并重复上述步骤将文件保存为“Goodbye-World.mp3”。

回答by Francisco Presencia

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.

我不知道 vb.net,但我将尝试通过 linux 中的 bash 脚本下载一些信息。在我看到 vb.net 标签之前,我会推荐使用 wget。检查这个线程,它可能会给你一些想法。您基本上想要下载链接的页面,这是一个 mp3。

回答by Hunternif

Here's a solution written in Java:

这是用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.

省略了异常处理。IOUtils 来自 Apache Commons IO。

回答by Jrud

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
    
  1. 是否通过网络请求从谷歌 TTS 获取 MP3 文件
  2. 将 MP3 保存到一个文件中(我把它放在运行目录更深一层)
  3. 使用 windows 媒体播放器 COM API 播放文件(可以作为 COM 引用包含在您的项目中)
  4. 存储以前说出的短语的历史记录,因此当出现重复的短语时不必再次点击 API(我计划安装它的互联网不是很好)。

    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
    

回答by Nicholas R. Grant

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.

我已经制作了一种半自动化的方法来获取 2017 年生成的语音文件并剥离所有谷歌的元数据。它适用于 Firefox,是用 C# 编写的。所以它接近你想要做的,但仍然不是 100%。我试图直接获取这些文件,但 Google 已经实现了一些我目前似乎无法绕过的非常漂亮的安全功能。当我以为我接近时,它给我发回了一个 ReCaptcha。

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

我在这里开源了它:https: //github.com/Goodlookinguy/FFMediaCacheGrabber并提供了一个操作视频。

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.

不会撒谎,我对这个答案感觉不太好,因为它不是你要找的,但至少现在我能做的就是帮助人们。