say 命令可以将文件转换成语音。命令示例 say -o hi.wav --data-format=alaw 北京欢迎您。下面例子是将 text.txt 文件中的文字,逐行转换为语音,保存到 voice 目录中。

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/bash

text=text.txt
voice_folder=voice

[ ! -f $text ] && { echo "file $text does not exist"; exit 1; }

[ ! -d $voice_folder ] && { echo "folder $voice_folder does not exist"; exit 1; }

while read i
do
say -o ${voice_folder}/${i}.wav --data-format=alaw $i
done < $text