IT TIP

Google Text-To-Speech API

itqueen 2020. 10. 28. 21:21
반응형

Google Text-To-Speech API


내 .NET 프로젝트에서 Google Text-to-Speech API를 어떻게 사용할 수 있는지 알고 싶습니다. 웹 서비스를 사용하려면 URL을 호출해야한다고 생각하지만 저에 대한 아이디어는 명확하지 않습니다. 누구든지 도울 수 있습니까?


이전 답변 :

다음 URL을 사용해보십시오. http://translate.google.com/translate_tts?tl=en&q=Hello%20World 모든 .net 프로그래밍을 통해 HTTP 요청으로 쉽게 얻을 수있는 wav 파일이 자동으로 생성됩니다.

편집하다:

Ohh Google, 당신은 사람들이 당신의 멋진 서비스를 간결한 http 헤더 확인으로 사용하는 것을 막을 수 있다고 생각했습니다.

다음은 여러 언어로 응답을받을 수있는 솔루션입니다 (가는대로 추가하려고합니다).

NodeJS

// npm install `request`
const fs = require('fs');
const request = require('request');
const text = 'Hello World';

const options = {
    url: `https://translate.google.com/translate_tts?ie=UTF-8&q=${encodeURIComponent(text)}&tl=en&client=tw-ob`,
    headers: {
        'Referer': 'http://translate.google.com/',
        'User-Agent': 'stagefright/1.2 (Linux;Android 5.0)'
    }
}

request(options)
    .pipe(fs.createWriteStream('tts.mp3'))

곱슬 곱슬하다

curl 'https://translate.google.com/translate_tts?ie=UTF-8&q=Hello%20Everyone&tl=en&client=tw-ob' -H 'Referer: http://translate.google.com/' -H 'User-Agent: stagefright/1.2 (Linux;Android 5.0)' > google_tts.mp3

헤더는 @Chris Cirefice의 예제를 기반으로하며, 어느 시점에서 작동이 중지되면이 코드가 작동 할 수있는 조건을 다시 만들려고합니다. 현재 헤더에 대한 모든 크레딧은 그와 WireShark라는 멋진 도구로 이동합니다. (또한 이것을 패치하지 않은 Google에 감사드립니다)


Schahriar SaffarShargh의 답변 업데이트 에서 Google은 최근 'Google 남용'기능을 구현하여 다음과 같은 URL에 일반 이전 HTTP GET을 보낼 수 없도록했습니다.

http://translate.google.com/translate_tts?tl=ko&q=Hello%20World

이전에는 훌륭하고 멋지게 작동했습니다. 이제 이러한 링크를 따라 가면 CAPTCHA가 표시됩니다. 또한 해당 URL을 사용하면 남용 방지 페이지 (보안 문자)로 리디렉션되기 때문에 브라우저 외부에서 HTTP GET 요청 (예 : cURL 사용)에도 영향을줍니다.

시작하려면 client요청 URL에 쿼리 매개 변수 추가해야합니다 .

http://translate.google.com/translate_tts?tl=en&q=Hello%20World&client=t

Google 번역에서을 전송 &client=t하므로 나도해야합니다.

HTTP 요청을하기 전에 Referer헤더 를 설정했는지 확인하십시오 .

Referer: http://translate.google.com/

분명히 User-Agent헤더도 필요하지만 흥미롭게도 충분히 비워 둘 수 있습니다.

User-Agent:

편집 : 참고 -Android 4.X와 같은 일부 사용자 에이전트에서는 사용자 정의 User-Agent 헤더가 전송되지 않습니다 . 이는 Google이 요청을 처리하지 않음을 의미합니다. 이 문제를 해결하기 위해 간단히 User-Agentstagefright/1.2 (Linux;Android 5.0). Google 서버가 응답하지 않는 경우 Wireshark사용 하여 요청을 디버그하고 (내가했던 것처럼) 이러한 헤더가 GET! Google은 503 Service Unavailable요청이 실패하면으로 응답 한 다음 CAPTCHA 페이지로 리디렉션됩니다.

이 솔루션은 약간 부서지기 쉽습니다. Google이 향후 이러한 요청을 처리하는 방식을 변경할 가능성이 전적으로 가능하므로 결국 에는 가짜 HTTP 헤더에 대해 더럽다고 느끼지 않고 사용할 수있는 실제 API 엔드 포인트 (무료 또는 유료) 를 만들도록 Google에 요청하는 것이 좋습니다 .


편집 2 : 관심있는 사람들을 위해이 cURL 명령은 영어 Hello 의 mp3를 다운로드하기에 완벽하게 작동합니다 .

