视频正在保存但仅显示 0 KB。这些 OpenCv 代码中可能出现的错误是什么
video is saving but showing 0 KB only. What is the probable error in these code of OpenCv
我正在尝试使用 python 在 openCv 中使用 videocapture 对象保存视频。但在按下 'q' 后,视频保存为 'output.avi' 但其大小显示为 0 KB。需要您的帮助,无法找出错误。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#1.2. Gui Features in OpenCV 25
#OpenCV-Python Tutorials Documentation, Release 1
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
这里需要玩一下fourCC码
FourCC 代码作为 cv2.VideoWriter_fourcc('M','J','P','G') 或 cv2.VideoWriter_fourcc(*'MJPG ) 对于 MJPG。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
更多阅读,OpenCV saving doc and FourCC doc
可能 XVID
的 codecs
没有正确安装。
(如果您使用的是 Jupyter Notebook,则会有以 OpenCV: FFMPEG: ...
开头的 jupyter 日志)
如果您使用 linux,您可以尝试使用 cv2.VideoWriter_fourcc(*'mp4v')
,如下所示:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', -1, 20.0, (640,480))
注意,扩展名mp4
很重要!应选择与 codec
.
匹配的正确扩展名
我正在尝试使用 python 在 openCv 中使用 videocapture 对象保存视频。但在按下 'q' 后,视频保存为 'output.avi' 但其大小显示为 0 KB。需要您的帮助,无法找出错误。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
#1.2. Gui Features in OpenCV 25
#OpenCV-Python Tutorials Documentation, Release 1
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
这里需要玩一下fourCC码
FourCC 代码作为 cv2.VideoWriter_fourcc('M','J','P','G') 或 cv2.VideoWriter_fourcc(*'MJPG ) 对于 MJPG。
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
# Define the codec and create VideoWriter object
#fourcc = cv2.cv.CV_FOURCC(*'DIVX')
#out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))
out = cv2.VideoWriter('output.avi', -1, 20.0, (640,480))
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,0)
# write the flipped frame
out.write(frame)
cv2.imshow('frame',frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
else:
break
# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
更多阅读,OpenCV saving doc and FourCC doc
可能 XVID
的 codecs
没有正确安装。
(如果您使用的是 Jupyter Notebook,则会有以 OpenCV: FFMPEG: ...
开头的 jupyter 日志)
如果您使用 linux,您可以尝试使用 cv2.VideoWriter_fourcc(*'mp4v')
,如下所示:
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter('output.mp4', -1, 20.0, (640,480))
注意,扩展名mp4
很重要!应选择与 codec
.