|
| 1 | +# Transcribe audio to text with Amazon Transcribe |
| 2 | + |
| 3 | +This tutorial shows you how to create a sample audio file, upload it to Amazon S3, start a transcription job with Amazon Transcribe, wait for the job to complete, retrieve the results, and list recent transcription jobs. |
| 4 | + |
| 5 | +## Prerequisites |
| 6 | + |
| 7 | +- AWS CLI configured with credentials and a default region |
| 8 | +- Python 3 installed (used to generate a WAV file) |
| 9 | +- Permissions for `transcribe:StartTranscriptionJob`, `transcribe:GetTranscriptionJob`, `transcribe:ListTranscriptionJobs`, `transcribe:DeleteTranscriptionJob`, `s3:CreateBucket`, `s3:PutObject`, `s3:DeleteObject`, `s3:DeleteBucket` |
| 10 | + |
| 11 | +## Step 1: Create a sample audio file |
| 12 | + |
| 13 | +Generate a 1-second WAV file containing silence using Python. This gives Transcribe a valid audio file to process without needing an external recording. |
| 14 | + |
| 15 | +```bash |
| 16 | +python3 -c " |
| 17 | +import struct, wave |
| 18 | +with wave.open('/tmp/sample.wav', 'w') as w: |
| 19 | + w.setnchannels(1) |
| 20 | + w.setsampwidth(2) |
| 21 | + w.setframerate(16000) |
| 22 | + w.writeframes(struct.pack('<' + 'h' * 16000, *([0] * 16000))) |
| 23 | +" |
| 24 | +``` |
| 25 | + |
| 26 | +The file is 16 kHz mono PCM, which is the recommended format for Amazon Transcribe. One second of silence produces a ~32 KB file. |
| 27 | + |
| 28 | +## Step 2: Upload to S3 |
| 29 | + |
| 30 | +Create an S3 bucket and upload the audio file. Transcribe reads input from S3. |
| 31 | + |
| 32 | +```bash |
| 33 | +BUCKET_NAME="transcribe-tut-$(openssl rand -hex 4)-$(aws sts get-caller-identity --query 'Account' --output text)" |
| 34 | + |
| 35 | +aws s3api create-bucket --bucket "$BUCKET_NAME" |
| 36 | +aws s3 cp /tmp/sample.wav "s3://$BUCKET_NAME/sample.wav" --quiet |
| 37 | +``` |
| 38 | + |
| 39 | +For regions other than `us-east-1`, the script adds `--create-bucket-configuration LocationConstraint=$REGION`. |
| 40 | + |
| 41 | +## Step 3: Start a transcription job |
| 42 | + |
| 43 | +Start an asynchronous transcription job pointing to the uploaded audio. |
| 44 | + |
| 45 | +```bash |
| 46 | +JOB_NAME="tut-job-$(openssl rand -hex 4)" |
| 47 | + |
| 48 | +aws transcribe start-transcription-job \ |
| 49 | + --transcription-job-name "$JOB_NAME" \ |
| 50 | + --language-code en-US \ |
| 51 | + --media "MediaFileUri=s3://$BUCKET_NAME/sample.wav" \ |
| 52 | + --output-bucket-name "$BUCKET_NAME" \ |
| 53 | + --query 'TranscriptionJob.{Name:TranscriptionJobName,Status:TranscriptionJobStatus}' \ |
| 54 | + --output table |
| 55 | +``` |
| 56 | + |
| 57 | +`--language-code` specifies the language of the audio. `--output-bucket-name` tells Transcribe where to write the JSON result file. Without it, Transcribe uses a service-managed bucket. |
| 58 | + |
| 59 | +## Step 4: Wait for completion |
| 60 | + |
| 61 | +Poll the job status until it reaches `COMPLETED` or `FAILED`. |
| 62 | + |
| 63 | +```bash |
| 64 | +for i in $(seq 1 30); do |
| 65 | + STATUS=$(aws transcribe get-transcription-job \ |
| 66 | + --transcription-job-name "$JOB_NAME" \ |
| 67 | + --query 'TranscriptionJob.TranscriptionJobStatus' --output text) |
| 68 | + echo " Status: $STATUS" |
| 69 | + [ "$STATUS" = "COMPLETED" ] || [ "$STATUS" = "FAILED" ] && break |
| 70 | + sleep 5 |
| 71 | +done |
| 72 | +``` |
| 73 | + |
| 74 | +Most short audio files complete within 15–30 seconds. The script polls every 5 seconds with a 150-second timeout. |
| 75 | + |
| 76 | +## Step 5: Get results |
| 77 | + |
| 78 | +Retrieve the transcript URI from the completed job. |
| 79 | + |
| 80 | +```bash |
| 81 | +aws transcribe get-transcription-job \ |
| 82 | + --transcription-job-name "$JOB_NAME" \ |
| 83 | + --query 'TranscriptionJob.Transcript.TranscriptFileUri' --output text |
| 84 | +``` |
| 85 | + |
| 86 | +The result is a JSON file in your S3 bucket containing the transcript text, confidence scores, and word-level timestamps. Since the input was silence, the transcript will be empty or minimal. |
| 87 | + |
| 88 | +## Step 6: List transcription jobs |
| 89 | + |
| 90 | +List recent completed transcription jobs. |
| 91 | + |
| 92 | +```bash |
| 93 | +aws transcribe list-transcription-jobs --status COMPLETED \ |
| 94 | + --query 'TranscriptionJobSummaries[:3].{Name:TranscriptionJobName,Status:TranscriptionJobStatus,Created:CreationTime}' \ |
| 95 | + --output table |
| 96 | +``` |
| 97 | + |
| 98 | +You can filter by `--status` (`QUEUED`, `IN_PROGRESS`, `COMPLETED`, `FAILED`) and by `--job-name-contains` to find specific jobs. |
| 99 | + |
| 100 | +## Cleanup |
| 101 | + |
| 102 | +Delete the transcription job and the S3 bucket: |
| 103 | + |
| 104 | +```bash |
| 105 | +aws transcribe delete-transcription-job --transcription-job-name "$JOB_NAME" |
| 106 | +aws s3 rm "s3://$BUCKET_NAME" --recursive |
| 107 | +aws s3 rb "s3://$BUCKET_NAME" |
| 108 | +``` |
| 109 | + |
| 110 | +Amazon Transcribe charges per second of audio transcribed. This tutorial transcribes 1 second of audio, costing a fraction of a cent. The S3 bucket is also deleted during cleanup. |
| 111 | + |
| 112 | +The script automates all steps including cleanup: |
| 113 | + |
| 114 | +```bash |
| 115 | +bash amazon-transcribe-gs.sh |
| 116 | +``` |
| 117 | + |
| 118 | +## Related resources |
| 119 | + |
| 120 | +- [Getting started with Amazon Transcribe](https://docs.aws.amazon.com/transcribe/latest/dg/getting-started.html) |
| 121 | +- [Amazon Transcribe API reference](https://docs.aws.amazon.com/transcribe/latest/APIReference/Welcome.html) |
| 122 | +- [Supported languages](https://docs.aws.amazon.com/transcribe/latest/dg/supported-languages.html) |
| 123 | +- [Amazon Transcribe pricing](https://aws.amazon.com/transcribe/pricing/) |
0 commit comments