Opencv2:Python:cv2.VideoWriter

时间:2017-09-12 19:03:41

标签: python opencv

为什么以下代码不保存视频? 同样强制要求网络摄像头的帧速率与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)

提前感谢您的帮助。

2 个答案:

答案 0 :(得分:1)

我怀疑您正在使用libv4l版本的OpenCV进行视频I / O. OpenCV的libv4l API中存在一个错误,导致VideoCapture::set方法无法更改视频分辨率。请参阅链接123。如果您执行以下操作:

...
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)
...

答案 1 :(得分:0)

输出框架和输入框架的大小必须相同才能写入...