mp4 显示 按帧 pyqt

2024-08-21 08:04
文章标签 显示 pyqt mp4

本文主要是介绍mp4 显示 按帧 pyqt,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

目录

pyqt安装:

运行报错 xcb:

xcb解决方法:

 随窗口改变,拖拽打开mp4:

n加载下一个视频,l加载上一个视频:

固定大小,不能随窗口改变:


pyqt安装:

ubuntu直接安装pip install pyqt5,报错:

File "/tmp/tmpqq_xs870", line 126, in prepare_metadata_for_build_wheel hook = backend.prepare_metadata_for_build_wheel AttributeError: module 'sipbuild.api' has no attribute 'prepare_metadata_for_build_wheel'

解决方法:

pip install pyqt5==5.15.2

运行报错 xcb:

qt.qpa.plugin: Could not load the Qt platform plugin "xcb" in "/home/lixiang/.local/lib/python3.8/site-packages/cv2/qt/plugins" even though it was found. This application failed to start because no Qt platform plugin could be initialized. Reinstalling the

xcb解决方法:

【解决】qt.qpa.plugin: Could not load the Qt platform plugin “xcb“-CSDN博客

import os
from PyQt5.QtCore import QLibraryInfo
# from PySide2.QtCore import QLibraryInfo
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(QLibraryInfo.PluginsPath
)

 随窗口改变,拖拽打开mp4:

import sys
import cv2
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt, QTimer
from collections import deque
from pynput import keyboardclass VideoPlayer(QMainWindow):def __init__(self):super().__init__()# 设置窗口self.setWindowTitle('Drag and Drop MP4 Player')self.setGeometry(100, 100, 800, 600)self.setAcceptDrops(True)# 主窗口部件和布局self.widget = QWidget(self)self.layout = QVBoxLayout(self.widget)self.setCentralWidget(self.widget)# 创建QGraphicsView和QGraphicsSceneself.graphics_view = QGraphicsView(self)self.layout.addWidget(self.graphics_view)# 设置 QGraphicsView 的样式表,以去除边框和滚动条self.graphics_view.setStyleSheet("border: none; background: transparent;")self.graphics_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.graphics_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.scene = QGraphicsScene(self)self.graphics_view.setScene(self.scene)self.pixmap_item = QGraphicsPixmapItem()self.scene.addItem(self.pixmap_item)# 创建显示文件路径的QLabelself.file_info_label = QLabel("Drag MP4 file here", self)self.file_info_label.setAlignment(Qt.AlignCenter)self.file_info_label.setStyleSheet("border-top: 1px solid black; border-left: none; border-right: none; border-bottom: none; padding: 10px;")  # 只显示上边框self.layout.addWidget(self.file_info_label)# 视频相关变量self.cap = Noneself.frame_buffer = deque(maxlen=300)self.queue_index = -1self.load_new_frame = Trueself.frame_index = -1# 定时器self.timer = QTimer(self)self.timer.timeout.connect(self.display_frame)# 启动键盘监听listener = keyboard.Listener(on_press=self.on_press)listener.start()# 初始化视频文件(确保文件路径可用)self.file_path = Nonedef on_press(self, key):if key == keyboard.Key.right or key == keyboard.Key.space:if self.queue_index < len(self.frame_buffer) - 1:self.queue_index += 1else:self.load_new_frame = Trueelif key == keyboard.Key.left:if self.queue_index > 0:self.queue_index -= 1self.load_new_frame = Falseelif str(key) == "'q'":self.close()def dragEnterEvent(self, event):if event.mimeData().hasUrls():event.accept()else:event.ignore()def dropEvent(self, event):for url in event.mimeData().urls():file_path = url.toLocalFile()if file_path.lower().endswith(".mp4"):self.file_path = file_pathself.file_info_label.setText(f"Loading: {file_path}")self.load_video(file_path)self.setWindowTitle(file_path)def load_video(self, file_path):if self.cap:self.cap.release()self.cap = cv2.VideoCapture(file_path)self.frame_buffer.clear()self.queue_index = -1self.frame_index = -1self.load_new_frame = True# 开始播放视频self.timer.start(30)def display_frame(self):if self.load_new_frame and self.cap:ret, frame = self.cap.read()if not ret:self.load_new_frame = Falsecv2.putText(self.frame_buffer[self.queue_index], str(self.frame_index) + ":end", (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)else:self.frame_index += 1cv2.putText(frame, str(self.frame_index), (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)self.frame_buffer.append(frame)self.queue_index = len(self.frame_buffer) - 1self.load_new_frame = Falseif len(self.frame_buffer) > 0:imshow = self.frame_buffer[self.queue_index]self.show_frame(imshow)def show_frame(self, frame):rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)h, w, ch = rgb_frame.shapebytes_per_line = ch * wq_img = QImage(rgb_frame.data, w, h, bytes_per_line, QImage.Format_RGB888)pixmap = QPixmap.fromImage(q_img)self.pixmap_item.setPixmap(pixmap)# 调整视图以适应图像self.fit_image_to_view()def fit_image_to_view(self):if self.pixmap_item.pixmap().isNull():returnself.graphics_view.resetTransform()# 调整视图以适应图像self.graphics_view.fitInView(self.pixmap_item, Qt.KeepAspectRatio)def resizeEvent(self, event):super().resizeEvent(event)self.fit_image_to_view()  # 窗口调整大小时调整图片的缩放比例def closeEvent(self, event):if self.cap:self.cap.release()self.timer.stop()event.accept()if __name__ == '__main__':app = QApplication(sys.argv)player = VideoPlayer()player.show()  # 显示窗口,使用默认大小,不最大化sys.exit(app.exec_())

