I have been looking for a good way to embed vlc in a python tkinter frame.
I found a script on stackexchange that does the basic work, but - to embed the vlc in a frame - the script relies upon an oop technique I have asked about before but never been able to figure out.
In its original state the script didnt have any controls. It just played the video according to the default settings of the video. Consequently, the videos dimensions can open up huge, out of time, and one is unable to stop or pause playback, and even closing a video out is pain in the butt.
Anyway, I advanced the script by writing in some dimensions to play at a reasonable size, and I added in some tkinter buttons and defs where I want it to pause, resume, stop, etc... And I researched the code enough to find the correct bindings... I just dont get how to actually set the bindings and get the buttons commands to call them...
The script is below. The buttons are supposed to show on the bottom but rest a bit high at the top; and the root.destroy is problematic because it only closes tkinter, not the vlc player (so the video will keep playing). The video will also autoplay. But the main problem is getting it to pause and resume. The code is below, and the bindings (which are not in tkinter, but only vlc) are below the code.
The parameter in the Pause / Resume binding is set to 1 to play and 0 to stop, as the original article explains.
Any help much appreciated!
import tkinter as tk
import vlc
class Screen(tk.Frame):
'''
Screen widget: Embedded video player from local or youtube
'''
def __init__(self, parent, *args, **kwargs):
tk.Frame.__init__(self, parent, bg='black')
self.parent = parent
# Creating VLC player
self.instance = vlc.Instance()
self.player = self.instance.media_player_new()
def GetHandle(self):
# Getting frame ID
return self.winfo_id()
def play(self, _source):
# Function to start player from given source
Media = self.instance.media_new(_source)
Media.get_mrl()
self.player.set_media(Media)
self.player.set_hwnd(self.winfo_id())
self.player.play()
########################################################################
def start (): pass
def pause (): pass
def resume (): pass
def stop (): pass
def close (): pass
########################################################################
url = r"C:\Users\whoever\Desktop\whatever.mp4" # changeable
root = tk.Tk()
root.geometry("600x600+100+100")
player = Screen(root)
player.place(x=0, y=0, width=600, height=400)
player.play(url)
framed2 = tk.Frame(root)
k = tk.Button(framed2, text='Play', command=start)
k.grid(row=0,column=1)
l = tk.Button(framed2, text='Pause', command=pause)
l.grid(row=0,column=2)
m = tk.Button(framed2, text='Stop', command=stop)
m.grid(row=0,column=3)
n = tk.Button(framed2, text='Quit', command=root.destroy)
n.grid(row=0,column=5)
framed2.pack(padx=5, pady=4)
root.mainloop()
###############################################################
https://www.geeksforgeeks.org/python-vlc-medialistplayer-pause-resume/?ref=rp
import vlc
import time
media_player = vlc.MediaListPlayer()
player = vlc.Instance()
media_list = player.media_list_new()
media = player.media_new(url)
media_list.add_media(media)
media_player.set_media_list(media_list)
media_player.play()
time.sleep(5)
media_player.set_pause(1) #<---------------------------------- PAUSE / RESUME BINDING
time.sleep(4)