How to use python Popen with a espeak and aplay

喜夏-厌秋 提交于 2019-12-22 11:14:31

问题


I'm trying to call

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

through subprocess.Popen, with

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

But it doesn't work

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

Any idea how to implement this? Thx


回答1:


Your example is the equivalent of typing this in the shell:

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

Which is obviously wrong. Each list entry is one argument (argv entry) passed to the executable, no escaping/quoting needed on your side. So you want to use:

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

Also see the documentation (emphasis mine):

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.



来源:https://stackoverflow.com/questions/27912586/how-to-use-python-popen-with-a-espeak-and-aplay

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!