Skip to content

Instantly share code, notes, and snippets.

@carsontang
Created March 27, 2018 23:05
Show Gist options
  • Save carsontang/67bd989ac6a855af4d71cbbc86712206 to your computer and use it in GitHub Desktop.
Save carsontang/67bd989ac6a855af4d71cbbc86712206 to your computer and use it in GitHub Desktop.
A script that converts text-to-speech
#!/usr/bin/env python
import base64
import sys
import requests
def main(text, filename):
r = requests.post(
"https://cxl-services.appspot.com/proxy?url=https%3A%2F%2Ftexttospeech.googleapis.com%2Fv1beta1%2Ftext%3Asynthesize",
json={
"input": {
"ssml": "<speak>%s</speak>" % text
},
"voice": {
"languageCode": "en-US",
"name": "en-US-Wavenet-F"
},
"audioConfig": {
"audioEncoding": "MP3",
"pitch": "0.0",
"speakingRate": "1.00"
}
}
)
data = base64.b64decode(r.json()["audioContent"])
with open(filename, "wb") as outfile:
outfile.write(data)
if __name__ == "__main__":
if len(sys.argv) != 3:
print("Usage: google-tts.py <text> <output MP3 filename>")
sys.exit(1)
main(sys.argv[1], sys.argv[2])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment