bash - 通过云 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 中运行。
解决方案
我找到了一种使用 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 中测试。
推荐阅读
- python - 为什么我们将串行对象传递给函数?
- javascript - 如何在 Yarn PnP 模式下使用 Rollup+Babel 转译依赖的 js 文件?
- docker - 使用 docker compose 将 API 连接到数据库
- java - Java 线程调度和对象锁定。线程优先级?
- android - Android 11 中的位置变化将如何影响后台请求?
- java - 多线程中的休眠会话
- python - 当我们更改数组值时,它如何影响 json 文件的结果
- salesforce - Salesforce Connected App 忽略 http ACS 值并改为重定向到 https
- javascript - 从角度的服务人员调用函数?
- xamarin.forms - 来自 appcenter 的 android 11 设备上的应用未安装错误