Created
November 10, 2023 10:04
-
-
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.
This file contains 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
#!/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" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Read an original story for more information on usage, thoughts and comments: https://medium.com/@v_ignatyev/text-to-speech-on-linux-mac-using-bash-made-by-chatgpt-4-570ade59c967