curl 'http://translate.google.com/translate_tts?ie=UTF-8&q=Hello&tl=en&client=t' -H 'Referer: http://translate.google.com/' -H 'User-Agent: stagefright/1.2 (Linux;Android 5.0)' > google_tts.mp3

아시다시피 요청에 RefererUser-Agent헤더를 모두 설정 client=t하고 쿼리 문자열에 매개 변수를 추가했습니다 . https대신 http선택하여 사용할 수 있습니다 !


편집 3 : Google은 이제 각 GET 요청에 대해 토큰을 요구합니다 ( tk쿼리 문자열에 표시됨). 다음은 TTS mp3를 올바르게 다운로드하는 수정 된 cURL 명령입니다.

curl 'https://translate.google.com/translate_tts?ie=UTF-8&q=hello&tl=en&tk=995126.592330&client=t' -H 'user-agent: stagefright/1.2 (Linux;Android 5.0)' -H 'referer: https://translate.google.com/' > google_tts.mp3

통지 및 TK = 995126.592330 쿼리 문자열에서을; 이것이 새로운 토큰입니다. 스피커 아이콘을 누르고 translate.google.comGET 요청을보고이 토큰을 얻었습니다 . 이 쿼리 문자열 매개 변수를 이전 cURL 명령에 추가하기 만하면 작동합니다.

참고 : 분명히이 솔루션은 매우 취약하며 요청에 필요한 토큰과 같은 새로운 것을 도입하는 Google의 건축가의 변덕에 부응합니다. 이 토큰은 내일 작동하지 않을 수 있습니다 (하지만 확인하고 다시보고하겠습니다) ... 요점은이 방법에 의존하는 것이 현명하지 않다는 것입니다. 대신, 특히 프로덕션에서 TTS를 사용하는 경우 상용 TTS 솔루션으로 전환해야합니다.

토큰 생성에 대한 자세한 설명과 이에 대해 수행 할 수있는 작업은 Boude의 답변을 참조하십시오 .


이 솔루션이 나중에 언제든지 중단 될 경우이 답변에 대한 의견을 남겨 주시면 해결 방법을 찾을 수 있습니다!


Chris의 대답을 확장합니다 . 토큰 생성 프로세스를 역 설계했습니다.

요청에 대한 토큰은 페이지 스크립트에 설정된 텍스트 및 전역 TKK 변수를 기반으로합니다. 이것들은 JavaScript에서 해시되어 tk 매개 변수가됩니다.

페이지 스크립트 어딘가에 다음과 같은 내용이 있습니다.

TKK='403413';

epoch 이후 경과 한 시간입니다.

텍스트는 다음 함수에서 펌핑됩니다 (다소 난독 처리됨).

var query = "Hello person";
var cM = function(a) {
    return function() {
        return a
    }
};
var of = "=";
var dM = function(a, b) {
    for (var c = 0; c < b.length - 2; c += 3) {
        var d = b.charAt(c + 2),
            d = d >= t ? d.charCodeAt(0) - 87 : Number(d),
            d = b.charAt(c + 1) == Tb ? a >>> d : a << d;
        a = b.charAt(c) == Tb ? a + d & 4294967295 : a ^ d
    }
    return a
};

var eM = null;
var cb = 0;
var k = "";
var Vb = "+-a^+6";
var Ub = "+-3^+b+-f";
var t = "a";
var Tb = "+";
var dd = ".";
var hoursBetween = Math.floor(Date.now() / 3600000);
window.TKK = hoursBetween.toString();

fM = function(a) {
    var b;
    if (null === eM) {
        var c = cM(String.fromCharCode(84)); // char 84 is T
        b = cM(String.fromCharCode(75)); // char 75 is K
        c = [c(), c()];
        c[1] = b();
        // So basically we're getting window.TKK
        eM = Number(window[c.join(b())]) || 0
    }
    b = eM;

    // This piece of code is used to convert d into the utf-8 encoding of a
    var d = cM(String.fromCharCode(116)),
        c = cM(String.fromCharCode(107)),
        d = [d(), d()];
    d[1] = c();
    for (var c = cb + d.join(k) +
            of, d = [], e = 0, f = 0; f < a.length; f++) {
        var g = a.charCodeAt(f);

        128 > g ? d[e++] = g : (2048 > g ? d[e++] = g >> 6 | 192 : (55296 == (g & 64512) && f + 1 < a.length && 56320 == (a.charCodeAt(f + 1) & 64512) ? (g = 65536 + ((g & 1023) << 10) + (a.charCodeAt(++f) & 1023), d[e++] = g >> 18 | 240, d[e++] = g >> 12 & 63 | 128) : d[e++] = g >> 12 | 224, d[e++] = g >> 6 & 63 | 128), d[e++] = g & 63 | 128)
    }


    a = b || 0;
    for (e = 0; e < d.length; e++) a += d[e], a = dM(a, Vb);
    a = dM(a, Ub);
    0 > a && (a = (a & 2147483647) + 2147483648);
    a %= 1E6;
    return a.toString() + dd + (a ^ b)
};

