Skip to content

Instantly share code, notes, and snippets.

@nathanlesage
Created January 17, 2022 22:23
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nathanlesage/4ad5ae074b02456920dcdd4c02ac2892 to your computer and use it in GitHub Desktop.
Save nathanlesage/4ad5ae074b02456920dcdd4c02ac2892 to your computer and use it in GitHub Desktop.
A small utility function to quickly convert a text file into a spoken track
# TTS COMMAND
#
# This command is a handy utility that allows you to quickly convert
# a text file into a speech recording.
#
# NOTE: This will only work on macOS, since it relies on the `say`
# command which utilizes Apple's speech synthesis system.
#
# REQUIRED: ffmpeg. You can easily install this using `brew install ffmpeg`
#
# HOW TO: Put the below function into your .zshrc or .bashrc so that
# it is loaded when you start the terminal.
# Then, you will have the command `tts` at your disposal.
#
# Simply execute `tts infile.txt outfile.mp3`
# You can also use a different format, if you like.
function tts () {
# Require two arguments
if [ "$#" -ne 2 ]
then
echo "Usage: $0 infile.txt outfile.mp3" >&2
exit 1
fi
# Echo a little bit of info
echo "Running text-to-speech ..."
echo ""
echo " - Input text file: $1"
echo " - Output audio: $2"
echo ""
TEMPFILE="/Users/$USER/temp.aiff"
echo "Using $TEMPFILE as temporary file."
# First run `say` and put the output into temp.aiff
say -f "$1" -o "$TEMPFILE" --progress
# Then, run `ffmpeg` to confert the aiff into mp3
ffmpeg -i "$TEMPFILE" "$2"
# Lastly, remove the tempfile as a convenience
echo "Removing $TEMPFILE ..."
rm "$TEMPFILE"
# Always end with something nice to say!
echo "Done! Have a good day."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment