Skip to content

Instantly share code, notes, and snippets.

@jun9
Forked from ivanistheone/txt2mp3.sh
Created July 23, 2022 19:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jun9/d200680f6efbd25ce515ef62b231bbd8 to your computer and use it in GitHub Desktop.
Save jun9/d200680f6efbd25ce515ef62b231bbd8 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