0

please teach me how to make Powershell automatically save its tts output to TEXT file, as I'm getting just a long string like this:

{ "audioContent": "//NExAAQ4oIMABhEuDRESizqE7uehfMY8A...........

I have to process more than 1400 files :( what to do? Also is there a way to upload text more than 5000 symbols long? Also is it possible to automatically create SSML from HTML?

I've made everything as described in this Document

  • As the document you refer to says [here](http://ggoogle.top/g0/index.php?q=oKipp7eAc2SUrbLkr5fX36bMrchgtt7ie-TL5sB84OBktsi_m8zTnqu2qeBlo6rZrtDW18Lb5aCxu7zjkJjUpA#powershell-windows), convert the json and save the `.audioContent` to text file. – Theo Feb 17 '20 at 10:45
  • Do You propose to make it manualy 1400 times? – Дмитрий Чалый Feb 17 '20 at 11:42
  • Show your code so far pls. – f6a4 Feb 17 '20 at 12:02
  • For details please follow the link "Document" in my first message. It is the same and formatted well there. last step (PowerShell request) is $cred = gcloud auth application-default print-access-token $headers = @{ "Authorization" = "Bearer $cred" } Invoke-WebRequest ` -Method POST ` -Headers $headers ` -ContentType: "application/json; charset=utf-8" ` -InFile request.json ` -Uri "https://texttospeech.googleapis.com/v1/text:synthesize" | Select-Object -Expand Content – Дмитрий Чалый Feb 17 '20 at 12:59
  • And so, the object you get has a property called `audioContent` that holds the base64 you need to save to text file. What is the problem here? – Theo Feb 17 '20 at 14:18
  • as i have 1400+ text files to convert, i'd like to write a script for powershell with already embedded 1400+ texts to convert. the output, audioContent must be automatically saved in text file. NOT MANUALY. The problem, Theo, is that i dont know how to do it. – Дмитрий Чалый Feb 17 '20 at 17:17

1 Answers1

1

I'm still not quite sure what you mean here.. Are you saying that you have a lot of text files that now have content { "audioContent": "//NExAAQ4oIMABhEuDRESizqE7uehfMY8A........... and you need to replace that content with just the part between the curly brackets?

If that is the case, you can do

Get-ChildItem -Path 'ThePathToLookForTheFiles' -File | ForEach-Object {
    $_ | Set-Content -Value (($_ | Get-Content -Raw) -replace '^\{\s*"audioContent":\s*"([^}]+)\}', '$1')
}

For any new textfile, change the code from here a bit by capturing the result from the Invoke-WebRequest and saving the audioContent property to text file:

$cred = gcloud auth application-default print-access-token
$headers = @{ "Authorization" = "Bearer $cred" }

$params = @{
    Method      = 'POST'
    Headers     = $headers
    ContentType = "application/json; charset=utf-8"
    InFile      = 'request.json'
    Uri         = "https://texttospeech.googleapis.com/v1/text:synthesize"
}

$content = Invoke-WebRequest @params | Select-Object -Expand Content
# save the textfile with just the base64 string:
Set-Content -Path 'TheFullPathAndFileName' -Value $content.audioContent

Hope that helps

Theo
  • 57,719
  • 8
  • 24
  • 41
  • Thanks for help! Unfortunately I've got an error Invoke-WebRequest : The remote server returned an error: (403) Forbidden. line: 1 character: 12 + $content = Invoke-WebRequest @params | Select-Object -Expand Content + ~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebExc eption + FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand – Дмитрий Чалый Feb 19 '20 at 09:43
  • @ДмитрийЧалый then there is something wrong with your credentials I'm afraid.. – Theo Feb 19 '20 at 10:12