刚刚逆向出来的谷歌文字转语音(TTS),可直接调用

我们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";
        }
    }
}

记得点个赞再走,回来用的时候好找~

90 Likes

不明觉厉!

3 Likes

太强了佬

4 Likes

收藏了,感谢大佬

2 Likes

使用方法,问的Gemini :blush:
图片

5 Likes

来个python版的 :grinning:

2 Likes

让ai直接转python脚本,这样可以不 :watermelon:

3 Likes

py方便

2 Likes

收藏

3 Likes

cf的woker版本才是最爱


下载地址:
https://pastebin.com/raw/af1eRXKh

16 Likes

Python版本的

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("语音文件生成失败")
16 Likes

:+1:

2 Likes

tts的使用场景有哪些?

2 Likes

感谢大佬,收下膝盖。

edge-tts也有大佬开源版本的

1 Like

先点歌赞。但不知道怎么用。。。

1 Like

6哇

马上用上

mark

厉害了,我的佬