var token = fM(query);
var url = "https://translate.google.com/translate_tts?ie=UTF-8&q="  + encodeURI(query) + "&tl=en&total=1&idx=0&textlen=12&tk=" + token + "&client=t";
document.write(url);

나는 이것을 gTTS 포크 에서 파이썬으로 성공적으로 이식 할 수 있었 으므로 이것이 작동 한다는 것을 알고 있습니다.

편집 : 이제 gTTS에서 사용하는 토큰 생성 코드가 gTTS-token 으로 이동되었습니다 .

편집 2 : Google이 API를 변경했습니다 (2016 년 5 월 10 일경),이 방법은 약간의 수정이 필요합니다. 저는 현재이 작업을하고 있습니다. 그 동안 클라이언트를 tw-ob로 변경하면 작동하는 것 같습니다.

편집 3 :

변경 사항은 사소하지만 아무리 작게 말하면 짜증납니다. TKK는 이제 두 부분으로 구성됩니다. 처럼 보입니다 406986.2817744745. 보시다시피 첫 번째 부분은 동일하게 유지되었습니다. 두 번째 부분은 겉보기에 난수 두 개의 합입니다. TKK=eval('((function(){var a\x3d2680116022;var b\x3d137628723;return 406986+\x27.\x27+(a+b)})())');여기서 \x3d의미 =하고 \x27있다 '. a와 b는 모두 UTC 분마다 변경됩니다. 알고리즘의 마지막 단계 중 하나에서 토큰은 두 번째 부분에 의해 XOR됩니다.

새로운 토큰 생성 코드는 다음과 같습니다.

var xr = function(a) {
    return function() {
        return a
    }
};
var yr = function(a, b) {
    for (var c = 0; c < b.length - 2; c += 3) {
        var d = b.charAt(c + 2)
          , d = "a" <= d ? d.charCodeAt(0) - 87 : Number(d)
          , d = "+" == b.charAt(c + 1) ? a >>> d : a << d;
        a = "+" == b.charAt(c) ? a + d & 4294967295 : a ^ d
    }
    return a
};
var zr = null;
var Ar = function(a) {
    var b;
    if (null  !== zr)
        b = zr;
    else {
        b = xr(String.fromCharCode(84));
        var c = xr(String.fromCharCode(75));
        b = [b(), b()];
        b[1] = c();
        b = (zr = window[b.join(c())] || "") || ""
    }
    var d = xr(String.fromCharCode(116))
      , c = xr(String.fromCharCode(107))
      , d = [d(), d()];
    d[1] = c();
    c = "&" + d.join("") + 
    "=";
    d = b.split(".");
    b = Number(d[0]) || 0;
    for (var e = [], f = 0, g = 0; g < a.length; g++) {
        var l = a.charCodeAt(g);
        128 > l ? e[f++] = l : (2048 > l ? e[f++] = l >> 6 | 192 : (55296 == (l & 64512) && g + 1 < a.length && 56320 == (a.charCodeAt(g + 1) & 64512) ? (l = 65536 + ((l & 1023) << 10) + (a.charCodeAt(++g) & 1023),
        e[f++] = l >> 18 | 240,
        e[f++] = l >> 12 & 63 | 128) : e[f++] = l >> 12 | 224,
        e[f++] = l >> 6 & 63 | 128),
        e[f++] = l & 63 | 128)
    }
    a = b;
    for (f = 0; f < e.length; f++)
        a += e[f],
        a = yr(a, "+-a^+6");
    a = yr(a, "+-3^+b+-f");
    a ^= Number(d[1]) || 0;
    0 > a && (a = (a & 2147483647) + 2147483648);
    a %= 1E6;
    return c + (a.toString() + "." + (a ^ b))
}
;
Ar("test");

물론 a와 b가 어떻게 생성되는지 모르기 때문에 더 이상 유효한 URL을 생성 할 수 없습니다.


추가 대안은 responsivevoice.org입니다 . 간단한 예제 JsFiddle은 여기에 있습니다.

HTML

<div id="container">
<input type="text" name="text">
<button id="gspeech" class="say">Say It</button>
<audio id="player1" src="" class="speech" hidden></audio>
</div>

JQuery

