Untitled
unknown
plain_text
a year ago
2.4 kB
5
Indexable
eeimport tkinter as tk
import sys
import vlc
_isMacOS = sys.platform.startswith('darwin')
_isWindows = sys.platform.startswith('win')
_isLinux = sys.platform.startswith('linux')
class VideoPlayer:
def __init__(self, parent, video_path):
self.parent = parent # == root
# Create video frame
self.video_frame = tk.Frame(self.parent)
self.canvas = tk.Canvas(self.video_frame)
self.canvas.pack(fill=tk.BOTH, expand=1)
# VLC player
args = []
if _isLinux:
args.append('--no-xlib')
args.append('--vout=mmal_vout') # Use mmal for Raspberry Pi hardware acceleration
# Create VLC instance
self.Instance = vlc.Instance(args)
# Create Player
self.player = self.Instance.media_player_new()
self.player.video_set_mouse_input(True)
# Set the window id where to render VLC's video output
h = self.canvas.winfo_id()
if _isWindows:
self.player.set_hwnd(h)
elif _isLinux:
self.player.set_xwindow(h)
# Load the media file
self.media = self.Instance.media_new(video_path)
self.player.set_media(self.media)
# Bind to the main window's close event
self.parent.protocol("WM_DELETE_WINDOW", self.on_closing)
# Attach event handler for video end event
self.player.event_manager().event_attach(vlc.EventType.MediaPlayerEndReached, self.on_video_end)
def on_closing(self):
"""Handle the closing of the application."""
self.player.stop() # Stop the player
self.parent.destroy() # Close the tkinter window
def on_video_end(self, event):
"""Callback for when the video playback ends."""
self.player.stop() # Ensure player stops after video ends
def play(self):
"""Start playback of the video."""
self.player.play()
self.player.set_fullscreen(True) # Enable full-screen mode
def startup():
video_file = "Video1.mp4" # Specify the video file to play
movie_player = VideoPlayer(root, video_file)
movie_player.play() # Start playback immediately
if __name__ == '__main__':
root = tk.Tk()
root.attributes('-fullscreen', True) # Set the window to full screen
root.bind('<Escape>', lambda e: root.attributes('-fullscreen', False)) # Bind Escape to exit full screen
root.after(1000, startup)
root.mainloop()
Editor is loading...
Leave a Comment