n加载下一个视频,l加载上一个视频:

import glob
import os.path
import sys
import cv2import os
from PyQt5.QtCore import QLibraryInfo
# from PySide2.QtCore import QLibraryInfo
os.environ["QT_QPA_PLATFORM_PLUGIN_PATH"] = QLibraryInfo.location(QLibraryInfo.PluginsPath
)from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QVBoxLayout, QWidget, QGraphicsView, QGraphicsScene, QGraphicsPixmapItem
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt, QTimer
from collections import deque
from pynput import keyboardfrom natsort import natsorted
class VideoPlayer(QMainWindow):def __init__(self):super().__init__()# 设置窗口self.setWindowTitle('Drag and Drop MP4 Player')self.setGeometry(100, 100, 800, 600)self.setAcceptDrops(True)# 主窗口部件和布局self.widget = QWidget(self)self.layout = QVBoxLayout(self.widget)self.setCentralWidget(self.widget)# 创建QGraphicsView和QGraphicsSceneself.graphics_view = QGraphicsView(self)self.layout.addWidget(self.graphics_view)# 设置 QGraphicsView 的样式表,以去除边框和滚动条self.graphics_view.setStyleSheet("border: none; background: transparent;")self.graphics_view.setHorizontalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.graphics_view.setVerticalScrollBarPolicy(Qt.ScrollBarAlwaysOff)self.scene = QGraphicsScene(self)self.graphics_view.setScene(self.scene)self.pixmap_item = QGraphicsPixmapItem()self.scene.addItem(self.pixmap_item)# 创建显示文件路径的QLabelself.file_info_label = QLabel("Drag MP4 file here", self)self.file_info_label.setAlignment(Qt.AlignCenter)self.file_info_label.setStyleSheet("border-top: 1px solid black; border-left: none; border-right: none; border-bottom: none; padding: 10px;")  # 只显示上边框self.layout.addWidget(self.file_info_label)# 视频相关变量self.cap = Noneself.frame_buffer = deque(maxlen=300)self.queue_index = -1self.load_new_frame = Trueself.frame_index = -1# 定时器self.timer = QTimer(self)self.timer.timeout.connect(self.display_frame)# 启动键盘监听listener = keyboard.Listener(on_press=self.on_press)listener.start()self.file_i=0self.file_list=[]# 初始化视频文件(确保文件路径可用)self.file_path = Noneself.timer_start=Falsedef on_press(self, key):if key == keyboard.Key.right or key == keyboard.Key.space:if self.queue_index < len(self.frame_buffer) - 1:self.queue_index += 1else:self.load_new_frame = Trueelif key == keyboard.Key.left:if self.queue_index > 0:self.queue_index -= 1self.load_new_frame = Falseelif str(key) == "'n'":print('-----', str(key))self.file_i += 1if self.file_i < len(self.file_list):self.load_video(self.file_list[self.file_i])elif str(key) == "'l'":print('-----', str(key))self.file_i -= 1if self.file_i>=0:self.load_video(self.file_list[self.file_i])elif str(key) == "'q'":self.close()def dragEnterEvent(self, event):if event.mimeData().hasUrls():event.accept()else:event.ignore()def dropEvent(self, event):for url in event.mimeData().urls():file_path = url.toLocalFile()if file_path.lower().endswith(".mp4"):self.file_path = file_pathself.load_video(file_path)self.file_list=glob.glob(f"{os.path.dirname(file_path)}/*.mp4")self.file_list=natsorted(self.file_list)self.file_i=0def load_video(self, file_path):if self.cap:self.cap.release()self.file_info_label.setText(f"{file_path}")self.cap = cv2.VideoCapture(file_path)self.frame_buffer.clear()self.queue_index = -1self.frame_index = -1self.load_new_frame = Trueif not self.timer_start:self.timer.start(30)self.timer_start=Truedef display_frame(self):if self.load_new_frame and self.cap:ret, frame = self.cap.read()if not ret:self.load_new_frame = False# self.end_count+=1# if self.end_count > 3:#     self.load_video(self.file_path)cv2.putText(self.frame_buffer[self.queue_index], str(self.frame_index) + ":end", (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)else:print('----append------')self.frame_index += 1self.setWindowTitle(f"w:{frame.shape[1]},h:{frame.shape[0]}")cv2.putText(frame, str(self.frame_index), (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)self.frame_buffer.append(frame)self.queue_index = len(self.frame_buffer) - 1self.load_new_frame = Falseif len(self.frame_buffer) > 0:imshow = self.frame_buffer[self.queue_index]self.show_frame(imshow)def show_frame(self, frame):rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)h, w, ch = rgb_frame.shapebytes_per_line = ch * wq_img = QImage(rgb_frame.data, w, h, bytes_per_line, QImage.Format_RGB888)pixmap = QPixmap.fromImage(q_img)self.pixmap_item.setPixmap(pixmap)# 调整视图以适应图像self.fit_image_to_view()def fit_image_to_view(self):if self.pixmap_item.pixmap().isNull():returnself.graphics_view.resetTransform()# 调整视图以适应图像self.graphics_view.fitInView(self.pixmap_item, Qt.KeepAspectRatio)def resizeEvent(self, event):super().resizeEvent(event)self.fit_image_to_view()  # 窗口调整大小时调整图片的缩放比例def closeEvent(self, event):if self.cap:self.cap.release()self.timer.stop()event.accept()if __name__ == '__main__':app = QApplication(sys.argv)player = VideoPlayer()player.show()  # 显示窗口,使用默认大小,不最大化sys.exit(app.exec_())

