0

Just starting out in the wonderful world of RPi maker projects, scored a Model 2b v.1.1, installed the latest Raspbian and now ready for my first project :-)

What I want to build is a video player for an event which sends out GPIO triggers at specific time codes of the video (only 1 video is being used at this moment), i.e. at 00:32:12 in the video, trigger GPIO x which in turn switches off a light.

Hardware-wise, I think I'm all set, I have:

  • A working Pi 2b 1.1
  • a breadboard
  • a double relay module
  • wires etc.

Now all(?) I need is a way to sync the video time code to trigger the right GPIO. I've read a bit about omxplayer-wrapper and I'm aware of OSC, but perhaps my goal can be achieved much simpler since I don't need any external syncing.

Edit - So apparently, python-omxplayer-wrapper is the way to go, thanks to @jsotola for pointing me in the right direction. Actually, just launching the player and a GPIO script simultaneously with the right amount of sleep time works on a basic level. But since this approach provides no 'real' syncing, as soon as any delay is introduced for whatever reason, the timing goes bad. Hence the python-omxplayer-wrapper route.

After a lot of tinkering, I've got python-omxplayer-wrapper installed and working. I also got the GPIO part working (will provide more detail on both when I've got everything working), so all that's left is combining the two to trigger the GPIO's a the right time code.

That's where I got stranded in the provided example code:

from omxplayer import OMXPlayer
from time import sleep
from pathlib import Path

#/* Setup the player as shown in omxplayer-wrapper examples : */
path = Path('Videos/myvideo.m4v')
player = OMXPlayer(path, args=['--no-osd', '--blank'])
player.pause()
sleep(5)
player.play()

#/* Make a query to position() inside infinite loop : */
while (1):
    position = player.position() * 1000
#    /* Event timecodes values are stored in "events" */
    for event in events.values():
        if position - 20 <= event['tc'] and position + 20 >= event['tc']:
#            /* Put your code here */
            print("This is where the first GPIO should be triggered")
player.quit()

When I run this, the video plays fine, but then I get this message in the terminal:

Traceback (most recent call last):
  File "./bm_test.py", line 21, in <module>
    for event in events.values():
NameError: name 'events' is not defined

Unfortunately, I don't have enough reputation yet to respond to the author of the script (source: Simon's answer at https://stackoverflow.com/questions/45532783/fire-events-at-specific-timestamps-during-video-playback/), but apparently I need to do something with events. Now I'm not sure if this requires extra packages, such as https://pypi.org/project/Events/ and perhaps https://pypi.org/project/timecode/ , or it's just a matter of adding some extra code.

Being a complete beginner with Python, I need some help here :-)

4
  • If you have the same program in control of the video and the gpio, it can be as simple as timing things in parallel. start video + wait 0:32:12 (maybe a bit more) then issue gpio command via wiringpi. If you need something a bit more integrated, you can go as far as tweaking and compiling vlc yourself. – Abel Oct 11 '20 at 15:07
  • does this answer your question? ...stackoverflow.com/questions/45532783/… – jsotola Oct 11 '20 at 20:05
  • @Abel, that's what I've done now, but as soon as any circumstances change, the timing goes bad. But it was a good idea for a first attempt, so thanks :-) – iGadget Oct 14 '20 at 19:25
  • @jstola, yes that seems to be what I want. I've got python-omxplayer-wrapper installed and working after a lot of tinkering, but I can't get the 'events' part working yet. Will try to get into contact with the author of your linked post. Thanks! – iGadget Oct 14 '20 at 19:28
0

Disclaimer: I'm just a beginner in Python, but the following works for me:

from omxplayer import OMXPlayer
from time import sleep
from pathlib import Path

# Setup the player as shown in omxplayer-wrapper examples :
path = Path('./recordingsign.mp4')

# the on and off times in seconds
on_target = 5
off_target = 10

player = OMXPlayer(path, args=['--no-osd', '--blank'])
player.pause()
sleep(5)
player.play()

# Make a query to position() inside infinite loop 
while (1):
    try:
        position = int(player.position())
    except:
        # You will get an OMXPlayerDeadError eventually
        break
    if position >= on_target and position <= off_target:
        # Put your code here */
        print("This is where the first GPIO should be triggered")
    # Check position again in one second. 
    # sleep works, but you might want to find an alternative
    sleep(1)

player.quit()

I'm not sure if the person in the first link above ever successfully ran their code in Python; I think /* is used in the C programming language, not Python.

They did, however, helpfully provide the link the the omxplayer-wrapper's git page. If you go there and open the player.py file, you can see the various functions the library provides. Be careful not to use any functions prefixed with _ , as they are private and are only to be used within player.py.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.