我正在使用Python/PyQt5创建一个GUI,它应该在同一窗口中显示视频和其他小部件。我已经尝试了不同的方法来解决这个问题,但似乎无法让它工作。
方法1:使用 OpenCV/cv2 在像素图中添加视频仅显示视频的第一帧。
方法2:我已经设法使用 cv2 播放视频,但是它会在新窗口中打开。
方法3:我也尝试使用QVideoWidget,但显示空白屏幕,视频无法播放。
# only shows an image from the video, but in the correct place
cap = cv2.VideoCapture('video.mov')
ret, frame = cap.read()
if ret:
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
pix = QPixmap.fromImage(img)
pix = pix.scaled(600, 400, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.ui.label_7.setPixmap(pix)
# opens new window
cap = cv2.VideoCapture('video.mov')
while (cap.isOpened()):
ret, frame = cap.read()
self.ui.frame = cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
# shows a blank screen
self.mediaPlayer = QMediaPlayer(None, QMediaPlayer.VideoSurface)
videoWidget = self.ui.vid_widget
self.mediaPlayer.setVideoOutput(videoWidget)
self.mediaPlayer.setMedia(QMediaContent(QUrl.fromLocalFile('video.mov')))
有关如何在另一个小部件/同一窗口中播放视频的任何帮助将不胜感激。
在 while
循环中,您需要再次将frame
转换为QPixmap
,类似于上面所做的,然后更新ui
:
cap = cv2.VideoCapture('video.mov')
while (cap.isOpened()):
ret, frame = cap.read()
if not ret:
break
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
img = QImage(frame, frame.shape[1], frame.shape[0], QImage.Format_RGB888)
pix = QPixmap.fromImage(img)
pix = pix.scaled(600, 400, Qt.KeepAspectRatio, Qt.SmoothTransformation)
self.ui.label_7.setPixmap(pix)
self.ui.frame = pix # or img depending what `ui.frame` needs
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()