Created
March 18, 2021 19:21
-
-
Save andreibosco/34d2b517ec9e8975056e5e72e52faa21 to your computer and use it in GitHub Desktop.
Powershell script to generate audio using google text-to-speech service
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requirements: | |
# - active google cloud project | |
# - enable 'Cloud Text-to-Speech API' | |
# - GOOGLE_APPLICATION_CREDENTIALS defined as an environment variable, pointing to your JSON account key file | |
# - Google Cloud SDK installed and initialized | |
# Based on official quickstart example: https://cloud.google.com/text-to-speech/docs/quickstart-protocol | |
$dialogue = Read-Host -Prompt 'Dialogue text' | |
$outputName = Read-Host -Prompt 'Output file name (.mp3 extension will be added automatically)' | |
$cred = gcloud auth application-default print-access-token | |
$headers = @{ "Authorization" = "Bearer $cred" } | |
@{input=@{text=$dialogue};voice=@{languageCode="en-US";name="en-US-Wavenet-C"; ssmlGender="FEMALE"};audioConfig=@{audioEncoding="MP3"}} | ConvertTo-Json -Compress | Out-File -FilePath request-temp.json | |
Invoke-WebRequest ` | |
-Method POST ` | |
-Headers $headers ` | |
-ContentType: "application/json; charset=utf-8" ` | |
-InFile request-temp.json ` | |
-Uri "https://texttospeech.googleapis.com/v1/text:synthesize" | Select-Object -Expand Content | Out-File -FilePath response.json | |
$JSONContent = Get-Content -Path "response.json" | ConvertFrom-JSON -AsHashtable | |
$JSONContent.Values | Out-File -FilePath audioContent.base64 | |
certutil -decode audioContent.base64 $outputName".mp3" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Hi there,
Is there a way to convert or add feature to accept file name (with text ) that can be converted to .mp3
Thanks for your help.
-Max.