Skip to content

Instantly share code, notes, and snippets.

@rise-worlds
Last active September 20, 2024 12:59
Show Gist options
  • Save rise-worlds/7aa4a920cd15229413d04b4b8b90aaa9 to your computer and use it in GitHub Desktop.
Save rise-worlds/7aa4a920cd15229413d04b4b8b90aaa9 to your computer and use it in GitHub Desktop.
谷歌翻译语音朗读接口
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";
}
}
}
import requests
import base64
class GetVoice:
headers = {
"accept": "*/*",
"accept-language": "en-US,en;q=0.9",
"priority": "u=1, i",
"referer": "https://www.google.com/",
"sec-ch-ua": "\"Chromium\";v=\"124\", \"Google Chrome\";v=\"124\", \"Not-A.Brand\";v=\"99\"",
"sec-ch-ua-arch": "\"x86\"",
"sec-ch-ua-bitness": "\"64\"",
"sec-ch-ua-full-version": "\"124.0.6367.208\"",
"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\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-model": "\"\"",
"sec-ch-ua-platform": "\"Windows\"",
"sec-ch-ua-platform-version": "\"15.0.0\"",
"sec-ch-ua-wow64": "?0",
"sec-fetch-dest": "empty",
"sec-fetch-mode": "cors",
"sec-fetch-site": "same-origin",
"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",
"x-dos-behavior": "Embed"
}
# 代理选填
proxies = {
"http": "http://127.0.0.1:7890",
"https": "http://127.0.0.1:7890",
}
@staticmethod
def write_speech(text, language, outputfile):
if not outputfile.lower().endswith(".mp3"):
outputfile += ".mp3"
text = text.replace(",", "%2C") # 特殊处理,否则translate将会以为是,结束参数
text = requests.utils.quote(text)
url = f"https://www.google.com/async/translate_tts?&ttsp=tl:{language},txt:{text},spd:1&cs=0&async=_fmt:jspb"
response = requests.get(url, headers=GetVoice.headers, proxies=GetVoice.proxies)
try:
response.raise_for_status()
except requests.exceptions.HTTPError:
return False
response_body = response.text
response_body = response_body[len(")]}'\n{\"translate_tts\":[\""):]
response_body = response_body[:len(response_body) - len("\"]}")]
try:
data = base64.b64decode(response_body)
with open(outputfile, "wb") as file:
file.write(data)
except Exception as e:
print(e)
return False
return True
class Language:
Chinese = "zh-CN"
English = "en"
# 测试函数使用
text = "你好,这是一个测试。"
language = Language.Chinese
outputfile = "output.mp3"
success = GetVoice.write_speech(text, language, outputfile)
if success:
print(f"语音文件已成功保存为 {outputfile}")
else:
print("语音文件生成失败")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment