【问题标题】:How to save Powershell's output to file when I create synthetic voice audio with Text-to-Speech using the command line当我使用命令行使用文本到语音创建合成语音音频时,如何将 Powershell 输出保存到文件
【发布时间】:2020-02-19 00:23:33
【问题描述】:

请教我如何让 Powershell 自动将其 tts 输出保存到 TEXT 文件,因为我得到的只是一个像这样的长字符串:

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

我必须处理超过 1400 个文件 :( 怎么办? 还有一种方法可以上传超过 5000 个符号的文本吗? 也可以从 HTML 自动创建 SSML 吗?

我已经按照Document中的描述做了所有事情

【问题讨论】:

  • 正如您参考的文档所说here,转换json并将.audioContent保存为文本文件。
  • 您建议手动制作 1400 次吗?
  • 请显示您的代码。
  • 详情请点击我第一条消息中的“文档”链接。它在那里是相同的并且格式很好。最后一步(PowerShell 请求)是 $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 "texttospeech.googleapis.com/v1/text:synthesize" |选择对象-展开内容
  • 因此,您获得的对象有一个名为audioContent 的属性,其中包含您需要保存到文本文件的base64。这里有什么问题?

标签: powershell ssml gtts


【解决方案1】:

我仍然不太清楚你在这里的意思。你是说你有很多文本文件现在有内容{ "audioContent": "//NExAAQ4oIMABhEuDRESizqE7uehfMY8A...........,你需要用大括号之间的部分替换这些内容吗?

如果是这样,你可以这样做

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

对于任何新的文本文件,通过捕获来自Invoke-WebRequest 的结果并将audioContent 属性保存到文本文件中,稍微更改here 的代码:

$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

希望有帮助

【讨论】:

  • 感谢您的帮助!不幸的是我有一个错误 Invoke-WebRequest :远程服务器返回一个错误:(403)禁止。行:1 个字符: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
  • @ДмитрийЧалый 那么恐怕您的凭据有问题..
猜你喜欢
  • 1970-01-01
  • 2019-11-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-01
  • 2023-03-03
  • 1970-01-01
  • 2023-04-10
相关资源
最近更新 更多