逐帧播放/停止视频文件,在tkinter中

问题描述 投票:0回答:1

我正在尝试使用python 3在覆盆子pi上的tkinter中启动/停止视频文件。

每当红外传感器处于低电平(断开)时,我需要从头开始视频,并在传感器再次变为高电平时立即停止。理想情况下,视频应位于tkinter画布内,以便我可以同时在屏幕上显示其他元素(例如加载栏)。

我设法让一切都在运行,除了视频,一旦检测到传感器就会运行,但它会冻结所有其他过程(例如加载条),并且当传感器处于高电平时它不会停止。

这里是一个简化(和未经检查)的代码版本,让您了解一般结构(真正的代码更长):

import tkinter as TK
import RPi.GPIO as GPIO
import os

GPIO.setmode(GPIO.BCM)
GPIO.setup(14, GPIO.IN)

class App:
    def __init__(self, root):
        self.root = root
        self.root.config(background = 'black', cursor = 'none')
        self.background = TK.Canvas(root, width = 1024, height = 600, bg = 'black')
        self.background.pack()

        self.ext = 0
        self.trial = 0
        self.infrared()

    def infrared(self):
        if (GPIO.input(14) == False):
            self.makebar()

            if (self.ext == 0):
                self.runvideo()

        else:
            os.system("killall omxplayer.bin")
            self.ext = 0

        self.root.after(16, self.infrared)

    def runvideo(self):
        os.system("omxplayer /home/pi/Desktop/testvideo.m4v")

    def makebar():
        self.stimulus_rect = TK.Canvas(root, width = 1024, height = 50, bg= 'white')
            if self.ext < 1000
                self.ext = self.ext + 10
                self.stimulus_rect.create_rectangle(0, 0, self.ext, 50, fill="red")
                self.stimulus_rect.place(x=0, y=0, anchor="new")
            else:
                self.trial = self.trial + 1
                self.ext = 0

root = TK.Tk()
App(root)
root.mainloop()

从我在网上找到的内容:1)tkinter可能与opencv结合实现这一点,但它看起来不像在树莓派上安装opencv是一个简单的操作; 2)一般来说,涉及“操作系统”的选项似乎必然会在我想要达到的目标中失败。

我找不到干净的方法。我的梦想场景是逐个加载到画布中的视频帧,并以60hz(屏幕频率)加载。然后,我会以完全相同的频率检查传感器,如果传感器没有损坏,则阻止下一帧加载。在伪代码中,这看起来像这样

def infrared(self):
    if (GPIO.input(14) == False):
        self.makebar()

        if (self.ext == 0):
            self.runvideo()

    else:
        self.video.stop
        self.ext = 0
        self.frame = 0

    self.root.after(16, self.infrared)

def runvideo(self):
    self.frame = self.frame + 1
    video.run("testvideo.m4v", self.frame)

关于如何在覆盆子pi的tkinter中实现这一点的任何想法?

谢谢蚂蚁

python video tkinter raspberry-pi3
1个回答
0
投票

经过一周的研究和试验以及错误,这就是我目前如何实现我所需要的(伪代码):

#### PSEUDOCODE ####         
from subprocess import Popen  # this library is used to open the video file


class App:
    def __init__(self, root):
        self.moviestart = False

    self.movieduration = 130000
    self.movie = "/home/pi/Desktop/test.mp4"

    def infrared(self):
        if (GPIO.input(IR) == False):
            if not self.moviestart:
                self.makevideo()  # Call the function to start the video
            self.moviestart = True  # Flag that the video has started
            self.moviestop = False  # Flag that the video is currently playing
            self.root.after(self.videoduration,
                            self.stopvideo)  # manually call the stop video function 
            # to stop the video after 13 seconds (video's length). 
            # I could not find a more elegant way of doing this, unfortunately. 
        else:
            self.clear_screen()

    self.root.after(self.refreshIR, self.infrared)


def makevideo(self):
    # popen will open the movie in a window of the size I specified, 
    # so that other element from tkinter can be placed on top of the movie window
    omxc = Popen(['omxplayer', self.movie, '--win', "0 30 800 450"])


def stopvideo(self):
    self.moviestart = False  # flag that movie has been stopped       
    if (self.moviestop == False):  # check if the movie is currently playing
        try:  # this is a cheap workaround other problems I had, do not try this at home
            os.system('killall omxplayer.bin')  # this literally kills any omxplayer instance 
            # currently open
            self.moviestop = True  # flag that the movie is not playing at the moment 
        except:
            pass

我希望这对任何有类似问题的人都有用。如果我找到更好的解决方案,我会更新答案。现在这个工作已经足够好了。

© www.soinside.com 2019 - 2024. All rights reserved.