Reputation: 51
i wanna embed a vlc instance in a tkinter frame. already there is a similar code here that show a terminal in a tkinter window but i wanna show a vlc instead. here is my vlc code:
import vlc
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('http://192.168.1.3:8080')
Media.add_option('network-caching=0')
player.set_media(Media)
player.play()
and here is simple tkinter code
try:
# Python2
from Tkinter import *
except ImportError:
# Python3
from tkinter import *
root = Tk()
root.geometry("380x480")
root.resizable(width=False, height=False)
frame1 = LabelFrame(root, width=459, height=300, bd=5)
frame1.grid(row=1, column=0, padx=10)
i want to show that vlc stream in this tkinter frame
Upvotes: 5
Views: 11924
Reputation: 168
I had the same problem and solved it by setting the window I want the video to be played in with set_xwindow:
player.set_xwindow(display.winfo_id())
Another answer here uses set_hwnd which did not work for me (played only audio, while giving the same error of "no frame" etc.). When I exchanged it with set_xwindow it started to work and displayed the video as well as played the sound.
For vlc I ended up with:
Instance = vlc.Instance()
player = Instance.media_player_new()
Media = Instance.media_new('example.mp4')
player.set_xwindow(display.winfo_id())
player.set_media(Media)
player.play()
And for tkinter:
root = tk.Tk()
frame = tk.Frame(root, width=700, height=600)
frame.pack()
display = tk.Frame(frame, bd=5)
display.place(relwidth=1, relheight=1)
root.mainloop()
Upvotes: 2
Reputation: 689
With python-vlc, and vlc player installed (I had to intall the 32 bits version)
self.Instance = vlc.Instance()
self.player = self.Instance.media_player_new()
self.player.set_hwnd(self.label.winfo_id())#tkinter label or frame
media = self.Instance.media_new(f)
self.player.set_media(media)
self.player.play()
sleep(1.5)
duration = self.player.get_length() / 1000
sleep(duration-1.5)
self.player.stop()
Upvotes: 1