繁体   English   中英

如何使用python opencv设置捕获视频的时间?

[英]How to set time for capturing video using python opencv?

我有一个代码可以从 github https://gist.github.com/keithweaver/4b16d3f05456171c1af1f1300ebd0f12#file-save-video-w-opencv-py 的摄像头捕获视频。

但是如何设置此捕获的时间限制?。我想连续捕获多个视频,持续时间为 3 分钟,没有任何丢帧。

我对编程有点陌生,任何人都可以帮助编写代码。 非常感谢

你可以这样做:

  1. startTime = time.time()
  2. timeElapsed = startTime - time.time()以秒为单位
  3. secElapsed = int(timeElapsed)
  4. while(secElapsed < 100)停止程序

代码示例,它应该是这样的:

import numpy as np
import cv2
import time

# The duration in seconds of the video captured
capture_duration = 10

cap = cv2.VideoCapture(0)

fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.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)
        out.write(frame)
         cv2.imshow('frame',frame)
    else:
        break

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

您也可以使用moviepy 以秒为单位设置startend持续时间。 比如说,你想捕捉子剪辑,从第二分钟开始,(例如start=120),你想记录5分钟。 (5 分钟 = 300 秒)。 这是如何做到的:

from moviepy import VideoFileClip

clip = VideoFileClip("/path/to/video.mp4")
starting_point = 120  # start at second minute
end_point = 420  # record for 300 seconds (120+300)
subclip = clip.subclip(starting_point, end_point)
subclip.write_videofile("/path/to/new/video.mp4")

从@Ali Yilmaz 所说的来看,这可能有点过时了。 它在带有内核 4.19.x 和 python3 的 Debian Buster 的 32 位 arm 处理器上以这种方式工作。

from moviepy.editor import VideoFileClip

clip = VideoFileClip("/path/to/yourfile.mp4")

start = 10 # start at 10 seconds
end   = 25 # plays for 15 seconds and ends at 25 seconds

subclip = clip.subclip(start, end)
subclip.write_videofile("/path/to/yournewfile.mp4")

我认为我的 moviepy 设置和 Ali 设置的唯一区别是,在两年内,moviepy 发行版和安装发生了变化。

暂无
暂无

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

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