$(document).ready(function(){

 $('#gspeech').on('click', function(){

        var text = $('input[name="text"]').val();
        responsiveVoice.speak("" + text +"");
        <!--  http://responsivevoice.org/ -->
    });

});

외부 리소스 :

https://code.responsivevoice.org/responsivevoice.js


Wget : D를 사용하여 음성을 다운로드 할 수 있습니다.

wget -q -U Mozilla "http://translate.google.com/translate_tts?tl=en&q=Hello"

출력을 mp3 파일로 저장합니다.

wget -q -U Mozilla "http://translate.google.com/translate_tts?tl=en&q=Hello" -O hello.mp3

즐겨 !!


좋습니다. 따라서 Google은 토큰을 도입했으며 (새 URL의 tk 매개 변수 참조) 이전 솔루션이 작동하지 않는 것 같습니다. 나는 대안을 찾았습니다. 더 나은 소리라고 생각하고 더 많은 목소리를 가지고 있습니다! 명령은 예쁘지 않지만 작동합니다. 이것은 테스트 목적으로 만 사용됩니다 (나는 약간의 domotica 프로젝트에 사용합니다). 상업적으로 사용할 계획이라면 acapella-group의 실제 버전을 사용하십시오.

curl $(curl --data 'MyLanguages=sonid10&MySelectedVoice=Sharon&MyTextForTTS=Hello%20World&t=1&SendToVaaS=' 'http://www.acapela-group.com/demo-tts/DemoHTML5Form_V2.php' | grep -o "http.*mp3") > tts_output.mp3

지원되는 음성 중 일부는 다음과 같습니다.

  • 샤론
  • Ella (진정한 아이 목소리)
  • EmilioEnglish (진정한 아이 목소리)
  • Josh (진정한 아이 목소리)
  • 여자 이름
  • 케니 (인공적인 아이 목소리)
  • 로라
  • 미가
  • 넬리 (인공 아동 목소리)
  • 막대
  • 라이언
  • 사울
  • 스콧 (진짜 십대 목소리)
  • 트레이시
  • ValeriaEnglish (진정한 아이 음성)
  • 의지
  • WillBadGuy (감정적 인 목소리)
  • WillFromAfar (감정적 인 목소리)
  • WillHappy (감정적 인 목소리)
  • WillLittleCreature (감정적 인 목소리)
  • WillOldMan (감정적 인 목소리)
  • WillSad (감정적 인 목소리)
  • WillUpClose (감정적 인 목소리)

또한 여러 언어와 더 많은 음성을 지원합니다. http://www.acapela-group.com/


Google 텍스트 음성 변환

<!DOCTYPE html>
<html>
    <head>
        <script>
            function play(id){
            var text = document.getElementById(id).value;
            var url = 'http://translate.google.com/translate_tts?tl=en&q='+text;
            var a = new Audio(url);
                a.play();
            }
        </script>
    </head>
    <body>
        <input type="text" id="text" />
        <button onclick="play('text');"> Speak it </button>
    </body>
</html>

나는 이것을 다음과 같이 만들었습니다 : q = urlencode & tl = 언어 이름

이것을 시도하십시오 :

https://translate.google.com.vn/translate_tts?ie=UTF-8&q=%E0%A6%86%E0%A6%AE%E0%A6%BF%20%E0%A6%A4%E0%A7 % 8B % E0 % A6 % AE % E0 % A6 % BE % E0 % A6 % AF % E0 % A6 % BC % 20 % E0 % A6 % AD % E0 % A6 % BE % E0 % A6 % B2 % E0 % A7 % 8B % E0 % A6 % AC % E0 % A6 % BE % E0 % A6 % B8 % E0 % A6 % BF + & tl = bn & client = tw-ob


http://www.translate.google.com/translate_tts?tl=ko&q=Hello%20World 사용

www.translate.google.com을 참고하세요.


현재 Google 공식 텍스트 음성 변환 서비스는 https://cloud.google.com/text-to-speech/ 에서 사용할 수 있습니다.

처음 4 백만 자에 대해서는 무료입니다.


위의 URL을 사용했습니다. http://translate.google.com/translate_tts?tl=ko&q=Hello%20World

그리고 파이썬 라이브러리와 함께 요청했습니다. HTTP 403 FORBIDDEN

결국 나는 성공하기 위해 브라우저의User-Agent 헤더로 헤더 를 조롱 해야했다.


console.developer.google.com로그인으로 이동하여 API 키를 받거나 Microsoft bing의 API를 사용
https://msdn.microsoft.com/en-us/library/?f=255&MSPPError=-2147217396

하거나 AT & T의 음성 API developer.att.com(유료)
를 사용하여 음성 인식

