wangzhibo
2024-12-04 4c5a1f2f09f6906bf26a783d2fa345a5886a2335
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
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()