我们CIA刚刚逆向出来,有需要拿走。微软TTS需要花钱,结果想起来谷歌翻译语音朗读这个接口,这样就能直接薅羊毛了。
说实话google做的挺有意思,为了避免点击朗读的时候出现网络延迟卡顿,它把发送事件放到了element:hover
里面,大家可以学习一下。
至于我是怎么知道它的编码格式的嘛,见下图:
遥遥领先!
上代码(C#):
v2版本,封装好了可以直接调用
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
namespace GetVoice
{
public static class GetVoice
{
private static HttpClient client = new HttpClient();
public static bool WriteSpeech(string text, string language, string outputfile)
{
if (!outputfile.ToLower().EndsWith(".mp3")) outputfile += ".mp3";
text = text.Replace(",", "%2C"); //特殊处理,否则translate将会以为是,结束参数。
text = System.Net.WebUtility.UrlEncode(text);
HttpRequestMessage request = new HttpRequestMessage(HttpMethod.Get, "https://www.google.com/async/translate_tts?&ttsp=tl:" + language + ",txt:" + text + ",spd:1&cs=0&async=_fmt:jspb");
//spd不知道怎么用,范围0.1-2.9,改变之后速度仍然不变
//里面删除的几个参数:
//ei=h4dJZpmcDvaWkPIP_Z686AE&opi=89978449&rlz=1C1ONGR_enUS1093US1093
//yv=3
request.Headers.Add("accept", "*/*");
request.Headers.Add("accept-language", "en-US,en;q=0.9");
request.Headers.Add("priority", "u=1, i");
request.Headers.Add("referer", "https://www.google.com/");
request.Headers.Add("sec-ch-ua", "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"");
request.Headers.Add("sec-ch-ua-arch", "\"x86\"");
request.Headers.Add("sec-ch-ua-bitness", "\"64\"");
request.Headers.Add("sec-ch-ua-full-version", "\"124.0.6367.208\"");
request.Headers.Add("sec-ch-ua-full-version-list", "\"Chromium\";v=\"124.0.6367.208\", \"Google Chrome\";v=\"124.0.6367.208\", \"Not-A.Brand\";v=\"99.0.0.0\"");
request.Headers.Add("sec-ch-ua-mobile", "?0");
request.Headers.Add("sec-ch-ua-model", "\"\"");
request.Headers.Add("sec-ch-ua-platform", "\"Windows\"");
request.Headers.Add("sec-ch-ua-platform-version", "\"15.0.0\"");
request.Headers.Add("sec-ch-ua-wow64", "?0");
request.Headers.Add("sec-fetch-dest", "empty");
request.Headers.Add("sec-fetch-mode", "cors");
request.Headers.Add("sec-fetch-site", "same-origin");
request.Headers.Add("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36");
request.Headers.Add("x-dos-behavior", "Embed");
HttpResponseMessage response = client.SendAsync(request).Result;
try
{
response.EnsureSuccessStatusCode();
}
catch
{
return false;
}
string responseBody = response.Content.ReadAsStringAsync().Result;
responseBody = responseBody.Substring(")]}'\n{\"translate_tts\":[\"".Length);
responseBody = responseBody.Substring(0, responseBody.Length - "\"]}".Length);
byte[] data = Convert.FromBase64String(responseBody);
// Write the decoded data to a file
try
{
File.WriteAllBytes(outputfile, data);
}
catch
{
return false;
}
return true;
}
public static class Language
{
public const string Chinese = "zh-CN";
public const string English = "en";
}
}
}
记得点个赞再走,回来用的时候好找~