Skip to content

Instantly share code, notes, and snippets.

@heavyengineer
Created November 2, 2019 17:03
Show Gist options
  • Save heavyengineer/ce59dcea027f977d6997c16fb3a1f2fd to your computer and use it in GitHub Desktop.
Save heavyengineer/ce59dcea027f977d6997c16fb3a1f2fd to your computer and use it in GitHub Desktop.
get text to speech as files from gcloud
#!/bin/bash
# supply a string of text to be encoded
# e.g. ./getGcloudTTs.sh $(cat speech.txt)
timestamp=`date +"%s"`
outputfilename=audio_${timestamp}.mp3
data()
{
cat <<EOF
{
'input':{
'ssml':"<speak>$*</speak>"
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}
EOF
}
curl -H "Authorization: Bearer $(gcloud auth print-access-token)" \
-H "Content-Type: application/json; charset=utf-8" --data "$(data $*)" \
"https://texttospeech.googleapis.com/v1/text:synthesize" > /tmp/synthesize-ssml.txt
cat /tmp/synthesize-ssml.txt | grep 'audioContent' | \
sed 's|audioContent| |' | tr -d '\n ":{},' > /tmp/tmp.txt && \
base64 /tmp/tmp.txt --decode > /home/steev/gcloudTTS/${outputfilename} && \
rm /tmp/tmp.txt
#cat /tmp/synthesize-ssml.txt
vlc file:///home/steev/gcloudTTS/${outputfilename}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment