bash : ttsTXT.sh

#!/bin/sh

usage () {
echo "
Usage:
$0 --name \"TTS-TNM@1\" --voice-kr Yuna --voice-en Susan -rate 155
" >&2; exit 1
}

while [ $# -gt 0 ]; do
        case "$1" in
                --name) NAME="$2"; shift;;
                --voice-kr) VOICEkr="$2"; shift;;
                --voice-en) VOICEen="$2"; shift;;
                --rate) RATE="$2"; shift;;
                --) shift; break;; -*) usage ;; *) break ;;
        esac; shift
done

[ "$NAME" = "" ] && usage

[ "$VOICEkr" = "" ] && VOICEkr=Yuna
[ "$VOICEen" = "" ] && VOICEen=Susan
[ "$VOICE" = "" ] && VOICE=$VOICEkr
[ "$RATE" = "" ] && RATE=155

NAMEPOXIS=$(echo "$NAME" | sed 's/ /\\ /g')

cmd="say -v Yuna -r $RATE -o \"$NAME.aiff\" -f \"$NAME.txt\""
echo $cmd #eval $cmd

MF=$2; N=0; FS="";

cat "${NAME}.txt" | sed \
-e 's/[.][ ]*$/\./g' -e $'s/[.][ ]*/\.\\\n/g' -e $'s/[?][ ]*/\?\\\n/g' \
-e 's/–/-/g' \
> ${NAME}_tts.txt

while read line; do 
        L=$(echo "${line}" | sed -e 's/^[[:space:]]*//')

	han=$(echo $L | grep -E '[ㄱ-힣]')
	[ ! -z "$han" ] && VOICE=$VOICEkr || VOICE=$VOICEen

	if [ ! -z "$L" ]; then
	echo $L
	N=$((N+1))
        SN=$( printf "%05d" $N )
	cmd="say -v $VOICE -r $RATE -o \"$NAME-$SN.aiff\"  \"$L\""
	echo $cmd; eval $cmd;
	fi
done < "${NAME}_tts.txt"

#sleep 10

N=0; FS=""; MF="$NAME-all.aiff"; MAP=""; GAP=""

NAMEPOXIS=$(echo "$NAME" | sed 's/ /\\ /g')
cmd="find -f ${NAMEPOXIS}*.aiff"
AIFFS=$(eval $cmd)
echo $AIFFS

IFS='
'
set -f
for f in $AIFFS; do 
	#echo $f
	FS="$FS -i \"$f\" "
	MAP="${MAP}[${N}][g${N}]"
	N=$((N+1)) 
done

NG=$(($N*2))
G=$(($N-1))

for (( i=0; i<$N; i++ )); do
        GAP="${GAP}[$N]atrim=duration=1[g${i}];"
done


cmd="ffmpeg -y \
$FS \
-f lavfi -i anullsrc \
-filter_complex '${GAP}${MAP}concat=n=$NG:v=0:a=1[a]' -map '[a]' \
\"$MF\""
echo $cmd
eval $cmd

cmd="lame  \"${NAME}-all.aiff\" \"${NAME}-all.mp3\""
echo $cmd; eval $cmd