Reputation: 29342
I'm trying to use VLC's python bindings to create my own little video player. The demo implementation is quite simple and nice, but it requires all the keyboard commands to be typed into the console from which the script was run.
Is there any way I can handle keyboard input also when the video player itself has focus? Specifically, I care about controlling the video while in fullscreen mode.
Perhaps there's a way to keep the keyboard focus in the console (or maybe another window) while showing the video?
I'm using Windows XP, if that has any relevance.
Upvotes: 2
Views: 1981
Reputation: 3467
I don't know what exactly you mean by "Python bindings" and if that is necessary in order to have keyboard control during VLC playing. You can have full and simple control, as follows:
import vlc
from time import sleep
import msvcrt as kb
player = vlc.MediaPlayer(media_file)
player.play()
while not player.is_playing():
sleep(0.1)
while player.is_playing():
if kb.kbhit():
k = ord(kb.getch())
if k in [27,83,113]: player.stop(); exit() # Exit with [Esc]/[Q]
# Etc. You can include pause, jumping to a position, back/forward, etc.
sleep(0.1)
Upvotes: 1
Reputation: 224
The easiest solution I think is to make the tkinter window fullscreen rather then making the vlc instance fullscreen. So you embed it, and then when you want fullscreen, just removing everything else from the window, set the window to full screen, and set the frame with the video to match the size of the root window.
Here's the code, with some comments in between to explain it:
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()
Upvotes: 1
Reputation: 66881
Ok here is their official answer:
http://forum.videolan.org/viewtopic.php?f=32&t=82807
Upvotes: 1
Reputation: 66881
looks like there's no native way. you could fake it by adding "global" key bindings or by capturing events like "MediaPlayerForward" and just remember "oh that means they probably hit the space bar" (or what not) and respond accordingly. GL! -r
Upvotes: 1
Reputation: 830
The best way to control VLC from Python is to talk via the web interface. I tried to get the VLC Python bindings to work and it was more trouble than it's worth, especially for cross-platform stuff. Just use wireshark or something similar to see what the web interface commands look like(they're very simple). I'm using twisted to do the HTTP GETs but you could use the built-in urllib2.
Upvotes: 2