Skip to content

Instantly share code, notes, and snippets.

@ivanistheone
Last active January 5, 2023 14:55
Show Gist options
  • Save ivanistheone/de3ccb244224d101bb93320fee1cab39 to your computer and use it in GitHub Desktop.
Save ivanistheone/de3ccb244224d101bb93320fee1cab39 to your computer and use it in GitHub Desktop.
This script converts a plain text file, e.g. article.txt into a mp3 audiobook using the MacOS text-to-speech accessibility command-line tool `say`. Adjust the `VOICE` and `RATE` parameters to customize to your liking. Note this requires running on MacOS.
#!/usr/bin/env bash
set -e
# This script converts any text file into a mp3 audiobook using the MacOS
# text-to-speech accessibility command-line tool `say`.
# Adjust the `VOICE` and `RATE` parameters to customize to your liking:
VOICE="Alex"
RATE="295" # pretty fast
if [ $# -eq 0 ]; then
echo "Usage: $0 article.txt";
exit 1
fi
txtfilename="$1"
aifffilename="$(basename -- "$txtfilename" .txt).aiff"
mp3filename="$(basename -- "$txtfilename" .txt).mp3"
echo "Convering $txtfilename to mp3..."
# 1. Speak the text file, saving as an .aiff audio file
say -v $VOICE -r $RATE -f "$txtfilename" -o "$aifffilename"
# 2. Convert the .aiff to .mp3
ffmpeg -hide_banner -loglevel error \
-i "$aifffilename" \
-b:a 128k "$mp3filename"
# 3. Remove the .aiff file
rm "$aifffilename"
echo "Done. Result saved to $PWD/$mp3filename"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment