2017-09-12 28 views
1

爲什麼下面的代碼不能保存視頻? 網絡攝像頭的幀速率是否與VideoWriter幀大小完全匹配也是強制性的?Opencv2:Python:cv2.VideoWriter

import numpy as np`enter code here 
import cv2 
import time 

def videoaufzeichnung(video_wdth,video_hight,video_fps,seconds): 
    cap = cv2.VideoCapture(6) 
    cap.set(3,video_wdth) # wdth 
    cap.set(4,video_hight) #hight 
    cap.set(5,video_fps) #hight 

    # Define the codec and create VideoWriter object 
    fps = cap.get(5) 
    fourcc = cv2.VideoWriter_fourcc(*'XVID') 
    out = cv2.VideoWriter('output.avi',fourcc,video_fps, (video_wdth,video_hight)) 
    #out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480)) 

    start=time.time() 
    zeitdauer=0 
    while(zeitdauer<seconds): 
     end=time.time() 
     zeitdauer=end-start 
     ret, frame = cap.read() 
     if ret==True: 
      frame = cv2.flip(frame,180) 
      # 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()` 

videoaufzeichnung.videoaufzeichnung(1024,720,10,30) 

在此先感謝您的幫助。

+0

您的系統上是否安裝了'Xvid'編解碼器? – zindarod

+0

是的,但我仍然有同樣的問題 – user8495738

回答

1

我懷疑你使用的是OpenCV的libv4l版本的視頻I/O。 OpenCV的libv4l API中存在一個錯誤,它可以防止VideoCapture::set方法更改視頻分辨率。見鏈接1,23。如果你做到以下幾點:

... 
frame = cv2.flip(frame,180) 
print(frame.shape[:2] # check to see frame size 
out.write(frame) 
... 

你會注意到,幀大小沒有被修改以匹配函數的參數提供的分辨率。克服此限制的一種方法是手動調整幀大小以匹配分辨率參數。

... 
frame = cv2.flip(frame,180) 
frame = cv2.resize(frame,(video_wdth,video_hight)) # manually resize frame 
print(frame.shape[:2] # check to see frame size 
out.write(frame) 
...