import cv2
import numpy as np
cap = cv2.VideoCapture('1.MP4')
fourcc = cv2.FOURCC(*'XVID')
out = cv2.videoWriter('output.avi',fourcc,20.0,(640,480))
while (1):
#Capture frame-by-frame
#ret = cap.set(3,960),cap.set(4,720)
ret, frame = cap.read()
if ret == True:
frame = cv2.flip(frame,0)
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) == ord('q'):
break
else:
break
cap.release()
out.release()
cv2.destroyAllWindows()如上所示,这是一个在opencv document.while中的演示,它可以在我的PC上工作。像这样回溯:
Traceback (most recent call last):
File "E:\opencv\Pic\test.py", line 34, in <module>
fourcc = cv2.cv.FOURCC(*'XVID')
AttributeError: module 'cv2.cv2' has no attribute 'cv'我也看到了一些解决方案,比如将cv2.cv更改为cv2,而回溯如下:
Traceback (most recent call last):
File "E:\opencv\Pic\test.py", line 34, in <module>
fourcc = cv2.FOURCC(*'XVID')
AttributeError: module 'cv2.cv2' has no attribute 'FOURCC'我非常感谢热心解答我的问题。
发布于 2018-01-15 22:41:15
链接@Spence wetjen帖子是针对旧OpenCV的,在OpenCV 3.x中不起作用。
下面是我的代码:
环境: Python3 + OpenCV 3.3
#!/usr/bin/python3
import cv2
## opening videocapture
cap = cv2.VideoCapture(0)
## some videowriter props
sz = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
fps = 20
#fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
#fourcc = cv2.VideoWriter_fourcc('m', 'p', 'e', 'g')
fourcc = cv2.VideoWriter_fourcc(*'mpeg')
## open and set props
vout = cv2.VideoWriter()
vout.open('output.mp4',fourcc,fps,sz,True)
cnt = 0
while cnt<20:
cnt += 1
print(cnt)
_, frame = cap.read()
cv2.putText(frame, str(cnt), (10, 20), cv2.FONT_HERSHEY_PLAIN, 1, (0,255,0), 1, cv2.LINE_AA)
vout.write(frame)
vout.release()
cap.release()结果是:

发布于 2020-02-17 15:58:23
同时导入contrib库。
pip install opencv-contrib-python或
pip3 install opencv-contrib-pythonhttps://stackoverflow.com/questions/48251545
复制相似问题