我正在尝试使用 Python 脚本在播放视频期间控制 OMXplayer。我是 Python 上的 Dbus 新手,希望只是缺少一些简单的东西。
最终我想通过使用 GPIO 引脚的运动传感器来做到这一点,但首先我只是想控制播放器。我需要做的是在经过一定时间后循环播放一段视频,然后在段落中断时跳转到“第二”部分。我正在使用密钥条目对此进行测试。
我的问题是代码中的“if”语句不会循环,除非输入的键或任何其他信号中断。任何键都会导致中断,但我希望循环中特定时间的“if”语句在不输入任何输入的情况下触发。我希望我在下面的代码中说的很清楚,但如果不是,请提出任何问题,我很乐意尝试澄清。
我正在使用 Will Price 的 OMXplayer 包装器:https ://github.com/willprice/python-omxplayer-wrapper/
我的硬件是 Raspberry Pi 3B+
我看过类似的问题,包括以下问题,但没有找到答案: 如何在播放视频时打开和关闭 omxplayer (Python/Raspberry Pi)? 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”键作为输入来控制视频,效果很好。但是,'checktime()' 函数不会在每次 while 循环时调用。我的猜测是,这是因为一旦视频正在播放,它不会从 Python 程序中寻找任何东西,直到它收到另一个输入。
我不确定我是否正确使用 DBus。理想情况下,我想使用 omxplayer-wrapper 的 seek_absolute 功能,但我一直无法理解这一点。自从发布了一个较早的问题(参见此处: OMXplayer-wrapper 和 Python - 跳转到视频中的特定点)以来,我一直在努力推进这个项目并且得到了更好的理解,但我在这里有点难过。
如果有人可以提供帮助,我将不胜感激任何建议!