简体   繁体   English

Python OpenCV视频录制快速转发

[英]Python OpenCV video recording fast-forwarding

I've made a short script that is the beginings of a rubik's cube timer, but when I play back the recorded video it is extremely sped up. 我制作了一个简短的脚本,它是魔方计时器的开端,但是当我播放录制的视频时,它的速度非常快。

My webcam is a 'Creative Labs Live! 我的网络摄像头是“ Creative Labs Live! Cam Chat HD' and has a resolution of 1280*720 pixels, Side note; Cam Chat HD” ,分辨率为1280 * 720像素,旁注; when I try and run the script encoding in that resolution playback fails, and a framerate of 30 fps. 当我尝试以该分辨率运行脚本编码时,播放失败,并且帧速率为30 fps。

Below is the code and a link of a video demonstrating the effect. 下面是演示效果的代码和视频链接。

from time import clock as t
import time
import cv2
import numpy as np
cap = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*"XVID")
out = cv2.VideoWriter("output.avi", fourcc, 30.0, (640, 480))
timing = False

while True:
    ret, frame = cap.read()
    #frame = cv2.flip(frame, 1)
    cv2.imshow("frame", frame)

    if timing:
        out.write(frame)

    if cv2.waitKey(1) & 0xFF == ord(" "):
        if timing:
            after = t()
            break
        else:
            timing = True
            start = t()

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

print(after - start)

Example of the accelerated video. 加速视频的示例。

You are writing output at 30 fps,and your webcam possibly not recording at 30 fps. 您正在以30 fps的速度写入输出,并且您的网络摄像头可能无法以30 fps的速度录制。 Thats why your video is fast. 这就是为什么您的视频播放速度快的原因。 Find proper fps and output will be normal. 找到适当的fps,输出将是正常的。

Find framerate like fps=cap.get(cv2.CAP_PROP_FPS) for opencv >=3 查找opencv> = 3的帧速率,如fps = cap.get(cv2.CAP_PROP_FPS)

I encountered the same issues. 我遇到了同样的问题。 I think it was caused by the write action. 我认为这是由写操作引起的。 You tried to save the video at 30fps, but the write action can't handle that. 您尝试将视频保存为30fps,但是write操作无法处理。 For example, you want to save 180frame at 30fps, that video should be 6 seconds. 例如,您要以30fps保存180帧,那么视频应为6秒。 But the write action can only save 10 frames per second, so 20 frames will be dropped per second. 但是写操作每秒只能保存10帧,因此每秒将丢失20帧。 But the video still played at 30fps, so 6 seconds video becomes 2 seconds. 但是视频仍以30fps的速度播放,因此6秒的视频变为2秒。 Seems faster. 似乎更快。

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

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