PyQt+OpenCV 录制保存、播放视频
时间: 2024-01-16 11:04:37 浏览: 353
要使用 PyQt 结合 OpenCV 进行录制保存和播放视频,可以按照以下步骤进行:
1. 安装 PyQt 和 OpenCV 库:
```
pip install PyQt5
pip install opencv-python
```
2. 创建一个 PyQt 窗口,用于显示视频:
```python
import sys
from PyQt5.QtWidgets import QApplication, QWidget, QLabel
from PyQt5.QtGui import QImage, QPixmap
class VideoPlayer(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel(self)
self.setGeometry(100, 100, 640, 480)
self.show()
```
3. 初始化 OpenCV 的摄像头,并将视频帧显示在 PyQt 窗口中:
```python
import cv2
class VideoPlayer(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel(self)
self.setGeometry(100, 100, 640, 480)
self.show()
self.cap = cv2.VideoCapture(0)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(50)
def update_frame(self):
ret, frame = self.cap.read()
if ret:
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = image.shape
bytes_per_line = ch * w
convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888)
p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.label.setPixmap(QPixmap.fromImage(p))
```
4. 添加录制和保存视频的功能:
```python
class VideoPlayer(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel(self)
self.setGeometry(100, 100, 640, 480)
self.show()
self.cap = cv2.VideoCapture(0)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(50)
self.recording = False
self.video_writer = None
def update_frame(self):
ret, frame = self.cap.read()
if ret:
if self.recording:
if self.video_writer is None:
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
self.video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
self.video_writer.write(frame)
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = image.shape
bytes_per_line = ch * w
convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888)
p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.label.setPixmap(QPixmap.fromImage(p))
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space:
self.recording = not self.recording
if not self.recording and self.video_writer is not None:
self.video_writer.release()
self.video_writer = None
```
5. 添加播放已保存视频的功能:
```python
class VideoPlayer(QWidget):
def __init__(self):
super().__init__()
self.label = QLabel(self)
self.setGeometry(100, 100, 640, 480)
self.show()
self.cap = cv2.VideoCapture(0)
self.timer = QTimer(self)
self.timer.timeout.connect(self.update_frame)
self.timer.start(50)
self.recording = False
self.video_writer = None
self.playing = False
self.video_capture = None
def update_frame(self):
ret, frame = self.cap.read()
if ret:
if self.recording:
if self.video_writer is None:
fourcc = cv2.VideoWriter_fourcc(*'MJPG')
self.video_writer = cv2.VideoWriter('output.avi', fourcc, 20.0, (640, 480))
self.video_writer.write(frame)
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = image.shape
bytes_per_line = ch * w
convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888)
p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.label.setPixmap(QPixmap.fromImage(p))
def keyPressEvent(self, event):
if event.key() == Qt.Key_Space:
self.recording = not self.recording
if not self.recording and self.video_writer is not None:
self.video_writer.release()
self.video_writer = None
elif event.key() == Qt.Key_P:
self.playing = not self.playing
if self.playing:
self.video_capture = cv2.VideoCapture('output.avi')
self.timer.stop()
self.timer.timeout.connect(self.update_saved_frame)
self.timer.start(1000//30)
else:
self.timer.stop()
self.timer.timeout.connect(self.update_frame)
self.timer.start(50)
def update_saved_frame(self):
ret, frame = self.video_capture.read()
if ret:
image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
h, w, ch = image.shape
bytes_per_line = ch * w
convert_to_QtFormat = QImage(image.data, w, h, bytes_per_line, QImage.Format_RGB888)
p = convert_to_QtFormat.scaled(640, 480, Qt.KeepAspectRatio)
self.label.setPixmap(QPixmap.fromImage(p))
```
完整的代码如下:
阅读全文