-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathgoogleTTS.sh
More file actions
executable file
·85 lines (71 loc) · 2 KB
/
googleTTS.sh
File metadata and controls
executable file
·85 lines (71 loc) · 2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#!/bin/bash
# folked from https://gist.github.com/markusfisch/873364#file-say-sh, written by markusfisch
#
#=#=#=
# ```
# Text to speech CLI interface using Google translate_TTS API
# Usage: googleTTS.sh [OPTION] TERM
#
# Set options for language, saving directory:
# -l, Set language (default: $LNG)
# -d, Set saving directory (default: ${HOME}/Downloads/TTS/$LNG)
# -p, Set the output player command (default: $playcmd)
#
# -h, Show this message and quit quietly
# ```
#=#=
# some error occured, exit immediately
set -e
# Google translate_TTS API's url
readonly URL="http://translate.google.com/translate_tts?ie=UTF-8&total=1&idx=0&client=t&tl=${LNG}&q="
# for Japanese
LNG=${LANGUAGE:-ja}
# for English
# LNG=${LNG:-en}
# set the root directory to save file
root_dir="${HOME}/Downloads/TTS"
# set default player
playcmd="mpv --really-quiet"
f="$0"
usage() {
sed -n '/^#=#=#=/,/^#=#=/p' $f | sed -e '1d;$d' | cut -b3- | grep -v "\`\`\`"
exit 0
}
# FUTURE #
#
# Option "-i" import the text and divide by some special character (like "---")
# and extract all sound files to the same directory named like "01_TERM.mp3"
#
while getopts l:d:p:hH OPT
do
case $OPT in
"l" ) LNG="$OPTARG" ;;
"d" ) root_dir="$OPTARG" ;;
"p" ) playcmd="$OPTARG" ;;
"h" ) usage ;;
"H" ) usage_all "$f"; exit 0 ;;
* ) usage ;;
esac
done
shift $((OPTIND-1))
save_dir="${root_dir}/${LNG}"
if [ ! -d "${save_dir}" ]; then
mkdir -p "${save_dir}" || (echo "error: cannot create speech stock \"$save_dir\""; exit 1)
fi
text="`echo $*`"
file="`date +"%Y%m%d%H%M%S"`"
# if [ `expr length "${text}"` -gt 10 ]; then
# file="`echo "$text" | cut -c -10`".mp3
# else
# file=${text}.mp3
# fi
file="${save_dir}/${file}"
TERM="`echo ${text} | nkf -wWMQ | tr = %`"
# TERM="${text}"
# [ -f "${file}" ] ||
echo ${URL}${TERM}
wget -U Mozilla --restrict-file-names=nocontrol -O "${file}" ${URL}${TERM}
# firefox --new-tab "${URL}${TERM}"
[ -n "${playcmd}" ] &&
${playcmd} "${file}" &>/dev/null
exit 0