无法在Python中通过OpenCV编写视频 [英] Can't write video by opencv in Python
问题描述
我是一位开放的简历初学者.我按照本教程中的步骤练习使用它. http://opencv-python -tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
I am an open cv beginner. I followed the steps in this tutorial to practice using it. http://opencv-python-tutroals.readthedocs.org/en/latest/py_tutorials/py_gui/py_video_display/py_video_display.html#display-video
我在Mac上更改了几行以提高性能:
I changed some lines for performance on my mac:
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
# Define the codec and create VideoWriter object
fourcc = cv2.cv.CV_FOURCC(*'DIVX') # upper case - yl3
out = cv2.VideoWriter('output.avi',fourcc, 20, size, 1) #20.0: number of frames per sec
while(cap.isOpened()):
ret, frame = cap.read()
if ret==True:
frame = cv2.flip(frame,1)
# 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()
我正在使用Python 2.7.10,opencv 2.4.12和mac OS X 10.10.5
现在,我可以成功使相机工作并在同一目录中生成output.avi
文件,但是该文件仅414k超时,无法打开它.看来它只包含黑色.
I am using Python 2.7.10, opencv 2.4.12 and mac OS X 10.10.5
Now I can successfully get the camera to work and generate an output.avi
file in the same directory, but the file was only 414k overtime and I can't open it. It seemed that it contained nothing but black.
有人可以帮我吗?
推荐答案
所以我和你有同样的问题.我将其范围缩小到cv2.cv.CV_FOURCC
的值.这些显然是区分大小写的.我无法使.avi
编解码器正常工作,但是,如果您不介意MPEG
,则对我有用.
So I had the same problem as you. I narrowed it down to the value of cv2.cv.CV_FOURCC
. Those are apparently case sensitive. I wasn't able to get the .avi
codecs working, but if you don't mind a MPEG
, this worked for me.
import numpy as np
import cv2
cap = cv2.VideoCapture(0)
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)),
int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)))
fps = 20
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case
vout = cv2.VideoWriter()
success = vout.open('output.mp4',fourcc,fps,size,True)
while True:
ret, frame = cap.read()
if not ret:
break
frame = cv2.flip(frame, -1)
vout.write(frame)
cv2.imshow('frame', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
vout.release()
cv2.destroyAllWindows()
这篇关于无法在Python中通过OpenCV编写视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!