问题:通过opencv录制下来的视频,打开的时候显示“无法解码多工流传输的视频”。
笔记本自带的摄像头,录制方法就是用opencv的VideoWrite。
问题原因发现及解决:网上看了很多录制下来打不开的原因,无非就是保存的帧率、视频尺寸和相机不匹配,去跟淘宝问了个软件,就是很简单的显示相机参数的软件,然后发现尺寸和我想的不一样(实际上我用opencv去设置了相机参数,但成功的话应该没有这个问题的了,这个问题…以后有时间研究一下),就是把尺寸调好以后就正常了。
# 录制测试视频
# coding:utf-8
import cv2
import numpy as np
# 选择摄像头
cap = cv2.VideoCapture(0)
# 设置编码器
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 25, (640,480))
while True:
ret,frame = cap.read()
if ret == True:
frame = cv2.flip(frame, 1)
out.write(frame)
cv2.imshow("frame", frame)
key = cv2.waitKey(1)
if key == 27:
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
# 播放视频
# coding:utf-8
import cv2
import numpy as np
cap = cv2.VideoCapture("output.avi")
while True:
ret, frame = cap.read()
if ret is False:
print("......")
break
cv2.imshow("frame", frame)
key = cv2.waitKey(30)
if key == 27:
break
cv2.destroyAllWindows()
在Ubuntu系统下如何使用v4l-utils工具来查看所连接摄像头(包含USB摄像头及树莓派摄像头)信息。
sudo apt-get install v4l-utils
(base) zz@xl:~/work/code$ ls /dev/video*
/dev/video0 /dev/video1 /dev/video2 /dev/video3
或
(base) zz@xl:~/work/code$ v4l2-ctl --list-devices
Integrated Camera: Integrated C (usb-0000:00:14.0-7):
/dev/video0
/dev/video1
/dev/video2
/dev/video3
(base) zz@xl:~/work/code$ v4l2-ctl -d /dev/video0 --list-formats-ext
ioctl: VIDIOC_ENUM_FMT
Type: Video Capture
[0]: 'MJPG' (Motion-JPEG, compressed)
Size: Discrete 1280x720
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x180
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 352x288
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 424x240
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 848x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 960x540
Interval: Discrete 0.033s (30.000 fps)
[1]: 'YUYV' (YUYV 4:2:2)
Size: Discrete 640x480
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x180
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 320x240
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 352x288
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 424x240
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 640x360
Interval: Discrete 0.033s (30.000 fps)
Size: Discrete 848x480
Interval: Discrete 0.050s (20.000 fps)
Size: Discrete 960x540
Interval: Discrete 0.067s (15.000 fps)
Size: Discrete 1280x720
Interval: Discrete 0.100s (10.000 fps)
(1)如上所示的/dev/video1 USB摄像头支持 MJPG 和 YUYV 两种像素格式,每种格式支持多种分辨率及帧率。如果需要实时抓取1080p的图像,则需要采用MJPG格式,默认的YUYV格式不能满足实时抓取需求。
(2)另外,调用摄像头时传入的摄像头设备、图像帧宽、帧高、帧率,应与v4l2-ctl 打印的参数一致。
#include
#include
int main()
{
cv::VideoCapture capture;capture.open("/dev/video1", cv::CAP_V4L2);
capture.set(cv::CAP_PROP_FOURCC, cv::VideoWriter::fourcc('M','J','P','G'));
capture.set(cv::CAP_PROP_FRAME_WIDTH, 1920);
capture.set(cv::CAP_PROP_FRAME_HEIGHT, 1080);
capture.set(cv::CAP_PROP_FPS, 30);
cv::Mat frame;
while (1) {
capture >> frame;
if (!frame.empty())
{
cv::imshow("frame", frame);
}
if (cv::waitKey(1) == 27)
break;
}
return 0;
}