Created
December 9, 2023 01:05
-
-
Save xunker/f6c2e324f4bd30e0e787d83b8506ec24 to your computer and use it in GitHub Desktop.
Make a single MP3 audio file of all the voices from MacOS `say` command
This file contains hidden or 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 | |
# set -o xtrace | |
# generates an MP3 file of every voice in MacOS saying the appropriate 'Hello' | |
# Requires ffmpeg to be install, by homebrew is preferred. | |
OUTPUT="everyone_hello" # extension will be added below | |
OUTPUT_AIFF="$OUTPUT.aiff" | |
OUTPUT_MP3="$OUTPUT.mp3" | |
RATE=300 # speed of speech: higher number is faster | |
GAP_LENGTH=3 # length of silence between each voice | |
TEMP_FILE="hello_temp.aiff" | |
# It first outputs the work to lossless .aiff files, and then converts the final to .mp3 | |
# Make a bunch of .aiff files, one for each voice in say(1) | |
say -v \? | awk 'BEGIN { FS = ",|、| +"} { print $1, $4 }' | sed "s/'//g" | while read LINE; do | |
echo $LINE | |
NAME=$(echo $LINE | awk 'BEGIN { FS = " " } { print $1 }') | |
FN=hello_$NAME.aiff | |
echo $FN | |
HELLO=$(echo $LINE | awk 'BEGIN { FS = " " } { print $2 }' | sed 's/Most/Hello/' | sed "s/Isnt/Hello/g" | sed "s/I$/Hello/g") | |
HELLO="$NAME says $HELLO" | |
echo $HELLO | |
say -o "$FN" -r $RATE -v $NAME "$HELLO" | |
done | |
# Generate a spacer of silence. Change `-t` increase/decrease time | |
ffmpeg -f lavfi -i anullsrc=r=44100:cl=mono -t $GAP_LENGTH -y silence.aiff | |
# create the initial output file | |
ffmpeg -i silence.aiff -filter_complex '[0:0]concat=n=1:v=0:a=1[out]' -map '[out]' -y $OUTPUT_AIFF | |
for FILENAME in hello_*.aiff; do | |
echo $FILENAME | |
ffmpeg -i $OUTPUT_AIFF -i $FILENAME -i silence.aiff -filter_complex '[0:0][1:0][2:0]concat=n=3:v=0:a=1[out]' -map '[out]' -y $TEMP_FILE | |
mv $TEMP_FILE $OUTPUT_AIFF | |
rm -v $FILENAME | |
done | |
# convert output to mp3 | |
ffmpeg -i $OUTPUT_AIFF -y $OUTPUT_MP3 | |
# cleanup | |
rm -v $OUTPUT_AIFF | |
rm -v silence.aiff | |
echo "wrote $OUTPUT_MP3. done." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment