Python Subprocess stdout PIPE for mplayer processes

1k views Asked by At

I have written the following code as a frontend for mplayer using tkinter. However, I'm stuck trying to use commands like 'get_time_pos' from mplayer because the answer is very sporadically written into the PIPE. By this I mean that sometimes, it doesn't write it at all and isn't read until the program is closed and sometimes it does. But every time, the video gets paused even though the toggle for pause is set to play (since it was already paused) due to passing in the other command (get time pos).

I'm at a loss, honestly and don't know how to fix this or even how to adequately describe the issue...

FOLLOWUP: Im modifying the post by posting only the relevant code parts

from Tkinter import *
from ttk import Frame
from tkFileDialog import askopenfilename, askopenfile
from tkMessageBox import showerror, showinfo
from subprocess import *
from threading import Thread
from Queue import Queue, Empty, LifoQueue
import os, sys
import socket
import select

def command_player(self, comd):
    if self.mplayer_isrunning():
        try:
            self.player_process.stdin.write("%s\r\n"%comd)
            self.player_process.stdin.flush()
        except :
            showerror("Error","Error passing command to mplayer\n%s"%sys.exc_info()[1])

self.player_process = Popen(["mplayer","-fs","-slave","-quiet","-wid",str(winid),self.filenm],stdin=PIPE, stdout=PIPE)

#TRYING TO GET VIDEO POSITION IN TERMS OF % AND THEN PAUSE
self.command_player("get_percent_pos")
output = self.player_process.stdout.readline()
self.command_player("pause")

The above does read the percent position but then it always keeps the video paused no matter what. If I keep only the pause command, the pause works as intended (it toggles b/w play and pause)

Also, the select() portion was me trying out a different way but it doesn't work on windows since it's not a socket. Any more help fixing the subprocess input/output issue will be appreciated.

1

There are 1 answers

1
Joachim Wagner On

Not sure but I think the problem is that a pipe is buffered. I see you are trying to address this issue on the stdin side with sending line breaks and calling flush(). Maybe this isn't enough. Try sending 8 KB of line breaks.

For readline(), you need an end-of-line character ('\n') but mplayer normally writes its progress information on the same line (with '\r').

It must be possible to get in- and output of a subprocess directly (without buffers) as terminal applications can print output immediately. It may involve attaching your processes to a pseudo-terminal device. A starting point may be How to use pseudo-terminals in Linux with C?