Python: Send command to mplayer under slave mode

痴心易碎 提交于 2019-12-02 04:02:15

问题


I'm trying to send command via pipe to mplayer when running it under slave mode like this:

import subprocess, time
# start mplayer
song = 'mysong.mp3'
cmd = ['mplayer', '-slave', '-quiet', song]
p = subprocess.Popen(cmd, stdout=subprocess.PIPE, stdin=subprocess.PIPE)

# send a command every 3 seconds.
# Full command reference here: http://www.mplayerhq.hu/DOCS/tech/slave.txt 
while True:
    print('sleep 3 seconds ...')
    time.sleep(3)
    cmd = 'get_meta_artist'
    print('send command: {}'.format(cmd))
    p.stdin.write(cmd)
    output = p.communicate()[0]
    print(output)

But the output was nothing.

I took the example from this question.

Running the same mplayer command in the terminal works fine. What am I missing here?

UPDATE:

I changed my cmd from "get_meta_artist" to "get_meta_artist\n" so that a line-break is sent to the pipe as well but still I got nothing in the output.

UPDATE2:

I changed the cmd into "\npause\n" and the music was paused. So that means sending command via stdin worked. It means the output string of "\nget_meta_artist\n" command didn't get piped back as expected....


回答1:


You can only use .communicate() once per subprocess. So using it in a while loop doesn't work.

Instead, you should parse the output of p.stdout directly. There seems to be one line per answer if there is an answer.

In order to prevent blocking, you have 3 options:

  1. Work with threads. You have a separate thread who reads from p.stdout and sends its data to the main thread. It blocks if no data is available.

  2. Set p.stdout to non-blocking mode. Essentially, you'll have to do this:

    import fcntl, os
    fcntl.fcntl(p.stdout.fileno(), fcntl.F_SETFL,
        fcntl.fcntl(p.stdout.fileno(), fcntl.F_GETFL) | os.O_NONBLOCK)
    

    If you read then without data being available, you get an exception (IOError: [Errno 11] Resource temporarily unavailable).

  3. Work with select.select(): perform p.stdout.readline() only if select.select([p.stdout], [], [], <timeout>)[0] is a non-empty list. In that case, the given file object is guaranteed to have data available and to not block on reading.

In order to separate "garbage output" from "useful" output, you could do this:

def perform_command(p, cmd, expect):
    import select
    p.stdin.write(cmd + '\n') # there's no need for a \n at the beginning
    while select.select([p.stdout], [], [], 0.05)[0]: # give mplayer time to answer...
        output = p.stdout.readline()
        print("output: {}".format(output.rstrip()))
        split_output = output.split(expect + '=', 1)
        if len(split_output) == 2 and split_output[0] == '': # we have found it
            value = split_output[1]
            return value.rstrip()

and then do

print perform_command(p, 'get_meta_artist', 'ANS_META_ARTIST')
print perform_command(p, 'get_time_pos', 'ANS_TIME_POSITION')



回答2:


I'm now doing it this way and I start to get output:

 while True:
    cmd = '\nget_meta_artist\n'
    p.stdin.write(cmd)
    output = p.stdout.readline()
    print("output: {}".format(output.rstrip()))
    sys.stdout.flush()

Although I still need to figure out a way to bypass the first flush of mplayer's own initialization stdout but I consider my problem solved.

Thanks to glglgl for giving me useful hints.



来源:https://stackoverflow.com/questions/15856922/python-send-command-to-mplayer-under-slave-mode

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