Based on Transcribe large amounts of audio data with Batch Transcription - Learn | Microsoft Docs
by Mark05E
- Powershell
- Azure CLI
- Azure Subscription
# Get and set the subscription and Resource Group
$subscription = az account list --query [0].id -o tsv
$resourceGroupName = az group list --query "[0] | name" -o tsv
# Create the cognitive services account
az cognitiveservices account create `
--name cognitive-services-account-resource-speech `
--resource-group $resourceGroupName `
--kind SpeechServices `
--sku S0 `
--location westus2 `
--subscription $subscription `
--yes
# Create a blob and container to hold our audio files
# Create blob
$lastchars = $resourceGroupName.Substring($resourceGroupName.Length - 10)
$blobName = "blob$lastchars"
az storage account create `
--name $blobName `
--resource-group $resourceGroupName `
--location westus2 `
--sku Standard_ZRS
# Create container
$blobContainerName = "container$lastchars"
$blobConnectionString = az storage account show-connection-string -g $resourceGroupName -n $blobName --query "connectionString" -o tsv
az storage container create `
--name $blobContainerName `
--public-access blob `
--connection-string $blobConnectionString
# Download sample audio files
git clone https://github.com/MicrosoftDocs/mslearn-batch-stt.git
# Copy files to storage container
az extension add --name storage-preview
az storage azcopy blob upload -c $blobContainerName --account-name $blobName -s "mslearn-batch-stt/audiofiles/*" --recursive
# Generate SAS Token
$ts = New-TimeSpan -Hours 1
$end = get-date((get-date) + $ts).ToUniversalTime() -format "yyyy-mm-ddTHH:ssZ"
$sasToken = az storage container generate-sas -n $blobContainerName --permissions rwl --expiry $end --connection-string $blobConnectionString -o tsv
"Our token is: $sasToken"
# Generate API Key
$apiKeySpeech=az cognitiveservices account keys list -g $resourceGroupName -n cognitive-services-account-resource-speech --query [key1] -o tsv
"Our Key Is: $apiKeySpeech"
# Prepare JSON payload
$contentContainerUrl="https://$blobName.blob.core.windows.net/$blobContainerName/?$sasToken"
$json=@'
{
"contentContainerUrl": "$contentContainerUrl",
"properties": {
"diarizationEnabled": false,
"wordLevelTimestampsEnabled": false,
"punctuationMode": "None",
"profanityFilterMode": "None",
"duration": "PT0S",
"timeToLive": "P1D"
},
"locale": "en-US",
"displayName": "Batch transcription"
}
'@
$json = $json.Replace('$contentContainerUrl',$contentContainerUrl)
$json = $json.Replace("""","'") # for curl on windows compatibility
# Submit the job
$response = curl -X POST "https://westus2.api.cognitive.microsoft.com/speechtotext/v3.0/transcriptions" `
-H "Content-Type:application/json" `
-H "Ocp-Apim-Subscription-Key:$apiKeySpeech" `
--data "$json"
# Check response
"$response"
# Check status of transcriptions
$info_uri = $response | ConvertFrom-Json | Select-Object -ExpandProperty self
$job_information = curl -X GET $info_uri -H "Ocp-Apim-Subscription-Key:$apiKeySpeech"
$job_information
# this part is different from MS docs
$result_info_uri = ($job_information | ConvertFrom-Json).links.files
$result_continue = $true
while ($result_continue){
$transcription_information = curl -X GET $result_info_uri -H "Ocp-Apim-Subscription-Key:$apiKeySpeech"
$transcript_uris = $transcription_information | ConvertFrom-Json
foreach ($value in $transcript_uris.values ){
"$($value.name) --> $($value.links.contentUrl)"
curl -X GET $($value.links.contentUrl) --output $($value.name)
}
If ( $transcript_uris.'@nextLink' -ne $null) {
$result_info_uri = $transcript_uris.'@nextLink';
$result_continue = $true
}
else {$result_continue = $false}
}