【问题标题】:How to set time for capturing video using python opencv?如何设置使用 python opencv 捕获视频的时间?
【发布时间】:2018-10-30 06:02:03
【问题描述】:

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

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

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

【问题讨论】:

    标签: python opencv


    【解决方案1】:

    根据@Ali Yilmaz 的说法,这可能有点过时了。它在带有 Debian Buster 内核 4.19.x 和 python3 的 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 的分发和安装发生了变化。

    【讨论】:

      【解决方案2】:

      你可以这样做:

      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()
      

      【讨论】:

        【解决方案3】:

        您也可以使用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")
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2017-07-02
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-04-29
          • 2016-01-09
          • 1970-01-01
          相关资源
          最近更新 更多