
解决opencv保存视频打不开问题
opencv
OpenCV: 开源计算机视觉库
项目地址:https://gitcode.com/gh_mirrors/opencv31/opencv

·
问题描述
使用opencv来保存录制的视频,但是保存后的视频就是打不开,网上查找了很多资料,后面发现原来是保存的视频尺寸和被录制的视频尺寸不一样造成的,原本的代码如下
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
videoWrite = cv2.VideoWriter(r'../videos/test.mp4', fourcc, 30, (640, 480))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
videoWrite.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(33) & 0xFF == ord('q'):
break
videoWrite.release()
cap.release()
cv2.destroyAllWindows()
解决办法
解决方法很简单,统一尺寸即可,使用代码获取被录制视频宽和高。
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
解决后的完整代码如下
import cv2
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
videoWrite = cv2.VideoWriter(r'../videos/test.mp4', fourcc, 30, (width,height))
while cap.isOpened():
ret, frame = cap.read()
if not ret:
break
videoWrite.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(33) & 0xFF == ord('q'):
break
videoWrite.release()
cap.release()
cv2.destroyAllWindows()




OpenCV: 开源计算机视觉库
最近提交(Master分支:8 个月前 )
4ad4bd5d
Add additional information about homogeneous transformations in the calib3d doc 1 天前
956f583b
hal/riscv_rvv: implemented flip_inplace to boost cv::rotate 1 天前
更多推荐
所有评论(0)