Skip to content

Instantly share code, notes, and snippets.

@mark05e
Created June 8, 2022 20:09
Show Gist options
  • Save mark05e/b244329a0dd3ed91ba6a2711b7dcb2f0 to your computer and use it in GitHub Desktop.
Save mark05e/b244329a0dd3ed91ba6a2711b7dcb2f0 to your computer and use it in GitHub Desktop.

Transcribe Audio Files Using Azure

Based on Transcribe large amounts of audio data with Batch Transcription - Learn | Microsoft Docs

by Mark05E

Requirements

  1. Powershell
  2. Azure CLI
  3. Azure Subscription

Steps

1. Prepare Environment

# 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

2. Load audio files into environment

# 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

3. Set up access keys and tokens

# 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"

4. Submit the job

# 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

5. Viewing the results

# 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}
}

Written with StackEdit by Mark05E.

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