Public Class Voice_recognition

    Public Function convertTotext(ByVal path As String, ByVal output As String) As String
        Dim request As HttpWebRequest = DirectCast(HttpWebRequest.Create("https://www.google.com/speech-api/v1/recognize?xjerr=1&client=speech2text&lang=en-US&maxresults=10"), HttpWebRequest)
        'path = Application.StartupPath & "curinputtmp.mp3"
        request.Timeout = 60000
        request.Method = "POST"
        request.KeepAlive = True
        request.ContentType = "audio/x-flac; rate=8000"  
        request.UserAgent = "speech2text"

        Dim fInfo As New FileInfo(path)
        Dim numBytes As Long = fInfo.Length
        Dim data As Byte()

        Using fStream As New FileStream(path, FileMode.Open, FileAccess.Read)
            data = New Byte(CInt(fStream.Length - 1)) {}
            fStream.Read(data, 0, CInt(fStream.Length))
            fStream.Close()
        End Using

        Using wrStream As Stream = request.GetRequestStream()
            wrStream.Write(data, 0, data.Length)
        End Using

        Try
            Dim response As HttpWebResponse = DirectCast(request.GetResponse(), HttpWebResponse)
            Dim resp = response.GetResponseStream()

            If resp IsNot Nothing Then
                Dim sr As New StreamReader(resp)
                MessageBox.Show(sr.ReadToEnd())

                resp.Close()
                resp.Dispose()
            End If
        Catch ex As System.Exception
            MessageBox.Show(ex.Message)
        End Try

        Return 0
    End Function
End Class

그리고 텍스트 음성 변환 : 이것을 사용하십시오 .


vbscript를 vb / C # 변환기로 사용하지 않았다면 이것을 이해할 것이라고 생각합니다 .
여전히 저에게 연락하지 않았습니다.

I have done this before ,can't find the code now that this why i'm not directly givin' you the code.


Because it came up in chat here , and the first page for googeling was this one, i decided to let all in on my findings googling some more XD

you really dont need to go any length anymore to make it work simply stand on the shoulders of giants:

there is a standard

https://dvcs.w3.org/hg/speech-api/raw-file/tip/webspeechapi.html

and an example

http://html5-examples.craic.com/google_chrome_text_to_speech.html

at least for your web projects this should work (e.g. asp.net)


#! /usr/bin/python2
# -*- coding: utf-8 -*-

def run(cmd):
    import os
    import sys
    from subprocess import Popen, PIPE
    print(cmd)
    proc=Popen(cmd, stdin=None, stdout=PIPE, stderr=None, shell=True)
    while True:
        data = proc.stdout.readline()   # Alternatively proc.stdout.read(1024)
        if len(data) == 0:
            print("Finished process")
            break
        sys.stdout.write(data)

import urllib

msg='Hello preety world'
msg=urllib.quote_plus(msg)
# -v verbosity
cmd='curl '+ \
    '--output tts_responsivevoice.mp2 '+ \
    "\""+'https://code.responsivevoice.org/develop/getvoice.php?t='+msg+'&tl=en-US&sv=g2&vn=&pitch=0.5&rate=0.5&vol=1'+"\""+ \
    ' -H '+"\""+'User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:44.0) Gecko/20100101 Firefox/44.0'+"\""+ \
    ' -H '+"\""+'Accept: audio/webm,audio/ogg,audio/wav,audio/*;q=0.9,application/ogg;q=0.7,video/*;q=0.6,*/*;q=0.5'+"\""+ \
    ' -H '+"\""+'Accept-Language: pl,en-US;q=0.7,en;q=0.3'+"\""+ \
    ' -H '+"\""+'Range: bytes=0-'+"\""+ \
    ' -H '+"\""+'Referer: http://code.responsivevoice.org/develop/examples/example2.html'+"\""+ \
    ' -H '+"\""+'Cookie: __cfduid=ac862i73b6a61bf50b66713fdb4d9f62c1454856476; _ga=GA1.2.2126195996.1454856480; _gat=1'+"\""+ \
    ' -H '+"\""+'Connection: keep-alive'+"\""+ \
    ''
print('***************************')
print(cmd)
print('***************************')
run(cmd)

Line:

/getvoice.php?t='+msg+'&tl=en-US&sv=g2&vn=&pitch=0.5&rate=0.5&vol=1'+"\""+ \

is responsible for language.

tl=en-US

There is another preety interesting site with tts engines that can be used in this manner.

substitute o for null iv0na.c0m

have a nice day

참고URL : https://stackoverflow.com/questions/9893175/google-text-to-speech-api

반응형