我的代码:
cap = cv2.VideoCapture("1.mp4") # 获取视频对象
w = cap.get(cv2.CAP_PROP_FRAME_WIDTH) # 注意这里必须是整型
h = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
out = cv2.VideoWriter('./2.mp4', cv2.VideoWriter_fourcc("M","P","4","V"), 30.0,(w,h),True)
while True:
ret, img = cap.read()
if ret:
out.write(img)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
报错内容:
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'VideoWriter'
Overload resolution failed:
- Can't parse 'frameSize'. Sequence item with index 0 has a wrong type
- VideoWriter() missing required argument 'frameSize' (pos 5)
- VideoWriter() missing required argument 'params' (pos 5)
- VideoWriter() missing required argument 'frameSize' (pos 5)
或者是:
cv2.error: OpenCV(4.6.0) :-1: error: (-5:Bad argument) in function 'VideoWriter'
Overload resolution failed:
- Can't parse 'frameSize'. Sequence item with index 0 has a wrong type
- Argument 'fourcc' is required to be an integer
- Can't parse 'frameSize'. Sequence item with index 0 has a wrong type
- VideoWriter() missing required argument 'params' (pos 6)
问题:
因为帧的宽度和长度必须是整型的。而我们使用CAP_PROP_FRAME_WIDTH获取的值得类型是float。
解决办法:
将float类型转换为int整型。
例如:
cap = cv2.VideoCapture("1.mp4") # 获取视频对象
w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) # 注意这里必须是整型
h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
out = cv2.VideoWriter('./2.mp4', cv2.VideoWriter_fourcc("M","P","4","V"), 30.0,(w,h),True)
while True:
ret, img = cap.read()
if ret:
out.write(img)
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()
网友评论