【问题标题】:Opencv2: Python: cv2.VideoWriterOpencv2:Python:cv2.VideoWriter
【发布时间】:2017-09-12 19:03:40
【问题描述】:

为什么下面的代码不保存视频? 网络摄像头的帧速率是否必须与VideoWriter 帧大小完全匹配?

import numpy as np
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)

【问题讨论】:

  • 您的系统上是否安装了Xvid 编解码器?
  • 是的,但我仍然有同样的问题

标签: python opencv


【解决方案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)
...

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2013-03-13
      • 2019-11-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-05
      • 1970-01-01
      • 2014-08-01
      相关资源
      最近更新 更多