2014-02-06 76 views
8

我已經看過OpenCV的Python example關於如何使用VideoCaptureVideoWriter來捕獲和寫出視頻文件。但我不斷收到:如何使用OpenCV和Python錄製視頻?

OpenCV Error: Assertion failed (dst.data == dst0.data) in cvCvtColor, file 
/tmp/opencv-n8PM/opencv-2.4.7.1/modules/imgproc/src/color.cpp, line 4422 
Traceback (most recent call last): 
    File "examples/observer/observer.py", line 17, in <module> 
    video_writer.write(frame) 
cv2.error: /tmp/opencv-n8PM/opencv-2.4.7.1/modules/imgproc/src/color.cpp:4422: error: 
(-215) dst.data == dst0.data in function cvCvtColor 

清理照相機。

下面是代碼:

#!/usr/bin/env python import cv2 


if __name__ == "__main__": 
    # find the webcam 
    capture = cv2.VideoCapture(0) 

    # video recorder 
    fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist 
    video_writer = cv2.VideoWriter("output.avi", fourcc, 20, (680, 480)) 

    # record video 
    while (capture.isOpened()): 
     ret, frame = capture.read() 
     if ret: 
      video_writer.write(frame) 
      cv2.imshow('Video Stream', frame) 

     else: 
      break 

    capture.release() 
    video_writer.release() 
    cv2.destroyAllWindows() 
+0

它工作嗎?您可能會接受一個答案,或編輯您的解決方案。 –

回答

4

在C++中,如果你能傳遞-1的編解碼器。然後您可以手動選擇機器上所有編解碼器的編解碼器。可能在python中是一樣的,但我在文檔中找不到它。

video_writer = cv2.VideoWriter("output.avi", -1, 20, (680, 480)) 

試着確保opencv能在你的機器上找到XVID。

+2

傳遞-1作爲編解碼器也適用於python。 –

8

幀的大小可能不正確:

w=int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)) 
h=int(capture.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT)) 
# video recorder 
fourcc = cv2.cv.CV_FOURCC(*'XVID') # cv2.VideoWriter_fourcc() does not exist 
video_writer = cv2.VideoWriter("output.avi", fourcc, 25, (w, h)) 

工作對我來說

4

我面臨着類似的問題。 如果問題出現在幀大小和顏色深度或編解碼器中,您應該進行調試。嘗試將空數組寫入文件中:

capSize = (100, 100) # this is the size of my source video 
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') 
out = cv2.VideoWriter('output.mov',fourcc, 1, capSize) 
... 
out.write(125 * np.ones((100,100,3), np.uint8)) 
...