opencv录制与播放视频设置

录制
CvCapture* capture=cvCaptureFromCAM(0);//除了0以外,还可以填其他的数字,这些数字标记着电脑上已连接的USB摄像头,改变数值就可以选择电脑上其他摄像头
CvVideoWriter* video=NULL;//新建一个视频写入器
video=cvCreateVideoWriter("文件名",-1,fps,cvSize(frame->width,frame->eight));
利用文件名可以根据后缀,识别视频类型,通过设置文件名后的参数为-1,会有个选择对话框给你选择编码库和编码质量等细节

利用cvSetCaptureProperty,可以设置录取视频的帧率和帧高宽等参数,但是必须要在摄像头支持范围内
cvSetCaptureProperty(capture,CV_CAP_PROP_FPS, fps);帧率
cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_WIDTH, width);
 宽  

cvSetCaptureProperty(capture, CV_CAP_PROP_FRAME_HEIGHT,height);高


播放

播放会利用ffmpeg来自动识别文件名的后缀,和解码
 CvCapture* captureL = cvCaptureFromFile("视频文件名路径+文件名");
通过cvGetCaptureProperty函数可以获得每一帧的高宽,波特率,视频总帧数,当前播放帧

的信息如在视频序列的第几帧或离起始点多少ms等
///获得视频的帧数和帧高宽,总帧数
   double frameLHeight=(double)cvGetCaptureProperty

(captureL,CV_CAP_PROP_FRAME_HEIGHT );高
   double frameLWidth=(double)cvGetCaptureProperty

(captureL,CV_CAP_PROP_FRAME_WIDTH);宽
   double frameLFps=(double)cvGetCaptureProperty(captureL,CV_CAP_PROP_FPS );帧率,有时对于某些格式会获取有错
   double frameLRatio=(double)cvGetCaptureProperty(captureL,CV_CAP_PROP_POS_AVI_RATIO);码率
   double frameLNumFrames=(double)cvGetCaptureProperty(captureL,CV_CAP_PROP_FRAME_COUNT);帧数
 
  

   /设置从视频哪里开始读取,但参数为CV_CAP_PROP_POS_MSEC,跟着的参数

为离起始点多少ms
  cvSetCaptureProperty(captureL,CV_CAP_PROP_POS_MSEC,long(1000)); //这里设置为1000ms

利用cvSetCaptureProperty,cvGetCaptureProperty,以及滑动条和ffmpeg和Portaudio即可实现一个支持多种格式的视频播放器

 

要使用 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)) ``` 完整的代码如下:
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值