视频跳帧播放器设计与实现
视频跳帧播放器设计与实现
下面我将设计并实现一个功能完善的视频跳帧播放器,支持设置多个跳帧区间,并提供直观的用户界面。
设计思路
这个播放器需要实现以下核心功能:
- 视频文件加载和基本播放控制
- 设置跳帧区间(起始帧和结束帧)
- 播放过程中自动跳过指定帧区间
- 可视化时间轴和跳帧区间
- 帧数显示和跳转功能
实现方案
import sys
import cv2
import numpy as np
from PyQt5.QtWidgets import *
from PyQt5.QtGui import *
from PyQt5.QtCore import *
from PyQt5.QtMultimedia import QMediaPlayer, QMediaContent
from PyQt5.QtMultimediaWidgets import QVideoWidgetclass VideoFramePlayer(QMainWindow):def __init__(self):super().__init__()self.setWindowTitle("高级视频跳帧播放器")self.setGeometry(100, 100, 1200, 800)# 初始化变量self.video_path = ""self.cap = Noneself.total_frames = 0self.fps = 0self.current_frame = 0self.is_playing = Falseself.jump_intervals = [] # 存储跳帧区间 [(start1, end1), (start2, end2), ...]# 创建UIself.create_ui()# 初始化定时器self.timer = QTimer(self)self.timer.timeout.connect(self.update_frame)# 设置样式self.set_style()def create_ui(self):# 主布局main_widget = QWidget()self.setCentralWidget(main_widget)main_layout = QVBoxLayout()main_widget.setLayout(main_layout)# 视频显示区域self.video_label = QLabel()self.video_label.setAlignment(Qt.AlignCenter)self.video_label.setMinimumSize(800, 450)self.video_label.setStyleSheet("background-color: #222; border: 1px solid #444;")main_layout.addWidget(self.video_label)# 控制区域control_layout = QHBoxLayout()# 播放控制按钮self.play_btn = QPushButton("播放")self.play_btn.clicked.connect(self.toggle_play)self.play_btn.setMinimumSize(80, 30)self.stop_btn = QPushButton("停止")self.stop_btn.clicked.connect(self.stop)self.stop_btn.setMinimumSize(80, 30)self.open_btn = QPushButton("打开视频")self.open_btn.clicked.connect(self.open_video)self.open_btn.setMinimumSize(100, 30)control_layout.addWidget(self.open_btn)control_layout.addWidget(self.play_btn)control_layout.addWidget(self.stop_btn)# 帧信息self.frame_label = QLabel("帧: 0 / 0")control_layout.addWidget(self.frame_label)main_layout.addLayout(control_layout)# 跳帧控制区域jump_frame_layout = QHBoxLayout()self.start_frame_input = QSpinBox()self.start_frame_input.setRange(0, 999999)self.start_frame_input.setValue(0)self.start_frame_input.setPrefix("起始帧: ")self.end_frame_input = QSpinBox()self.end_frame_input.setRange(0, 999999)self.end_frame_input.setValue(100)self.end_frame_input.setPrefix("结束帧: ")self.add_jump_btn = QPushButton("添加跳帧区间")self.add_jump_btn.clicked.connect(self.add_jump_interval)self.clear_jump_btn = QPushButton("清除所有跳帧")self.clear_jump_btn.clicked.connect(self.clear_jump_intervals)jump_frame_layout.addWidget(self.start_frame_input)jump_frame_layout.addWidget(self.end_frame_input