wangzhibo
2024-12-09 1d077b3a77bb988af8a4d5146d27aebaa10e86da
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
import subprocess
import tkinter as tk
from tkinter import ttk
import threading
import time
 
class VideoPlayer:
    def __init__(self, root, video_path):
        self.root = root
        self.video_path = video_path
        self.is_paused = False
        self.is_running = True
 
        # 创建界面元素
        self.canvas = tk.Canvas(root, width=640, height=480)
        self.canvas.pack()
 
        self.play_button = tk.Button(root, text="播放/暂停", command=self.toggle_pause)
        self.play_button.pack(side=tk.LEFT)
 
        self.restart_button = tk.Button(root, text="重新播放", command=self.restart_video)
        self.restart_button.pack(side=tk.LEFT)
 
        self.progress = ttk.Scale(root, from_=0, to=100, orient=tk.HORIZONTAL, command=self.update_position)
        self.progress.pack(fill=tk.X, expand=True)
 
        # 启动播放视频线程
        self.video_thread = threading.Thread(target=self.play_video)
        self.video_thread.start()
 
    def toggle_pause(self):
        """切换播放/暂停状态"""
        if self.is_paused:
            self.is_paused = False
        else:
            self.is_paused = True
 
    def restart_video(self):
        """重新开始播放"""
        self.is_paused = False
        self.progress.set(0)
        self.play_video()
 
    def update_position(self, value):
        """更新进度条位置"""
        self.current_position = float(value)
 
    def play_video(self):
        """播放视频并更新进度条"""
        # subprocess.run 会阻塞,启动一个后台进程运行 ffplay
        ffplay_cmd = ["ffplay", "-i", self.video_path, "-v", "quiet", "-x", "640", "-y", "480", "-autoexit"]
        
        # 创建 subprocess 来播放视频
        process = subprocess.Popen(ffplay_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        
        while self.is_running:
            if self.is_paused:
                continue
            # 更新进度条
            self.update_progress(process)
            time.sleep(0.1)  # 控制更新频率
 
        process.wait()  # 等待视频播放结束
        self.restart_video()  # 播放结束后自动重播
 
    def update_progress(self, process):
        """更新进度条"""
        # 获取 ffplay 的输出并更新进度
        stdout, stderr = process.communicate()
        lines = stderr.decode().splitlines()
        for line in lines:
            if "time=" in line:
                # 提取时间信息
                current_time = line.split("time=")[-1].split(" ")[0]
                hours, minutes, seconds = map(float, current_time.split(":"))
                total_seconds = hours * 3600 + minutes * 60 + seconds
                video_duration = 120  # 手动设置视频时长,或者通过其他方式获取时长
                progress_percentage = (total_seconds / video_duration) * 100
                self.progress.set(progress_percentage)
 
    def close(self):
        """关闭视频播放器"""
        self.is_running = False
        self.video_thread.join()
 
# 创建 Tkinter 窗口
root = tk.Tk()
root.title("视频播放器")
 
media_path = "E:/抖音-AI风景/data/关注/1515593476095344/视频/7375164244729302272.mp4"  # 替换为实际视频文件路径
player = VideoPlayer(root, media_path)
 
root.protocol("WM_DELETE_WINDOW", player.close)
root.mainloop()