mrs3733
mrs3733

Reputation: 1

How to set time for capturing video using python opencv?

I have a code that capture video from camera from github https://gist.github.com/keithweaver/4b16d3f05456171c1af1f1300ebd0f12#file-save-video-w-opencv-py.

But how to set time limit to this capture?.I want to capture multiple videos continuously with duration say 3 minutes without any frame drops.

I m kind of new to programming can anyone help with the code. Thanks a lot

Upvotes: 0

Views: 15291

Answers (4)

W. E.
W. E.

Reputation: 11

OpenCV has that function already:

  start_time = 14  # in frame number 14
  end_time = 30  # in frame number 30
  # Set the starting and ending frame numbers
  start_frame = int(cap.get(cv2.CAP_PROP_FPS) * start_time)
  end_frame = int(cap.get(cv2.CAP_PROP_FPS) * end_time)

  # Iterate over the frames and write to the output file
  cap.set(cv2.CAP_PROP_POS_FRAMES, start_frame)
  while cap.get(cv2.CAP_PROP_POS_FRAMES) <= end_frame:

     # your code here...

We can also do it by time, but you have to change cv2.CAP_PROP_FPS TO cv2.CAP_PROP_MSEC. Then, we have to give it a boundary between time interval, which is more complicated. Here's time in milliseconds:

11811.8
11845.166666666668
11878.533333333333
11911.900000000001
11945.266666666666
11978.633333333335
12012.0
12045.366666666669
12078.733333333334
12112.1
12145.466666666667
12178.833333333334
12212.2

Optionally, convert it to integer. Otherwise, it would not take a snapshot.

11911
11945
11978
12012
take a snapshot at time: 12
12045
12078
12112
12145

Too much work.

Upvotes: 1

De Funct
De Funct

Reputation: 495

From what @Ali Yilmaz stated, that might be a bit outdated. It works this way on a 32-bit arm processor with Debian Buster with kernel 4.19.x and python3.

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

I think the only difference in my moviepy setup and Ali's set up was the fact that in two years things changed with moviepy distributions and installations.

Upvotes: 0

Ja_cpp
Ja_cpp

Reputation: 2334

You can do it like that:

  1. startTime = time.time()
  2. timeElapsed = startTime - time.time() in seconds
  3. secElapsed = int(timeElapsed)
  4. Stop the program when while(secElapsed < 100)

Example of the code, it should look like that:

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

Upvotes: 7

Ali Yılmaz
Ali Yılmaz

Reputation: 1695

You can also use moviepy. Set start and end durations in terms of seconds. Say, you want to capture subclip, starting from second minute, (e.g. start=120), you want to record for 5 minutes. (5minutes=300seconds). Here is how to do it:

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

Upvotes: 2

Related Questions