Skip to content

Instantly share code, notes, and snippets.

@vladignatyev
Created November 10, 2023 10:04
Show Gist options
  • Save vladignatyev/537a26554fd043742defca77c8eeb819 to your computer and use it in GitHub Desktop.
Save vladignatyev/537a26554fd043742defca77c8eeb819 to your computer and use it in GitHub Desktop.
Say AI: Linux's `say` command-line tool for voice over using OpenAI TTS model.
#!/bin/bash
# Function to display help
usage() {
echo "Usage: $0 [-o output_file] [-v voice] [-m model] [-i input_file] \"input_string\""
exit 1
}
# Default values
output_file="speech.mp3"
voice="echo"
model="tts-1"
input_file=""
input_string=""
# Parse command-line options
while getopts 'o:v:m:i:h' flag; do
case "${flag}" in
o) output_file="${OPTARG}" ;;
v) voice="${OPTARG}" ;;
m) model="${OPTARG}" ;;
i) input_file="${OPTARG}" ;;
h) usage ;;
*) usage ;;
esac
done
# Check for remaining arguments
if [ -z "$input_file" ]; then
if [ $OPTIND -gt $# ]; then
echo "Error: Missing input string"
usage
else
input_string="${@:$OPTIND:1}"
fi
else
if [ -f "$input_file" ]; then
input_string=$(<"$input_file")
else
echo "Error: Input file does not exist"
exit 1
fi
fi
# Prepare parameters for OpenAI API call
PARAM=$(jq -n -c --arg model "$model" --arg voice "$voice" --arg input "$input_string" '$ARGS.named')
# Call OpenAI API to generate speech
curl https://api.openai.com/v1/audio/speech \
-H "Authorization: Bearer $OPENAI_API_KEY" \
-H "Content-Type: application/json" \
-d "$PARAM" \
--output "$output_file"
@vladignatyev
Copy link
Author

You have to provide your OPENAI_API_KEY via environmental variable. Simple way to do that is export OPENAI_API_KEY=your_api_key.

@vladignatyev
Copy link
Author

vladignatyev commented Nov 10, 2023

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