mark
mark! 感谢分享!
感谢
让chatgpt写了一个python版,亲测可用
import requests
import base64
class GetVoice:
@staticmethod
def write_speech(text, language, outputfile):
if not outputfile.lower().endswith('.mp3'):
outputfile += '.mp3'
text = text.replace(',', '%2C') # Special handling for commas
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"
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"
}
response = requests.get(url, headers=headers)
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("\"]}")]
data = base64.b64decode(response_body)
try:
with open(outputfile, 'wb') as f:
f.write(data)
except IOError:
return False
return True
class Language:
Chinese = 'zh-CN'
English = 'en'
# Example usage:
GetVoice.write_speech("夜色如墨,灯火阑珊,一城秋雨,一街残烟。梧桐深巷,风声涟涟,孤影独行,梦绕心弦。", GetVoice.Language.Chinese, "output.mp3")
1 个赞
我修改放置位置只能 提示报错 有谁知道怎么回事吗 ,Version upload failed. You attempted to upload a Service Worker syntax script to the version upload API, which only supports ES Modules. See Migrate from Service Workers to ES Modules · Cloudflare Workers docs for migration instructions. (Code: 10216)
import base64
import requests
import urllib.parse
def write_speech(text, language, outputfile):
if not outputfile.lower().endswith(".mp3"):
outputfile += ".mp3"
# 处理特殊字符
text = urllib.parse.quote(text.replace(",", "%2C"))
# 构造URL
url = f"https://www.google.com/async/translate_tts?&ttsp=tl:{language},txt:{text},spd:1&cs=0&async=_fmt:jspb"
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"
}
try:
response = requests.get(url, headers=headers,verify=False)
except requests.exceptions.RequestException:
return False
response_body = response.text
response_body = response_body[len(")]}'\n{\"translate_tts\":[\""):]
response_body = response_body[:-len("\"]}")]
try:
data = base64.b64decode(response_body)
with open(outputfile, "wb") as file:
file.write(data)
except:
return False
return True
# 定义语言常量
LANGUAGE_CHINESE = "zh-CN"
LANGUAGE_ENGLISH = "en"
# 使用函数 - 例如:
write_speech("你好热老", LANGUAGE_CHINESE, "output")
mark
佬们主观能动性都很强啊
要睡觉了枕头就来了,谢谢佬。
mark
这个是直接复制到woker代码里面部署吗
这个想法确实厉害了!点个赞
对的
太6了,要是能换声音就好了
md大善人快被薅秃了,十个帖子里面八个是worker
1 个赞
Java版本:
import java.io.*;
import java.net.*;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class GetVoice {
// 定义基础 URL
private static final String BASE_URL = "https://www.google.com/async/translate_tts";
/**
* 将文本转换为语音并写入到 MP3 文件
*
* @param text 文本内容
* @param language 语言代码
* @param outputFile 输出文件路径
* @return 如果操作成功返回 true,否则返回 false
*/
public static boolean writeSpeech(String text, String language, String outputFile) {
// 如果输出文件名不以 .mp3 结尾,则添加 .mp3 后缀
if (!outputFile.toLowerCase().endsWith(".mp3")) {
outputFile += ".mp3";
}
try {
// 替换文本中的逗号,避免 URL 编码问题
text = text.replace(",", "%2C");
// 对文本进行 URL 编码
String encodedText = URLEncoder.encode(text, StandardCharsets.UTF_8.toString());
// 构建请求的 URL
StringBuilder urlBuilder = new StringBuilder(BASE_URL);
urlBuilder.append("?ttsp=tl:")
.append(language)
.append(",txt:")
.append(encodedText)
.append(",spd:1&cs=0&async=_fmt:jspb");
// 创建 URL 对象并打开连接
URL url = new URL(urlBuilder.toString());
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
// 设置请求头信息
connection.setRequestProperty("accept", "*/*");
connection.setRequestProperty("accept-language", "en-US,en;q=0.9");
// ... 其他头部信息添加方式类似
// 设置请求方法为 GET
connection.setRequestMethod("GET");
// 获取响应码
int responseCode = connection.getResponseCode();
if (responseCode == HttpURLConnection.HTTP_OK) {
// 读取响应内容
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String inputLine;
StringBuilder response = new StringBuilder();
while ((inputLine = in.readLine()) != null) {
response.append(inputLine);
}
in.close();
// 处理响应体,提取音频数据
String responseBody = response.toString();
responseBody = responseBody.substring(")]}'\n{\"translate_tts\":[".length());
responseBody = responseBody.substring(0, responseBody.length() - "\"]}".length());
// 对音频数据进行 Base64 解码
byte[] data = Base64.getDecoder().decode(responseBody);
// 将解码后的数据写入文件
Files.write(Paths.get(outputFile), data);
return true;
} else {
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
}
// 定义语言代码的内部类
public static final class Language {
public static final String Chinese = "zh-CN"; // 简体中文
public static final String English = "en"; // 英语
}
public static void main(String[] args) {
// 示例用法
String textToSpeech = "Hello, world!";
String language = Language.English;
String outputFile = "output.mp3";
boolean result = writeSpeech(textToSpeech, language, outputFile);
System.out.println("转换为语音并写入文件结果: " + result);
}
}
要把代码最底下的翻译给删了?不然部署不成功
有这个问题吗?我直接部署的哎,你也可以看看下面哪个有老哥优化后的
我又看下,你们发的没有,为什么我复制过去下面就有翻译两个字,我才发现
不知道你加了啥