import tkinter as tk from tkinter import ttk import vlc import os class VideoPlayer: def __init__(self, root): self.root = root self.player = vlc.Instance() self.is_playing = False self.is_paused = False self.listPlayer = None self.create_ui() def create_ui(self): # Canvas to display video self.canvas = tk.Canvas(self.root, width=640, height=360) self.canvas.pack() # Create play, pause, and stop buttons self.play_button = tk.Button(self.root, text="Play", command=self.toggle_play_pause) self.play_button.pack(side=tk.LEFT) self.stop_button = tk.Button(self.root, text="Stop", command=self.stop) self.stop_button.pack(side=tk.LEFT) # Progress bar for video self.progress_bar = ttk.Scale(self.root, from_=0, to=100, orient="horizontal", length=500, command=self.on_progress_drag) self.progress_bar.pack() # Start a thread to update progress bar self.update_progress_task() def add_video(self, video_path): # Create media list and add video self.mediaList = self.player.media_list_new() self.media = self.player.media_new(video_path) self.mediaList.add_media(self.media) # Create MediaListPlayer and associate it with the media list self.listPlayer = self.player.media_list_player_new() self.listPlayer.set_media_list(self.mediaList) self.listPlayer.set_playback_mode(vlc.PlaybackMode(1)) def play(self): if self.listPlayer: self.listPlayer.play() self.is_playing = True self.is_paused = False self.play_button.config(text="Pause") def stop(self): if self.listPlayer: self.listPlayer.stop() self.is_playing = False self.is_paused = False self.play_button.config(text="Play") def toggle_play_pause(self): if self.is_playing: self.listPlayer.pause() self.is_playing = False self.is_paused = True self.play_button.config(text="Resume") elif self.is_paused: self.listPlayer.play() self.is_playing = True self.is_paused = False self.play_button.config(text="Pause") else: self.play() def update_progress_task(self): # Continuously update the progress bar if self.listPlayer: current_time = self.listPlayer.get_media_player().get_time() # Get current playback time duration = self.listPlayer.get_media_player().get_length() # Get video duration if duration > 0: progress = (current_time / duration) * 100 # Calculate progress percentage self.progress_bar.set(progress) # Call the function every 100 ms self.root.after(100, self.update_progress_task) def on_progress_drag(self, value): # Seek the video to the dragged position if self.listPlayer: duration = self.listPlayer.get_media_player().get_length() new_time = (float(value) / 100) * duration self.listPlayer.get_media_player().set_time(int(new_time)) def main(): root = tk.Tk() root.title("VLC Video Player") video_player = VideoPlayer(root) video_path = "E:/抖音-AI风景/data/关注/1515593476095344/视频/7375164244729302272.mp4" # 替换为实际视频文件路径 video_player.add_video(video_path) # Replace with the path to your video # Start the Tkinter main loop root.mainloop() if __name__ == "__main__": main()