是否可以在Python中根据时间控制OMXplayer?

时间:2019-06-09 21:58:02

标签: python raspberry-pi3 dbus omxplayer

我正在尝试使用Python脚本在视频播放期间控制OMXplayer。我是刚接触Python的Dbus的新手,希望只是缺少一些简单的东西。

最终,我想使用使用GPIO引脚的运动传感器来执行此操作,但是首先,我只是试图获得对播放器的控制。我需要做的是在特定时间过去后循环播放视频的一部分,然后在段落中断时跳到“第二部分”。我正在使用密钥条目进行测试。

我的问题是,除非输入键中断或任何其他信号中断,否则代码中的'if'语句不会循环。任何键都会导致中断,但是我希望在不输入任何输入的情况下触发循环中特定于时间的“ if”语句。我希望我的意思在下面的代码中清晰可见,但如果没有,请提出任何问题,我将很乐意尝试澄清。

我正在使用Will Price的OMXplayer包装器:https://github.com/willprice/python-omxplayer-wrapper/

我的硬件是Raspberry Pi 3B +

我查看了类似的问题,包括以下问题,但未找到答案: How to open and close omxplayer (Python/Raspberry Pi) while playing video? https://www.raspberrypi.org/forums/viewtopic.php?p=533160

我已经清除了代码中不必要的部分,所以这是一个基本的功能版本。

from omxplayer.player import OMXPlayer #runs from the popcornmix omxplayer wrapper at https://github.com/popcornmix/omxplayerhttps://github.com/popcornmix/omxplayer and https://python-omxplayer-wrapper.readthedocs.io/en/latest/)
from pathlib import Path
import time
import RPi.GPIO as GPIO #for taking signal from GPIO
import subprocess
import logging
logging.basicConfig(level=logging.INFO)

VIDEO_PATH = Path("VIDEO.m4v")
player_log = logging.getLogger("Player 1")

player = OMXPlayer(VIDEO_PATH, dbus_name='org.mpris.MediaPlayer2.omxplayer1')
player.playEvent += lambda _: player_log.info("Play")
player.pauseEvent += lambda _: player_log.info("Pause")
player.stopEvent += lambda _: player_log.info("Stop")

positionEvent = 12
flyin = 3
flyaway = 15

'''
THE ERROR OCCURS IN THE BELOW FUNCTION!!!
the following function does not run each time
the 'while' loop below is playing, but does run
when there is a key pressed as an 'input'

I want to add a chck so that each time a certain
amount of time passes, the video jumps back
'''
def checktime():
    currtime = player.position()
    print("current time is " + str(currtime))

    if(currtime > 3):
        print("currtime is greater than 3")


try:

    player.play()

    print ("  Ready")

    while True:

        '''
        Below is the call for the function 'checktime'
        This runs once when the video starts, and only
        runs again when a key is entered.
        I want this to run on a continuous loop
        and call each time the video loops
        '''
        checktime()

        key = input()

        if key == 'd': #works perfectly
            currtime = player.position()
            player.seek(flyin-currtime)
            print("player position is" + str(player.position()))

        if key == 'a': #works perfectly
            currtime = player.position()
            player.seek(flyaway-currtime)

        if key == 's': #works perfectly
            player.play()

        '''
        in addition to the key inputs above, entering any key
        and pressing return will run the checktime()
        function. I understand that this is receiving an
        input, therefore 'waking' the program up,
        but I don't understand why the loop is not working
        continuously
        '''


        # Wait for 10 milliseconds
        time.sleep(.01)


except KeyboardInterrupt:
  print ("  Quit")
  # Reset GPIO settings
  GPIO.cleanup()


使用“ s”,“ a”和“ d”键作为输入来控制视频,并且效果很好。但是,每次while循环结束时,“ checktime()”函数不会调用。我的猜测是,这是因为一旦播放了视频,它就不会在Python程序中寻找任何东西,直到接收到另一个输入为止。

我不确定我是否正确使用了DBus。理想情况下,我想使用omxplayer-wrapper的seek_absolute函数,但我一直无法理解。自发布更早的问题(请参见OMXplayer-wrapper and Python - jump to a specific point in video)以来,我一直在为这个项目做事,并且得到了更好的理解,但是我在这里有些困惑。

如果有人可以提供帮助,我将不胜感激!

0 个答案:

没有答案