7

I have a python script that allows me to interact with my Rapsberry Pi from my phone using a simple web server (Flask).

In this script, I can call omxplayer to play a media file, I do this via a command like this:

Popen(['omxplayer '+filePath], shell=True, stdin=None, stdout=None, stderr=None, close_fds=True) 

This works fine but then I would like to be able to interact with this process by sending key commands. When omxplayer is running, you can press space to play/pause, arrows to skip forward/back, etc. I would like to send these keyboard inputs programatically from my python script instead of requiring a person to manually press them on the keyboard.

How can I send a key command to this process from Python after opening it with Python?

Also, how can I detect from Python if there is another instance of omxplayer running that is not being currently run from this Python script? For example, if omxplayer was called and is running from someone on an ssh connection, how can I detect this inside the python script and kill this process before calling my own omxplayer process?

3
  • Take a look at psutils. Commented Dec 24, 2013 at 14:05
  • So you want to detect and kill running processes that are being used by the user? That seems impolite. Commented Dec 24, 2013 at 14:08
  • psutils could be good for killing the process but the main thing I want to do is send keyboard input to the process opened by the script. I know it is impolite to kill something someone else is doing but its for a Rapsberry Pi and only 1 instance of this program can run at a time without crashing everything so its necessary. Commented Dec 24, 2013 at 14:17

1 Answer 1

7

It sounds like omxplayer is reading from stdin - you can probably write to that process' stdin with your commands.

from subprocess import Popen, PIPE p = Popen(['omxplayer', filePath], stdin=PIPE, stdout=PIPE, stderr=PIPE, close_fds=True) p.stdin.write(' ') # sends a space to the running process p.stdin.flush() # if the above isn't enough, try adding a flush 

Edit: To send arrow keys, try Ctrl+v and then in a terminal to see what key code is being sent - it will be ^[[D, or esc[D. You can enter this with stream.write("\x1b[D") (or "^[[D", where the ^[ is one character produced by Ctrl+v esc and waiting half a second).

Sign up to request clarification or add additional context in comments.

5 Comments

This works just fine, thank you. Do you happen to know though how to send the arrow keys to stdin.write()? Like, left, up, down, right arrow key press.
@shiznatix added how to write arrow keys to answer.
excellent, thank you! One last question, is there a way to find if there is already a omxplayer process is running (that part solved already) and if so, send the keyboard commands to that process (even if the python process did not spawn the original omxplayer process)?
Not nearly as easily - you'll need to start the omxplayer process in such a way that it listens to something on stdin that you can send commands to.
@Thomas, how does flush help in sending commands to process right away

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.