固定大小,不能随窗口改变:

import sys
import cv2
from PyQt5.QtWidgets import QApplication, QMainWindow, QLabel, QFileDialog, QVBoxLayout, QWidget
from PyQt5.QtGui import QPixmap, QImage
from PyQt5.QtCore import Qt, QTimer
from collections import deque
from pynput import keyboard
import numpy as npclass VideoPlayer(QMainWindow):def __init__(self):super().__init__()# Set up the windowself.setWindowTitle('Drag and Drop MP4 Player')self.setGeometry(100, 100, 800, 600)self.setAcceptDrops(True)# Main widget and layoutself.widget = QWidget(self)self.layout = QVBoxLayout(self.widget)self.setCentralWidget(self.widget)# Label to display video framesself.video_label = QLabel(self)self.video_label.setAlignment(Qt.AlignCenter)self.layout.addWidget(self.video_label)# Instructions labelself.label = QLabel("Drag and drop an MP4 file here to play", self)self.label.setAlignment(Qt.AlignCenter)self.layout.addWidget(self.label)# Video variablesself.cap = Noneself.frame_buffer = deque(maxlen=300)self.queue_index = -1self.load_new_frame = Trueself.frame_index = -1# Timer for video playbackself.timer = QTimer(self)self.timer.timeout.connect(self.display_frame)# Start keyboard listenerlistener = keyboard.Listener(on_press=self.on_press)listener.start()def on_press(self, key):if key == keyboard.Key.right or key == keyboard.Key.space:  # Right arrow keyif self.queue_index < len(self.frame_buffer) - 1:self.queue_index += 1else:self.load_new_frame = Trueelif key == keyboard.Key.left:  # Left arrow keyif self.queue_index > 0:self.queue_index -= 1self.load_new_frame = Falseelif str(key) == "'q'":  # Press 'q' to exitself.close()def dragEnterEvent(self, event):if event.mimeData().hasUrls():event.accept()else:event.ignore()def dropEvent(self, event):for url in event.mimeData().urls():file_path = url.toLocalFile()if file_path.lower().endswith(".mp4"):self.load_video(file_path)def load_video(self, file_path):if self.cap:self.cap.release()self.cap = cv2.VideoCapture(file_path)self.frame_buffer.clear()self.queue_index = -1self.frame_index = -1self.load_new_frame = True# Start playing videoself.timer.start(30)  # Start timer to update frames every 30msdef display_frame(self):if self.load_new_frame and self.cap:ret, frame = self.cap.read()if not ret:self.timer.stop()print('Video ended')returnself.frame_index += 1cv2.putText(frame, str(self.frame_index), (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)self.frame_buffer.append(frame)self.queue_index = len(self.frame_buffer) - 1self.load_new_frame = False# Show the current frameif len(self.frame_buffer) > 0:imshow = self.frame_buffer[self.queue_index]self.show_frame(imshow)def show_frame(self, frame):# Convert the frame from OpenCV (BGR) format to RGB formatrgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)h, w, ch = rgb_frame.shapebytes_per_line = ch * wq_img = QImage(rgb_frame.data, w, h, bytes_per_line, QImage.Format_RGB888)# Set the QPixmap to the QLabelself.video_label.setPixmap(QPixmap.fromImage(q_img).scaled(self.video_label.width(), self.video_label.height(), Qt.KeepAspectRatio))def closeEvent(self, event):if self.cap:self.cap.release()self.timer.stop()event.accept()if __name__ == '__main__':app = QApplication(sys.argv)player = VideoPlayer()player.show()sys.exit(app.exec_())

