vlc python绑定-如何接收键盘输入?

67up9zun  于 2023-01-29  发布在  Python
关注(0)|答案(4)|浏览(147)

我正在尝试使用VLC的python bindings来创建我自己的小视频播放器,演示实现非常简单和漂亮,但是它需要将所有键盘命令输入到运行脚本的控制台中。
当视频播放器本身有焦点时,有没有什么方法可以处理键盘输入?具体来说,我关心的是在全屏模式下控制视频。
也许有一种方法可以在显示视频的同时保持键盘在控制台(或者另一个窗口)中的焦点?
我用的是Windows XP,如果这有什么关系的话。

lbsnaicq

lbsnaicq1#

从Python控制VLC的最好方法是通过Web界面进行对话。我试着让VLC Python绑定工作,但它比它的价值更麻烦,特别是对于跨平台的东西。只要使用wireshark或类似的东西来看看Web界面命令是什么样子的(它们非常简单)。我使用twisted来做HTTP GET,但你可以使用内置的urllib2。

kx5bkwkv

kx5bkwkv2#

看起来没有原生的方法.你可以通过添加“全局”键绑定或通过捕获事件来伪造它,比如“MediaPlayerForward”,并且记住“哦,这意味着他们可能击中了空格键”(或者什么的)并相应地响应. GL!-r

von4xj4u

von4xj4u4#

我认为最简单的解决方案是使Tkinter窗口全屏,而不是使VLC示例全屏。所以你嵌入它,然后当你想要全屏时,只需从窗口中移除其他所有内容,将窗口设置为全屏,并设置视频的框架以匹配根窗口的大小。
下面是代码,中间有一些注解来解释它:

from tkinter import (
    Tk, Menu, Frame, Label, Button, Scale, Toplevel,
    BOTH, Entry, DISABLED, END, HORIZONTAL, VERTICAL
)
from vlc import Instance

class Test:
    def __init__(self):
        self.Instance = Instance()
        self.player = self.Instance.media_player_new()
        self.full_screen = 0
        self.root = Tk()
        self.defaultbg = self.root.cget('bg') #this line of code is to get the default background
        #colour for the purpose of changing it between black and default
        self.root.geometry("800x800")
        self.frame = Frame(self.root, width=700, height=600)
        self.frame.pack()
        #the above frame is for the purpose of embedding the video in non-fullscreen
        self.root.bind('<space>', self.toggle_pause)
        self.root.bind('<Escape>', self.toggle_FullScreen)
        self.button_frame = Frame(self.root)
        self.button_frame.pack()
        #the above frame is for the purpose of butting multiple widgets into
        #one frame that can be easily hidden and viewed again
        self.button1 = Button(
            self.button_frame, text="OK",
            command=self.play)
        self.button1.pack(side='left')
        #button to play the video
        self.button2 = Button(
            self.button_frame, text="Full Screen",
            command=self.toggle_FullScreen)
        self.button2.pack(side='left')
        #button to toggle fullscreen
        self.display = Frame(self.frame, bd=4)
        self.display.place(relwidth=1, relheight=1)
        #the above display is to adapt the size of the video to the frame
        #so that a bigger frame can hold a bigger video
        self.root.mainloop()

    def play(self):
        Media = self.Instance.media_new('/home/balthazar/test.webm')
        self.player.set_xwindow(self.display.winfo_id())
        #setting the xwindow is for embedding the video
        self.player.set_media(Media)
        self.player.play()

    def toggle_FullScreen(self, event=False):
        if self.full_screen == 0 and event==False:
            self.full_screen = 1
            self.button_frame.pack_forget()
            #pack forget removes the buttons from view
            self.display.config(background="black")
            self.frame.pack_forget()
            self.frame.place(relwidth=1, relheight=1)
            #to make the frame fulscreen it must be unpacked and then placed
            self.root.attributes("-fullscreen", True)
        elif event and self.full_screen==1:
            self.frame.place_forget()
            self.frame.pack()
            self.button_frame.pack()
            self.full_screen = 0
            self.display.config(background=self.defaultbg)
            self.root.attributes("-fullscreen", False)

    def toggle_pause(self, event):
        pause = self.player.is_playing()
        self.player.set_pause(pause)

def main():
    Test()

if __name__ == '__main__':
    main()

相关问题