Writing to FIFO from a Python Program

I am trying to control the volume of mplayer from a python program. The mplayer program starts with 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 needs to control the volume of the mplayer instance that is playing. I tried the following:

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

This works if instead of $ musicvol you need to enter a numerical value instead, but this is unfortunately useless. I need to pass a variable.

I could also solve it by calling a bash script from a Python application, but I cannot get this to work:

subprocess.call("/home/administrator/files/setvolume.sh", executable="bash", shell=True)
+3
source share
1 answer

You do not need to call os.systemand call the shell to write this line to FIFO from your Python script - you can simply do:

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 executable shell, setvolume.sh #! - :

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

open write Python, .

+7

Source: https://habr.com/ru/post/1786362/


All Articles