Created
August 6, 2026 09:34
-
-
Save gretel/47c02be001fb65d1cf06554e40a30057 to your computer and use it in GitHub Desktop.
AWS Polly TTS demo
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bash | |
| # | |
| # AWS Polly TTS demo | |
| # Usage: polly.sh [--voice ID] [--out FILE] [--no-play] "text" | echo text | polly.sh | |
| # | |
| set -euo pipefail | |
| export AWS_PAGER="" # no less(1) between you and the JSON | |
| REGION="${AWS_REGION:-eu-west-1}" | |
| VOICE=Vicki; OUT=out.mp3; PLAY=1 | |
| PLAY_CMD=""; for c in afplay paplay aplay ffplay; do command -v "$c" >/dev/null && PLAY_CMD="$c" && break; done | |
| while [ $# -gt 0 ]; do | |
| case "$1" in | |
| --voice) VOICE="$2"; shift 2 ;; | |
| --out) OUT="$2"; shift 2 ;; | |
| --no-play) PLAY=0; shift ;; | |
| -h|--help) sed -n '2,3p' "$0"; exit 0 ;; | |
| *) TEXT="${TEXT:+$TEXT }$1"; shift ;; | |
| esac | |
| done | |
| TEXT="${TEXT:-$(cat)}" | |
| [ -n "$TEXT" ] || { echo "no text given" >&2; exit 1; } | |
| aws sts get-caller-identity >/dev/null 2>&1 || { echo "AWS session not active. Run: aws login" >&2; exit 1; } | |
| mkdir -p "$(dirname "$OUT")" | |
| aws --region "$REGION" polly synthesize-speech --voice-id "$VOICE" --engine neural \ | |
| --output-format mp3 --text "$TEXT" "$OUT" | |
| if [ "$PLAY" = 1 ] && [ -n "$PLAY_CMD" ]; then | |
| case "$PLAY_CMD" in | |
| ffplay) ffplay -nodisp -autoexit "$OUT" >/dev/null 2>&1 ;; | |
| *) "$PLAY_CMD" "$OUT" ;; | |
| esac | |
| fi | |
| ls -lh "$OUT" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment