好吧,我的google-fu真的很糟糕,我找不到答案,希望你们能帮助我^_^
好的,所以我认为一个简单的脚本似乎没有正确地与其子进程通信,我正在逐行运行这个脚本。我也在使用 mpg123 播放器,这是一个 Linux 系统(好吧,Raspberry Pi)
from subprocess import Popen, PIPE, STDOUT
p = Popen(["mpg123", "-C", "test.mp3"], stdout=PIPE, stdin=PIPE, stderr=STDOUT)
#wait a few seconds to enter this, "q" without a newline is how the controls for the player work to quit out if it were ran like "mpg123 -C test.mp3" on the command line
p.communicate(input='q')[0]
我可以在上面运行 stdout.read() 就可以了,但是使用 communications 作为输入只会让它挂起,而 p.stdin.write('q') 似乎什么也没做。这是与 python 相关的,尽管我有一种感觉,我也没有在 mpg123 文档中查找正确的位置。请友善,因为我对此非常陌生^_^
检查您的
mpg123
版本可以理解哪些参数。以下内容在我的机器上有效:
#!/usr/bin/env python3
import time
from subprocess import Popen, PIPE, DEVNULL, STDOUT
p = Popen(["mpg123", "-K", "test.mp3"], stdin=PIPE, stdout=DEVNULL, stderr=STDOUT)
# wait a little
time.sleep(3)
# send command "Skip song", wait for the player to exit
p.communicate(b'n')[0]
它开始播放文件,等待约 3 秒,然后退出。
这是一个糟糕的解决方案,但它在紧要关头有效。我使用它作为补丁,因为由于某种原因,我无法让 Python 库在我的 Raspberry Pi 上正常运行。
如果您以远程模式 (
mpg123
) 启动 mpg123 -R
,您可以更轻松地向其发送命令:
proc = sp.Popen(["mpg123", "-R"], stdin=sp.PIPE)
然后,您可以向其
stdin
属性发送命令。
注:
"pause"
,而不是 " "
。在控制台中运行 mpg123 -R
,然后向其发送 help
命令以查看命令列表。来自文档:
-R,--远程
激活通用控制界面。然后 mpg123 将读取并执行来自 stdin 的命令。基本用法是“加载”来播放某些文件和明显的“暂停”、“命令”。 ''jump '' 将跳转/寻找到给定点(MPEG 帧号)。发出“帮助”以获取命令和语法的完整列表。