Impresario
Posts: 14
Joined: Sun May 31, 2015 3:50 am

Initiating and controlling omxplayer using a python app

Sat Oct 28, 2017 9:38 pm

I recently assembled a Raspi 3 with the 7" touchscreen in order to play MP4 videos contained on a 128GB USB thumb-drive.

Starting omxplayer from the command line worked great and i was surprised at the quality of the display. To complete my vision, i initiated omxplayer from a python app and that's where I got stuck. There seemed to be no way to end the app, except by remoting into the raspi with SSH from another terminal, identifying omxplayer's process ID and killing it.

Of course I wanted to do more than just kill the app, I also wanted to pause/resume, fast forward and perform all the other functions that are possible through keystrokes in the command line method. I tried various ways of routing keystrokes to my python app, but nothing worked.

I'm neither a linux nor python expert. Can someone point me the right way?

Thank you

gkreidl
Posts: 5653
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Initiating and controlling omxplayer using a python app

Sun Oct 29, 2017 6:22 am

Impresario wrote:
Sat Oct 28, 2017 9:38 pm
I recently assembled a Raspi 3 with the 7" touchscreen in order to play MP4 videos contained on a 128GB USB thumb-drive.

Starting omxplayer from the command line worked great and i was surprised at the quality of the display. To complete my vision, i initiated omxplayer from a python app and that's where I got stuck. There seemed to be no way to end the app, except by remoting into the raspi with SSH from another terminal, identifying omxplayer's process ID and killing it.

Of course I wanted to do more than just kill the app, I also wanted to pause/resume, fast forward and perform all the other functions that are possible through keystrokes in the command line method. I tried various ways of routing keystrokes to my python app, but nothing worked.

I'm neither a linux nor python expert. Can someone point me the right way?

Thank you
Something like this:

Code: Select all

omxprocess = subprocess.Popen(['omxplayer',uri],stdin=subprocess.PIPE)
where "uri" is a file path or URL.
You can send keyboard commands like this:

Code: Select all

omxprocess.stdin.write('q')
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

Impresario
Posts: 14
Joined: Sun May 31, 2015 3:50 am

Re: Initiating and controlling omxplayer using a python app

Sun Oct 29, 2017 10:20 pm

Thank you for your suggestion. Here is my test code:

import subprocess
import time

moviepath = 'path to my MP4 file'
omxprocess = subprocess.Popen(['omxplayer', moviepath], stdin=subprocess.PIPE)

time.sleep(30)
print('Time to quit')
omxprocess.stdin.write('q')

The video starts but never terminates. After 30 secs, the last line throws an error: Typeerror: 'str' does not support the buffer interface

I appreciate your advice!

User avatar
Douglas6
Posts: 4365
Joined: Sat Mar 16, 2013 5:34 am
Location: Chicago, IL

Re: Initiating and controlling omxplayer using a python app

Sun Oct 29, 2017 10:48 pm

There are at least a couple of Python libraries that use D-Bus to control omxplayer. One of them I wrote; see here: https://github.com/Douglas6/omxcontrol

User avatar
OutoftheBOTS
Posts: 618
Joined: Tue Aug 01, 2017 10:06 am

Re: Initiating and controlling omxplayer using a python app

Mon Oct 30, 2017 9:11 am

Is it easier to play the video file from within Python instead of playing it with an external process, this would give you much more control.

I see that pygame has video playing fuctions

gkreidl
Posts: 5653
Joined: Thu Jan 26, 2012 1:07 pm
Location: Germany

Re: Initiating and controlling omxplayer using a python app

Mon Oct 30, 2017 9:14 am

Impresario wrote:
Sun Oct 29, 2017 10:20 pm
Thank you for your suggestion. Here is my test code:

import subprocess
import time

moviepath = 'path to my MP4 file'
omxprocess = subprocess.Popen(['omxplayer', moviepath], stdin=subprocess.PIPE)

time.sleep(30)
print('Time to quit')
omxprocess.stdin.write('q')

The video starts but never terminates. After 30 secs, the last line throws an error: Typeerror: 'str' does not support the buffer interface

I appreciate your advice!
use this:

Code: Select all

omxprocess = subprocess.Popen(['omxplayer',moviepath],stdin=subprocess.PIPE,stdout=None,stderr=None,bufsize=0)
time.sleep(10)
omxprocess.stdin.write(b'q')
Minimal Kiosk Browser (kweb)
Slim, fast webkit browser with support for audio+video+playlists+youtube+pdf+download
Optional fullscreen kiosk mode and command interface for embedded applications
Includes omxplayerGUI, an X front end for omxplayer

Impresario
Posts: 14
Joined: Sun May 31, 2015 3:50 am

Re: Initiating and controlling omxplayer using a python app

Mon Oct 30, 2017 2:14 pm

Thank you all for the help. The modification to my python test code worked great. Sorry I'm not more versed in python, but I'm learning. I'm also going to test the D-Bus approach and report back.

zipzit
Posts: 1
Joined: Wed May 23, 2018 5:41 pm

Re: Initiating and controlling omxplayer using a python app

Wed May 23, 2018 5:58 pm

Impresario and gkreidl:

You guys rock! Many thanks... Here's my code based on your share:

Code: Select all

import subprocess 
import time 

while 1: 
    moviepath = 'http://192.168.1.11/fileserver/downloadVideo'
    omxprocess = subprocess.Popen(['omxplayer', moviepath, '-b'],  stdin=subprocess.PIPE, stdout=None, stderr=None, bufsize=0)
    time.sleep(10)
    omxprocess.stdin.write(b'q')
    time.sleep(5)

In my case, I'm running a node server within my local network, serving up video files via Express and `res.sendFile(...)` I wanted to verify when the server swapped the video, that the Pi received the latest and greatest. Note, the '-b' after moviepath is for full screen, blackout background omxplayer mode...

Again, thanks to you guys for sharing...

Return to “Python”

Who is online

Users browsing this forum: No registered users and 13 guests