python - 如何将 python Popen 与 espeak 和 aplay 一起使用

标签 python asynchronous popen espeak

我正在打电话

espeak -ves -s130 'HEY' --stdout | aplay -D 'sysdefault'

通过 subprocess.Popen,用

espeak_process = Popen(["espeak", "-ves -s100 'HEY' --stdout"], stdout=subprocess.PIPE)
aplay_process = Popen(["aplay", "-D 'sysdefault'"], stdin=espeak_process.stdout, stdout=subprocess.PIPE)

但是没用

ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM  'sysdefault'
aplay: main:682: audio open error: No such file or directory

知道如何实现吗? 谢谢

最佳答案

你的例子相当于在 shell 中输入:

$ espeak '-ves -s100 \'HEY\' --stdout'
$ aplay '-D \'sysdefault\''

这显然是错误的。每个列表条目都是传递给可执行文件的一个参数(argv 条目),您这边不需要转义/引用。所以你想使用:

["aplay", "-D", "sysdefault"]
["espeak", "-ves", "-s100", "HEY", "--stdout"],

另见 the documentation (强调我的):

args is required for all calls and should be a string, or a sequence of program arguments. Providing a sequence of arguments is generally preferred, as it allows the module to take care of any required escaping and quoting of arguments (e.g. to permit spaces in file names). If passing a single string, either shell must be True (see below) or else the string must simply name the program to be executed without specifying any arguments.

关于python - 如何将 python Popen 与 espeak 和 aplay 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27912586/

相关文章:

javascript - 单个值的 Observable 与 Promise

asynchronous - 即使范围拥有变量的所有权,引用仍被保留而无法使用

python - 如何将多个进程的输出连接到另一个进程的输入?

Python subprocess.Popen 在 uWSGI 下慢

java - XML自动编辑器(基于XSD方案)

python - Scrapy:动态定义项目

asynchronous - Flutter: future 和共同偏好

python - 使用 subprocess.Popen 时 Scrapy ImportError : No module named project. 设置

python - 在二维空间中可视化加权图(权重作为顶点之间的距离)

Python 使用 kwargs 初始化对象