Skip to content

Instantly share code, notes, and snippets.

@andreibosco
Created March 18, 2021 19:21
Show Gist options
  • Save andreibosco/34d2b517ec9e8975056e5e72e52faa21 to your computer and use it in GitHub Desktop.
Save andreibosco/34d2b517ec9e8975056e5e72e52faa21 to your computer and use it in GitHub Desktop.
Powershell script to generate audio using google text-to-speech service
# 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"
@mxav1111
Copy link

mxav1111 commented Dec 12, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment