import pygame import subprocess import threading import time import tkinter as tk from tkinter import ttk 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.current_position = 0 # 初始化 pygame pygame.init() self.screen = pygame.display.set_mode((640, 480)) pygame.display.set_caption("视频播放器") # 创建 Tkinter 控件 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 播放视频并提取帧 ffmpeg_cmd = [ "ffmpeg", "-i", self.video_path, "-f", "image2pipe", "-pix_fmt", "rgb24", "-vcodec", "rawvideo", "-"] # 创建 subprocess 来执行 ffmpeg 命令 process = subprocess.Popen(ffmpeg_cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE) frame_count = 0 while self.is_running: if self.is_paused: time.sleep(0.1) continue # 从 ffmpeg 提取一帧 raw_frame = process.stdout.read(640 * 480 * 3) # 读取一帧(RGB24) if len(raw_frame) < 640 * 480 * 3: break # 如果读取的帧不完整,则停止 frame = pygame.image.fromstring(raw_frame, (640, 480), 'RGB') self.screen.blit(frame, (0, 0)) pygame.display.flip() # 更新屏幕 frame_count += 1 self.update_progress(frame_count) time.sleep(0.04) # 控制帧率 (假设视频的帧率是 25 FPS) process.wait() # 等待视频播放结束 self.restart_video() # 播放结束后自动重播 def update_progress(self, frame_count): """更新进度条""" video_duration = 120 # 手动设置视频时长,或者通过其他方式获取时长 progress_percentage = (frame_count / (25 * 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()