Page 1 of 1

Sending keys to omxplayer

Posted: Wed Nov 13, 2013 11:16 pm
by icanhazscreenid
I think I need to do something with pipes here but I am not familiar with the process. I'm looking for a way to send signals or key presses to a process (specifically omxplayer) and I can't piece it all together. The "TODO" comments below show what I am trying to accomplish. Video playback works. I get valid PIDs. I just don't know how to control omxplayer once the process has started. Any help? Thanks in advance.

Code: Select all

#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace std;

int main(void){
	int pidChild;
	int pidParent;

	int pid = fork();
	if(pid == 0){
		pidChild = getpid();
		cout << "Child PID: " << pidChild << endl;
		execlp("/usr/bin/omxplayer", "-r ", "/mnt/disk1/Videos/002.mp4", NULL);

		//TODO: Send 'spacebar' to Omxplayer to pause playback
		cout << "Playback loaded" << endl;

		_exit(0);	
	}
	else{
		pidParent = getpid();
		cout << "Parent PID: " << pidParent << endl;
		//wait();
	} 

	//External events occur here, simulate with sleep
	sleep(5);

	//TODO: Send 'spacebar' to Omxplayer to start playback
	cout << "Playback start" << endl;

	//External events occur here, simulate with sleep
	sleep(5);

	//TODO: Send 'q' or kill omxplayer
	cout << "Playback end" << endl;
}

Re: Sending keys to omxplayer

Posted: Mon Nov 18, 2013 6:27 am
by icanhazscreenid
In case anyone ever has the same question, use ptty for this. I found this post amazingly helpful:

http://stackoverflow.com/questions/1168 ... -dup2-in-c

Re: Sending keys to omxplayer

Posted: Mon Nov 18, 2013 8:56 am
by Joe Schmoe
Actually, if the program is strictly tty-based (basically, although there are some caveats here, this means standard-IO based), then the canonical answer (and the one you should follow/use) is the tool called "Expect" (Google it).

However, if the program is X/GUI based, then Expect probably won't work (although note that Expect does have the ability to drive xterm - in a cute way described in the book), in which case you will need to use something called "xdotool" (or something like that - I've never used it - it seems to be a stripped down version of WinBatch for X).

Re: Sending keys to omxplayer

Posted: Mon Nov 18, 2013 9:56 am
by KenT
There is a pure python version of expect called pexpect from Noah. Works OK in my Pi Presents and in my TBOPlayer

Also look for pyomxplayer, there are two versions one from me using pipes (very newbie attempt) the other from Johannes Baiter using pexpect. Both will probably need modification to catch up with the latest omxplayer.