Skip to content

Instantly share code, notes, and snippets.

@megaherz
Last active November 14, 2018 10:54
Show Gist options
  • Save megaherz/f2bf3095c7cb02e351871a1f881b291e to your computer and use it in GitHub Desktop.
Save megaherz/f2bf3095c7cb02e351871a1f881b291e to your computer and use it in GitHub Desktop.
Google text-to-speech from file in terminal
#!/usr/bin/env bash
# jq and gcloud are prerequisites
# voices: https://cloud.google.com/text-to-speech/docs/voices
export GOOGLE_APPLICATION_CREDENTIALS="gcloud.json" # Create your own
accessToken=$(gcloud auth application-default print-access-token)
translate() {
curl --silent -H "Authorization: Bearer "${accessToken} \
-H "Content-Type: application/json; charset=utf-8" \
--data "{
'input':{
'text':'$2'
},
'voice':{
'languageCode':'en-gb',
'name':'en-GB-Standard-A',
'ssmlGender':'FEMALE'
},
'audioConfig':{
'audioEncoding':'MP3'
}
}" "https://texttospeech.googleapis.com/v1/text:synthesize" | jq '.audioContent' -r | base64 --decode > audio/$1.mp3
}
while IFS=' ' read -r id text
do
echo ${id}
translate ${id} ${text}
done < vocab.txt
id1 tea
id2 apple
id3 banana
id4 beer
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment