2012-05-15 88 views
14

我想在OSX下從OpenCV的python包裝創建視頻。我正在使用python 2.7.1,opencv 2.3.1a以及來自那個opencv版本的willowgarage的python包裝。我有:opencv VideoWriter在OSX下產生沒有輸出

import cv,cv2 
w = cv2.VideoWriter('foo.avi', cv.FOURCC('M','J','P','G'), 25, (100,100)) 
for i in range(100): 
    w.write(np.ones((100,100,3), np.uint8)) 

OpenCV的說

WARNING: Could not create empty movie file container. 
Didn't successfully update movie file. 
... [ 100 repetitions] 

我不知道下一個嘗試

回答

45

在這個話題上有很多過時和不正確的在線指南 - 我想我幾乎每一個都嘗試過。望着基於QTKit代碼實現VideoWriter在Mac OSX後,我終於可以用下面的代碼來獲得VideoWriter到輸出有效的視頻文件:

fps = 15 
capSize = (1028,720) # this is the size of my source video 
fourcc = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') # note the lower case 
self.vout = cv2.VideoWriter() 
success = self.vout.open('output.mov',fourcc,fps,capSize,True) 

要寫入圖像幀(注意,imgFrame必須尺寸均相同翻船高於或更新會失敗):

self.vout.write(imgFrame) 

完成後一定要:

vout.release() 
self.vout = None 

這對我的作品在Mac OS X 10.8.5(山獅):沒有關於其他平臺的保證。我希望這段代碼可以節省別人幾小時的實驗經驗!

+5

我可以確認這段代碼適用於小牛,Python 2.7,openCV2 –

+1

在到達此處之前嘗試了至少五個其他的答案。這是唯一一個在小牛/ Python 2.7和OpenCV 2.4.6.1上爲我工作的人。 –

+5

與其他評論者的情況相同。我發現決定性的因素是使用小寫的''m','p','4','v'。許多其他示例顯示用於'fourcc'的大寫字母,我不知道它是區分大小寫的。 –

1

這不是好主意

您必須創建一個VideoWriter結構和使用方法WriteFrame

import cv 
cv.NamedWindow('cap', 1) 
w = cv.CreateVideoWriter('test.avi',cv.CV_FOURCC('X','V','I','D'),25,(640,480)) 
cap = cv.CaptureFromCAM(0) 
while(1): 
    img = cv.QueryFrame(cap) 
    cv.ShowImage('cap', img) 
    cv.WriteFrame(w, img) 
    if cv.WaitKey(1) == 27: 
     break 
cv.DestroyWindow('cap') 
+0

這個答案現在看起來已經過時了。 – JohnAllen

1

您的ffmpeg版本可能已過期。我遇到了與ffmpeg 0.8類似的問題,並且在升級到ffmpeg 0.11後它工作正常。

1

這裏的@ ToddStellanova的回答爲我工作的變化:

def write_video(image_dir): 
    ''' 
    Writes a video from a set of images in `image_dir` 
    ''' 
    target = join('data/labelled-videos', 
       basename(image_dir) + '.mp4v') 
    codec = cv2.cv.CV_FOURCC('m', 'p', '4', 'v') 
    size = (256, 256) 
    v = cv2.VideoWriter(target, codec, 16, size) 
    for image_name in listdir(image_dir): 
    image_filename = join(image_dir, image_name) 
    arr = np.asarray(Image.open(image_filename)) 
    assert arr.shape[:2] == size 
    v.write(arr) 
1

後嘗試不同的選擇,我發現,我用不適合在VideoWriter指定大小frame.size:所以將其設置爲默認的我的iMac 1280x720使事情工作!

import numpy as np 
import cv2 

cap = cv2.VideoCapture(0) 

# Define the codec and create VideoWriter object 
fourcc = cv2.VideoWriter_fourcc('m', 'p', '4', 'v') 
out = cv2.VideoWriter() 
succes = out.open('output.mp4v',fourcc, 15.0, (1280,720),True) 
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() 
2

這是「大小」問題。

import cv2 
import time 

filename = time.strftime("%m-%d-%H-%M-%S") + '.avi' 
fps = 16 

cap = cv2.VideoCapture(0) 

#in this way it always works, because your get the right "size" 
size = (int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_WIDTH)), 
     int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_HEIGHT))) 
fourcc = cv2.cv.FOURCC('8', 'B', 'P', 'S')  #works, large 
out = cv2.VideoWriter(filename, fourcc, fps, size, True) 

#in this way, you must set the "size" to your size, 
#because you don't know the default "size" is right or not 
#cap.set(3, 640) 
#cap.set(4, 480) 
#out = cv2.VideoWriter(filename, fourcc, fps, (640, 480), True) 

while(cap.isOpened()): 
    ret, frame = cap.read() 
    if ret == True: 
     out.write(frame) 
     cv2.imshow('frame', frame) 

    if cv2.waitKey(1) & 0xFF == ord('q'): 
     break; 

else: 
    print 'Error...' 
    break; 

cap.release() 
out.release() 
cv2.destroyAllWindows() 
0

由於@TeddStellanova

這裏的XCode C++版本:

int fWidth = capture.get(CV_CAP_PROP_FRAME_WIDTH); 
int fHeight = capture.get(CV_CAP_PROP_FRAME_HEIGHT); 
string vid_path = path + "frames.mov"; 
VideoWriter vid(vid_path,CV_FOURCC('m','p','4','v'),30,Size(fWidth,fHeight),true); 
0

這裏是與工作的版本:

  • 的Python 3.6.3
  • OpenCV的3.3。 1
  • macOS 10.13.1

安裝了brew install opencv

#!/usr/bin/env python3 

import cv2 

def main(): 
    vc = cv2.VideoCapture() 
    if not vc.open('input.mp4'): 
     print('failed to open video capture') 
     return 

    fourcc = cv2.VideoWriter_fourcc(*'mp4v') 
    # Match source video features. 
    fps = vc.get(cv2.CAP_PROP_FPS) 
    size = (
     int(vc.get(cv2.CAP_PROP_FRAME_WIDTH)), 
     int(vc.get(cv2.CAP_PROP_FRAME_HEIGHT)), 
    ) 

    vw = cv2.VideoWriter() 
    if not vw.open('output.mp4', fourcc, fps, size): 
     print('failed to open video writer') 
     return 

    while True: 
     ok, frame = vc.read() 
     if not ok: 
      break 

     # Flip upside down. 
     frame = cv2.flip(frame, 0) 

     # Write processed frame. 
     vw.write(frame) 

     cv2.imshow('frame', frame) 
     if cv2.waitKey(1) & 0xFF == ord('q'): 
      break 

    vc.release() 
    vw.release() 
    cv2.destroyAllWindows() 

if __name__ == "__main__": 
    main()