16

My task is to make a utility that can take a video and time in seconds.

The utility should write out jpeg images from the video with the given input.

E.g. let the video name be abc.mpeg and time be supplied to the tool as 20 seconds. The utility should write out image from video @ 20th second.

    # Import the necessary packages
    import argparse
    import cv2

    vidcap = cv2.VideoCapture('Wildlife.mp4')
    success,image = vidcap.read()
    count = 0;
    while success:
      success,image = vidcap.read()
      cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
      if cv2.waitKey(10) == 27:                     # exit if Escape is hit
          break
      count += 1

The above code gives all frames of the entire video, my concern is how can I pass time and get the frame at the specified time?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
venpo045
  • 303
  • 1
  • 2
  • 7
  • 6
    in C++ you would do `vidcap.set(CV_CAP_PROP_POS_MSEC, 20000);` to set the the capturing to position 20 seconds = 20000 milliseconds. In python it will probably look very similar. You can use `vidcap.get(CV_CAP_PROP_POS_MSEC)` to ask for the current position if you want to stop after some given time. – Micka Dec 15 '14 at 10:33

3 Answers3

24

why don't you just do, what @micka proposed ?

import cv2

vidcap = cv2.VideoCapture('d:/video/keep/Le Sang Des Betes.mp4')
vidcap.set(cv2.CAP_PROP_POS_MSEC,20000)      # just cue to 20 sec. position
success,image = vidcap.read()
if success:
    cv2.imwrite("frame20sec.jpg", image)     # save frame as JPEG file
    cv2.imshow("20sec",image)
    cv2.waitKey()                    
berak
  • 39,159
  • 9
  • 91
  • 89
  • 1
    thanks now i understood. Also we should use vidcap.set(0,20000) in place of CAP_PROP_POS_MSEC – venpo045 Dec 15 '14 at 19:28
  • ^^ ah, sorry, tried on 3.0, where the constants are more cosistent. it's probably cv2.cv.CAP_PROP_POS_MSEC with opencv2.4 – berak Dec 15 '14 at 19:33
1
# Import the necessary packages
import cv2

vidcap = cv2.VideoCapture('Wildlife.mp4')
success,image = vidcap.read()
print success
#cv2.imwrite("frame.jpg", image) 

count = 0
framerate = vidcap.get(5)
print "framerate:", framerate
framecount = vidcap.get(7)
print "framecount:", framecount
vidcap.set(5,1)
newframerate = vidcap.get(5)
print "newframerate:", newframerate  

while success:
  success,image = vidcap.read()
  #cv2.imwrite("frame%d.jpg" % count, image) 

  getvalue = vidcap.get(0)
  print getvalue
  if getvalue == 20000:
    cv2.imwrite("frame%d.jpg" % getvalue, image)  

  #if cv2.waitKey(10) == 27:                     
      #break
  count += 1

The output is as follows

framerate: 29.97002997
framecount: 901.0
newframerate: 29.97002997

Why frame rate is not changing.I want to change frame rate to 1 so that whatever time value user gives i should be able to get image frame.

venpo045
  • 303
  • 1
  • 2
  • 7
0
import cv2

cap = cv2.VideoCapture('bunny.mp4')
cap.set(cv2.CAP_PROP_POS_MSEC,1000)      # Go to the 1 sec. position
ret,frame = cap.read()                   # Retrieves the frame at the specified second
cv2.imwrite("image.jpg", frame)          # Saves the frame as an image
cv2.imshow("Frame Name",frame)           # Displays the frame on screen
cv2.waitKey()                            # Waits For Input

Here, cap.set(cv2.CAP_PROP_POS_MSEC,1000) is responsible for skipping directly to the 1st second in the video (1000th millisecond). Feel free to substitute the value of your choice.

I have tested the code on OpenCV 3.1.0.

John
  • 2,445
  • 2
  • 17
  • 25