Python cv2.VideoWriter的输出不正确。它';快一点

Python cv2.VideoWriter的输出不正确。它';快一点,python,opencv,image-processing,video,raspberry-pi,Python,Opencv,Image Processing,Video,Raspberry Pi,我正在尝试使用opencv的cv2.VideoWriter录制特定时间的视频。问题是输出不正确。例如,10秒的视频只有2秒,而且播放速度更快,就像加速一样。 这是我的密码。欢迎提出任何建议或想法。另外,另一个问题是输出视频是静音的。谢谢 主持人:树莓皮 语言:Python import numpy as np import cv2 import time # Define the duration (in seconds) of the video capture here capture_d

我正在尝试使用opencv的cv2.VideoWriter录制特定时间的视频。问题是输出不正确。例如,10秒的视频只有2秒,而且播放速度更快,就像加速一样。 这是我的密码。欢迎提出任何建议或想法。另外,另一个问题是输出视频是静音的。谢谢

主持人:树莓皮

语言:Python

import numpy as np
import cv2
import time

# Define the duration (in seconds) of the video capture here
capture_duration = 10

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output3.avi',fourcc, 20.0, (640,480))

start_time = time.time()
while( int(time.time() - start_time) < capture_duration ):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()
将numpy导入为np
进口cv2
导入时间
#在此定义视频捕获的持续时间(以秒为单位)
捕获时间=10
cap=cv2.视频捕获(0)
#定义编解码器并创建VideoWriter对象
fourcc=cv2.视频编写器_fourcc(*“XVID”)
out=cv2.VideoWriter('output3.avi',fourcc,20.0,(640480))
开始时间=time.time()
while(int(time.time()-start\u time)
您忽略了代码中的两个重要因素:

while循环中的帧计数:

您希望以每秒20帧(fps)的速度写入10秒的视频。这将为整个视频提供总共200帧。要实现这一点,您需要意识到在捕获每个帧并将其写入文件之前while循环中的等待时间。如果忽略等待时间,则:

  frameCount = 0
  while( int(time.time() - start_time) < capture_duration ):
      # we assume that all the operations inside the loop take 0 seconds to accomplish.

      frameCount = frameCount+1

  print('Total frames: ',frameCount)

它使用的是Genius iSlim 2000AF V2真正的即插即用。根据摄像头的规格,它支持640 x 480@30 fps的视频捕获。然而,我在10秒内只得到了60帧,即每秒6帧。您有什么想法吗?要检查您的相机属性:
  frameCount = 0
  while( int(time.time() - start_time) < capture_duration ):
      # wait 50 milliseconds before each frame is written.
      cv2.waitKey(50)

      frameCount = frameCount+1

  print('Total frames: ',frameCount)
  frameCount = 0
  while( int(time.time() - start_time) < capture_duration ):
      # wait for camera to grab next frame
      ret, frame = cap.read()
      # count number of frames
      frameCount = frameCount+1

  print('Total frames: ',frameCount)