从 Python 程序写入 FIFO

发布于 2024-10-12 17:39:19 字数 690 浏览 5 评论 0原文

我试图从 python 程序控制 mplayer 的音量。 mplayer 程序从 bash 脚本启动:

#!/bin/bash
mkfifo /home/administrator/files/mplayer-control.pipe
/usr/bin/mplayer -slave -input file=/home/administrator/files/mplayer-control.pipe /home/administrator/music/file.mp3

然后我有一个用 Python 编写的 GUI,它应该能够控制正在播放的 mplayer 实例的音量。我尝试过以下方法:

os.system('echo "set_property volume $musicvol" > /home/administrator/files/mplayer-control.pipe')

如果我用数值替换 $musicvol ,则可以,但不幸的是,这没有用。我需要能够传递变量。

我还可以通过从 Python 应用程序调用 bash 脚本来解决这个问题,但我也无法让它工作:

subprocess.call("/home/administrator/files/setvolume.sh", executable="bash", shell=True)

I a trying to control the volume of mplayer from a python program. The mplayer program gets started from a bash script:

#!/bin/bash
mkfifo /home/administrator/files/mplayer-control.pipe
/usr/bin/mplayer -slave -input file=/home/administrator/files/mplayer-control.pipe /home/administrator/music/file.mp3

Then I have a GUI written in Python that is supposed to be able to control the volume of the instance of mplayer that is being played. I have tried the following:

os.system('echo "set_property volume $musicvol" > /home/administrator/files/mplayer-control.pipe')

That works if i substitute $musicvol with the numeric value instead, but that is unfortunately of no use. I need to be able to pass the variable.

I would also be able to solve it by invoking a bash script from the Python application, but I can not get that to work either:

subprocess.call("/home/administrator/files/setvolume.sh", executable="bash", shell=True)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

伴我老 2024-10-19 17:39:19

您不需要调用 os.system 并调用 shell 将该行从 Python 脚本写入 FIFO - 您只需执行以下操作:

new_volume = 50
with open("/home/administrator/files/mplayer-control.pipe","w") as fp:
    fp.write("set_property volume %d\n" % (new_volume,))

我不清楚您期望在您的代码中发生什么不过,原始的 python - 环境中是否设置了musicvol?相反,如果您想要插入到要传递的字符串中的 Python 变量,最简单的方法是使用字符串插值运算符 (%),就像我在上面的示例中所做的那样。

在使用 subprocess.call 的示例中,如果 setvolume.sh,则不需要 executableshell 关键字参数> 是可执行的,并且有一个 #! 行 - 你可以这样做:

subprocess.call("/home/administrator/files/setvolume.sh")

但是,最好像上面那样在 Python 中使用 openwrite , 我认为。

You don't need to call os.system and invoke a shell to write that line to the FIFO from your Python script- you can just do:

new_volume = 50
with open("/home/administrator/files/mplayer-control.pipe","w") as fp:
    fp.write("set_property volume %d\n" % (new_volume,))

It's not clear to me what you expect to happen in your original python, though - is musicvol set in the environment? If instead it's a Python variable that you want to insert into the string that you're passing, the easiest way is to use the string interpolation operator (%) as I've done in the example above.

In your example of using subprocess.call you don't need the executable or shell keyword arguments if setvolume.sh is executable and has a #! line - you could just do:

subprocess.call("/home/administrator/files/setvolume.sh")

However, it's better to just use open and write in Python as above, I think.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文