首页 > 解决方案 > 通过云 API 从命令行进行文本到语音转换

问题描述

我正在尝试定义一个单行别名,如下所示:

alias speak='curl -G --data-urlencode "text=$(cat /dev/stdin)" "https://speech.botiumbox.com/api/tts/en?&tts=marytts" -H "accept: audio/wav" | mpv -'

这样我就可以像这样使用它

echo Hello World! | speak
speak Hello World!
speak<RET> # continuously do TTS per line until ^D
Hello World!
Another line!
<Ctrl-D>

如果我使用我正在使用的 API

curl -G --data-urlencode "text=Hello World!" "https://speech.botiumbox.com/api/tts/en?&tts=marytts" -H "accept: audio/wav" | mpv -

如上所示,简单地采用标准输入cat /dev/stdin似乎并不能创建交互式 CLI 程序。将此 API 包装到交互式 CLI 程序中的任何想法?理想情况下,要兼容 POSIX,以便它可以bash在 Unixen 的 shell 中运行。

标签: bashcurlcommand-line-interfacestring-interpolation

解决方案


我找到了一种使用 bash 函数而不是别名的解决方法。该函数marySpeak接受一个字符串,将其在线发送到 TTS,然后使用mpv. 一切都被定义为一个oneliner,所以可以放在你的.bashrc

marySpeak() { curl -s -G --data-urlencode "text=$1" "https://speech.botiumbox.com/api/tts/en?&tts=marytts" -H "accept: audio/wav" | mpv - 2>&1 > /dev/null; } 

# then simply use it like this
marySpeak "hello world!"

在 GNU/Linux 和 macOS 中测试。


推荐阅读