这篇关于mp4 显示 按帧 pyqt的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1092589

相关文章

RedisTemplate默认序列化方式显示中文乱码的解决

《RedisTemplate默认序列化方式显示中文乱码的解决》本文主要介绍了SpringDataRedis默认使用JdkSerializationRedisSerializer导致数据乱码,文中通过示... 目录1. 问题原因2. 解决方案3. 配置类示例4. 配置说明5. 使用示例6. 验证存储结果7.

idea中project的显示问题及解决

《idea中project的显示问题及解决》:本文主要介绍idea中project的显示问题及解决方案,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录idea中project的显示问题清除配置重China编程新生成配置总结idea中project的显示问题新建空的pr

在 PyQt 加载 UI 三种常见方法

《在PyQt加载UI三种常见方法》在PyQt中,加载UI文件通常指的是使用QtDesigner设计的.ui文件,并将其转换为Python代码,以便在PyQt应用程序中使用,这篇文章给大家介绍在... 目录方法一:使用 uic 模块动态加载 (不推荐用于大型项目)方法二:将 UI 文件编译为 python 模

电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案

《电脑显示mfc100u.dll丢失怎么办?系统报错mfc90u.dll丢失5种修复方案》最近有不少兄弟反映,电脑突然弹出“mfc100u.dll已加载,但找不到入口点”的错误提示,导致一些程序无法正... 在计算机使用过程中,我们经常会遇到一些错误提示,其中最常见的就是“找不到指定的模块”或“缺少某个DL

Python GUI框架中的PyQt详解

《PythonGUI框架中的PyQt详解》PyQt是Python语言中最强大且广泛应用的GUI框架之一,基于Qt库的Python绑定实现,本文将深入解析PyQt的核心模块,并通过代码示例展示其应用场... 目录一、PyQt核心模块概览二、核心模块详解与示例1. QtCore - 核心基础模块2. QtWid

Linux虚拟机不显示IP地址的解决方法(亲测有效)

《Linux虚拟机不显示IP地址的解决方法(亲测有效)》本文主要介绍了通过VMware新装的Linux系统没有IP地址的解决方法,主要步骤包括:关闭虚拟机、打开VM虚拟网络编辑器、还原VMnet8或修... 目录前言步骤0.问题情况1.关闭虚拟机2.China编程打开VM虚拟网络编辑器3.1 方法一:点击还原VM

CSS模拟 html 的 title 属性(鼠标悬浮显示提示文字效果)

《CSS模拟html的title属性(鼠标悬浮显示提示文字效果)》:本文主要介绍了如何使用CSS模拟HTML的title属性,通过鼠标悬浮显示提示文字效果,通过设置`.tipBox`和`.tipBox.tipContent`的样式,实现了提示内容的隐藏和显示,详细内容请阅读本文,希望能对你有所帮助... 效

使用PyQt实现简易文本编辑器

《使用PyQt实现简易文本编辑器》这篇文章主要为大家详细介绍了如何使用PyQt5框架构建一个简单的文本编辑器,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录分析主窗口类 (MyWindow)菜单操作语法高亮 (SyntaxHighlighter)运行程序主要组件代码图示分析实现

如何设置vim永久显示行号

《如何设置vim永久显示行号》在Linux环境下,vim默认不显示行号,这在程序编译出错时定位错误语句非常不便,通过修改vim配置文件vimrc,可以在每次打开vim时永久显示行号... 目录设置vim永久显示行号1.临时显示行号2.永www.chinasem.cn久显示行号总结设置vim永久显示行号在li

电脑显示hdmi无信号怎么办? 电脑显示器无信号的终极解决指南

《电脑显示hdmi无信号怎么办?电脑显示器无信号的终极解决指南》HDMI无信号的问题却让人头疼不已,遇到这种情况该怎么办?针对这种情况,我们可以采取一系列步骤来逐一排查并解决问题,以下是详细的方法... 无论你是试图为笔记本电脑设置多个显示器还是使用外部显示器,都可能会弹出“无HDMI信号”错误。此消息可能