欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

挂接Google翻译,下载音频

程序员文章站 2022-05-26 22:29:57
...

最近在开发一个语音朗读模块,突然想到Google翻译支持在线朗读,查看了Google翻译的网络请求后,做了一个简单的语音生成。

请求参数说明:https://translate.google.cn/translate_tts?ie=UTF-8&q=success&tl=en&ie=UTF-8&total=1&idx=0&textlen=7&tk=955705.583384&client=t&prev=input

q = 要朗读的字符串  (200字节以内哦)

textlen = 朗读字符串的长度

tk = 朗读的字符串和tkk方可生成

tkk =eval('((function(){var a\x3d222433949;var b\x3d4070417730;return 422881+\x27.\x27+(a+b)})())');

(tkk值是在不定周期内改变的,你可以在F12下搜索关键字"TKK="以获取最近的tkk值 )

挂接Google翻译,下载音频

挂接Google翻译,下载音频

<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8" />
		<title></title>
		<style type="text/css">
			.bod{
				width: 500px;
				margin: 0 auto;
			}
			form{
				border: 1px solid darkseagreen;
				padding: 120px;
			}
		</style>
		<script type="text/javascript">
		function b(a, b) {
        for (var d = 0; d < b.length - 2; d += 3) {
            var c = b.charAt(d + 2),
                c = "a" <= c ? c.charCodeAt(0) - 87 : Number(c),
                c = "+" == b.charAt(d + 1) ? a >>> c : a << c;
            a = "+" == b.charAt(d) ? a + c & 4294967295 : a ^ c
        }
        return a
    }

    function tk(a,TKK) {
        for (var e = TKK.split("."), h = Number(e[0]) || 0, g = [], d = 0, f = 0; f < a.length; f++) {
            var c = a.charCodeAt(f);
            128 > c ? g[d++] = c : (2048 > c ? g[d++] = c >> 6 | 192 : (55296 == (c & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (c = 65536 + ((c & 1023) << 10) + (a.charCodeAt(++f) & 1023), g[d++] = c >> 18 | 240, g[d++] = c >> 12 & 63 | 128) : g[d++] = c >> 12 | 224, g[d++] = c >> 6 & 63 | 128), g[d++] = c & 63 | 128)
        }
        a = h;
        for (d = 0; d < g.length; d++) a += g[d], a = b(a, "+-a^+6");
        a = b(a, "+-3^+b+-f");
        a ^= Number(e[1]) || 0;
        0 > a && (a = (a & 2147483647) + 2147483648);
        a %= 1E6;
        return a.toString() + "." + (a ^ h)
    }
	
		var tkk =eval('((function(){var a\x3d222433949;var b\x3d4070417730;return 422881+\x27.\x27+(a+b)})())');
		//console.log(tk('add',tkk));
		//alert(tkk);
		//client=t&prev=input
		
	</script>
	</head>
	<body>	
			<div class="bod">
				<textarea name="" id="generate" cols="30" rows="10"></textarea>
				<div onclick="sub()">计算</div>
				<hr />
				<form action="https://translate.google.cn/translate_tts?" method="get">
					<input type="hidden" name="ie" value="UTF-8" />
					<textarea name="q" id="q" cols="30" rows="10"></textarea><br />
					<input type="hidden" name="tl" value="en" />
					<input type="hidden" name="ie" value="UTF-8" />
					<input type="hidden" name="total" value="1" />
					<input type="hidden" name="idx" value="0" />
					SIZE:<input type="text" id="textlen" name="textlen" value="" /><br />
					T   K:<input type="text" id='tk' name="tk" value="" /><br/>
					<input type="hidden" name="client" value="t" />
					<input type="hidden" name="prev" value="input" />
					<input type="submit" value="获取音频"/>
				</form>
			</div>
	</body>
	<script>
		function sub(){
			var val = document.getElementById('generate').value;
			document.getElementById('q').value = document.getElementById('generate').value;
			document.getElementById('textlen').value = val.length;
			document.getElementById('tk').value = tk(val,tkk);
			}
	</script>
</html>