Multiple instances of omxplayer as background processes

I originally bought myself a raspberry pi for all sorts of automation needs, and as a possible means to replace the arduinos that I have running.

My first project was to get XBMC running and make use of its player to playback 720p rtsp streams from my IP cameras. This worked well, but was excessive and only allowed the playback of a single stream at a time. I was in need of a solution that allowed me to present multiple streams simultaneously and making use of as GPU acceleration as much as possible.

Finally, I reverted back to running the pure Rasbian OS for the Raspberry Pi and additionally installing the latest OMXPlayer builds. These are available from here.

Initially, this worked very well as I was able to get the OMXPlayer to present the live stream over HDMI with relative ease. It was also possible to scale and present this video in a specific area on screen.

omxplayer.bin --win x1 y1 x2 y2 rtsp://ip_address/live

However, OMXPlayer only allows a single instance be run from a session as it requires a interactive console to be alive. This was solved easily by making use of the screen function. The example below illustrates how to create a 2×2 matrix (four rtsp streams) for display on a 1920×1080 capable display. My raspberry pi averages around 15% CPU utilisation in this configuration.

screen -dmS camera1 sh -c 'omxplayer --win "0 0 960 540" rtsp://ip_address/live; exec bash'
screen -dmS camera2 sh -c 'omxplayer --win "960 0 1920 540" rtsp://ip_address/live; exec bash'
screen -dmS camera3 sh -c 'omxplayer --win "0 540 960 1080" rtsp://ip_address/live; exec bash'
screen -dmS camera4 sh -c 'omxplayer --win "960 540 1920 1080" rtsp://ip_address/live; exec bash'

From a bash scripting perspective, it is then easy to control, create and kill.

screen -X -S camera3 kill

An alternative approach would be

nohup omxplayer --win "0 0 960 540" rtsp://ip_address/live &
...

and to stop

killall omxplayer.bin

In a future post, I will explore cycling of feeds, dynamic allocation for any size grid, rendering of text and capturing motion.

16 thoughts on “Multiple instances of omxplayer as background processes

  1. Hi Johan,

    Thanks for this! Its awesome and it works.

    I am trying to get the visual effect of one video ending and the next one playing seamlessly, without any black screen between the one ending and the next starting. Do you have any suggestions on how I could achieve this?
    I have looked all over the net without any luck.

  2. thanks for the hint to use “screen” i played before with “nohup” but this caused that the xserver crashed sometimes on my raspberry pi. with “screen” it is stable.

  3. Very interresting post, but it doesn’t work on my pi… I tryed both methods (nohup and screen) with video : the first video run correctly at the good position, but even if you launch another video, nothing happend and the first video stay on screen… Moreover, it seems like when you launch another instance of nohup or screen while the first is running, it stopped instantly (no trace with ps or if you kill the first one, the 2nd didn’t launch)… Have you got some idea ?
    Regards,

    Romain

  4. What does the “sh” and “-c” bits do here? (I understood the -c flag is used to set a config file – or does the following text act as that?)

  5. Hy Mr,,, i want question for you..
    i hope can help you,,,
    How to set up synchronize Raspberry Pi with IP Camera,,, ?
    thank you

  6. I really appreciate your work here. Any possibility of hearing more about the “In a future post, I will explore cycling of feeds, dynamic allocation for any size grid, rendering of text and capturing motion.”? I am involved in a project that this exploration could aid immensely.

    Thanks again!

  7. Hi.
    I tried did all from your describe, but doesn’t work for me well.
    On Raspiban OS (2015-05-05) running omxplayer (omxplayer_0.3.6~git20150505~b1ad23e_armhf.deb) only in two instances in one time and last two instances don’t running. Command “top” show all four process but raspberry show only two rtsp videos. If I change order of start commands (switch last two lines with first two lines) with same result – only first two commands show video.
    What I doing bad?

  8. Thanks, really helped me out!

    Worked perfectly to stream Unifi-video camera’s to my TV using OSMC on a RaspberryPi 2 (after installing screen:
    sudo apt-get install screen
    ). Even has sound (ideal for babycam usage). Anyone else trying a similar setup via OSMC: you have to first kill kodi with
    sudo systemctl stop mediacenter
    before running the screen commands. When you’re done, you can kill the cams (see commands in the guide) and restart kodi with:
    sudo systemctl start mediacenter

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s