Create a gist now

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Convert Speech to Text with PowerShell and Cognitive Services STT
# Audio Phrase
$audofile = Get-ChildItem "C:\temp\speech2convert.wav"
# Read audio into byte array
$audioBytes = [System.IO.File]::ReadAllBytes($audofile)
# API Key
$key1 = "your api key"
# Token URI
$tokenuri = "https://api.cognitive.microsoft.com/sts/v1.0/issueToken"
# Conversion URI
$conversionURI = "https://speech.platform.bing.com/speech/recognition/interactive/cognitiveservices/v1?language=en-us&format=detailed"
# Generate Request Auth Headers
$TokenHeaders = @{"Ocp-Apim-Subscription-Key" = $key1;
"Content-Length"= "0";
"Content-type" = "application/x-www-form-urlencoded"}
# Get OAuth Token
$OAuthToken = Invoke-RestMethod -Method POST -Uri $tokenuri -Headers $TokenHeaders
# Conversion Headers
$Headers = @{
'Ocp-Apim-Subscription-Key' = $key1;
'Transfer-Encoding' = 'chunked';
'Content-type' = 'audio/pcm; codec=audio/pcm; samplerate=16000'
}
# Convert
$TextResponse = Invoke-RestMethod -Method POST -Uri $conversionURI -Headers $Headers -Body $audioBytes
# Result
$TextResponse.NBest.Lexical
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment