File size: 870 Bytes
e569c5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
import requests
import os
import base64
import json
os.environ["GOOGLE_APPLICATION_CREDENTIALS"]="./api.json"

url = "https://texttospeech.googleapis.com/v1/text:synthesize"
headers = {'Content-Type': 'application/json; charset=utf-8',
           'X-Goog-Api-Key': 'synclub-2383kjhjksxfv.2341gs' # 待补充
          }
text = "二月の下旬に差し掛かる頃だった。"

data = {
    "input":{
        "text":text
    },
    "voice":{
        "languageCode":"ja-JP",
        "name":"ja-JP-Neural2-C",
        "ssmlGender":"MALE"
    },
    "audioConfig":{
        "audioEncoding":"MP3"
    }
    }
response = requests.post(url, headers=headers, json=data)
response = response.json()
print(response)
audio = response['audioContent']
audio = base64.b64decode(audio)
# The response's audio_content is binary.
with open("test.mp3", "wb") as out:
    out.write(audio)