简体   繁体   English

用opencv录制的视频被加速

[英]Video recorded with opencv is accelerated

I trying to record a 30 seconds video, but the output video are with just 15 seconds and accelerated. 我试图录制30秒的视频,但是输出视频只有15秒并且加速了。

import numpy as np
import os
import cv2
import time

filename = 'video.avi'
frames_per_second = 30.0
res = '480p'

def change_res(cap, width, height):
    cap.set(3, width)
    cap.set(4, height)

STD_DIMENSIONS =  {
    "480p": (640, 480),
    "720p": (1280, 720),
    "1080p": (1920, 1080),
    "4k": (3840, 2160),
}

def get_dims(cap, res='1080p'):
    width, height = STD_DIMENSIONS["480p"]
    if res in STD_DIMENSIONS:
        width,height = STD_DIMENSIONS[res]
    change_res(cap, width, height)
    return width, height

VIDEO_TYPE = {
    'avi': cv2.VideoWriter_fourcc(*'XVID'),
    'mp4': cv2.VideoWriter_fourcc(*'XVID'),
}

def get_video_type(filename):
    filename, ext = os.path.splitext(filename)
    if ext in VIDEO_TYPE:
      return  VIDEO_TYPE[ext]
    return VIDEO_TYPE['avi']

cap = cv2.VideoCapture(0)
fps = cap.get(cv2.CAP_PROP_FPS)
print(fps)

out = cv2.VideoWriter(filename, get_video_type(filename), 30, get_dims(cap, res))

stop_time = time.time() + 30.0
while stop_time >= time.time():
    ret, frame = cap.read()
    out.write(frame)

cap.release()
out.release()
cv2.destroyAllWindows()

I'm printed the FPS coming from video capture, and it's return 30.0. 我打印了来自视频捕获的FPS,返回30.0。 The camera is the Logitech C920. 相机是Logitech C920。

Any help would be awesome. 任何帮助都是极好的。

You are reading from your camera with VideoCapture and writing to your VideoWriter . 您正在使用VideoCapture从相机读取内容并将其写入VideoWriter You expect that the reading time will coincide with the writing time of your VideoWriter but obviously that's not the case. 您希望读取时间与VideoWriter的写入时间一致,但显然并非如此。

I guess you are reading a number of frames in 30 sec (the time you specified for your loop) and since these number of frames is not big enough to create a 30-second video a smaller one is created. 我猜您正在30秒(为循环指定的时间)内读取多个帧,并且由于这些帧的数量不足以创建30秒的视频,因此会创建较小的视频。 Roughly from your saying it would be you need 30*30 = 900 frames and you only got half of them let's say 450 frames. 粗略地说,这将需要30*30 = 900帧,而只有一半,即450帧。 So, when you pass them to the writer they correspond to a 15-sec video. 因此,当您将它们传递给编写器时,它们对应于15秒的视频。 Since on the other hand you provided a 30-sec span for recording they correspond to 30-sec real action and that's why you see an accelerated version of only 15 seconds. 另一方面,由于您提供了30秒的录制时间跨度,因此它们对应于30秒的真实动作,因此您只能看到15秒的加速版本。

My guess is you can try some things to accelerate your video capturing or your video writing since you are applying them sequentially (one after the other that is). 我的猜测是,您可以尝试一些方法来加快视频捕获或视频写作的速度,因为您是依次应用它们的(一个接一个)。 Your camera supports 30fps@1080p so I guess you can achieve your goal some how. 您的相机支持30fps @ 1080p,所以我想您可以通过某种方式实现目标。

My first thought is to just record your images for 30 seconds and then write them in your video. 我的第一个想法是只记录您的图像30秒钟,然后将其写入视频中。 This way you focus only in capturing the images at first. 这样,您只专注于首先捕获图像。 You can accomplish this by using a container like a list or a dict to hold your images. 您可以通过使用诸如列表或字典之类的容器来保存图像来完成此操作。 Then, after finishing the recording write them down. 然后,在完成录制后将它们写下来。 You can check the number you can catch in 30 seconds to see the actual fps for recording. 您可以检查30秒钟内可以捕捉到的数字,以查看录制的实际fps。 If you get a value of let's say 850 frames you can create a 30-sec video of 850/30 = 28.33 fps which does not make much difference in practice instead of 30fps to represent a video that is not accelerated. 如果您获得850帧的值,则可以创建30秒的850/30 = 28.33 fps的视频,这在实践中并没有太大区别,而不是30fps来代表未加速的视频。 Of course you should create a suitable VideoWriter in that case (pass a suitable 3rd parameter in constructor). 当然,在这种情况下,您应该创建合适的VideoWriter (在构造函数中传递合适的第3个参数)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM