Skip to main content
 首页 » 编程设计

python之Subprocess.call 和 --stdout

2025年04月02日15qq78292959

subprocess.call(["espeak", "-s 5 -ven", "where are you", "--stdout", 'shell=True', "aplay"]) 

此输出将只是大量特殊字符的输出,而不是来自 espeak 的音频。当我输入这个时:

subprocess.call(["espeak", "-s 5 -ven", "where are you", 'shell=True', "aplay"]) 

然后就可以听到声音了,但是有时会出现说话慢的问题,同时输出如下信息:

ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.front 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.rear 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.center_lfe 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.side 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround40 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround41 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround50 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround51 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.surround71 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.iec958 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.hdmi 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.modem 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline 
ALSA lib pcm.c:2217:(snd_pcm_open_noupdate) Unknown PCM cards.pcm.phoneline 
ALSA lib pcm_dmix.c:957:(snd_pcm_dmix_open) The dmix plugin supports only playback stream 
Cannot connect to server socket err = No such file or directory 
Cannot connect to server request channel 
jack server is not running or cannot be started 

有人可以解释 --stdout 在这里的意思吗?为什么会导致上述错误?

请您参考如下方法:

来自 espeak documentation :

--stdout

Writes the speech output to stdout as it is produced, rather than speaking it. The data starts with a WAV file header which indicates the sample rate and format of the data. The length field is set to zero because the length of the data is unknown when the header is produced.

对于jack server is not running or cannot be started错误检查this link解决方案:

Do you have installed the alsa package (type 'alsa' and the tab key twice, you should see some commands beginning with alsa..)? If it isn't installed, do that with

sudo apt-get install alsa-tools alsa-utils 

无论如何,这个错误不应该阻止 espeak 工作。您可以通过将 stderr 重定向到 /dev/null 来删除它,如下所示:

FNULL = open(os.devnull, 'w') 
retcode = subprocess.call(["espeak", "-s 5", "-ven", "where are you", "aplay"], stdout=FNULL, stderr=subprocess.STDOUT) 

另请注意,您使用 shell=True 作为 espeak 的参数之一,它实际上应该是 call 的参数方法本身。 只需将其删除即可。