首页 文章

Opencv2:Python:cv2.VideoWriter

提问于
浏览
1

为什么以下代码不保存视频?此外,网络摄像头的帧速率是否与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)

在此先感谢您的帮助 .

1 回答

  • 1

    我怀疑你正在使用 libv4l 版本的OpenCV进行视频I / O.有's a bug in OpenCV' s 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)
    ...
    

相关问题