每隔10秒钟从视频文件中捕获一帧

时间:2016-09-21 16:50:16

标签: python video video-capture

我希望每10秒钟从一个视频文件中捕获一帧,所以如果有人能帮助我,我会非常感激。我的python代码是这样的:

import cv2


print(cv2.__version__)
vidcap = cv2.VideoCapture('Standoff.avi')
vidcap.set(cv2.CAP_PROP_POS_MSEC,96000)  
success,image = vidcap.read()
count = 0
success = True
while success:
  success,image = vidcap.read()
  print 'Read a new frame: ', success
  cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
  cv2.waitKey(200)
  count += 1

3 个答案:

答案 0 :(得分:3)

如果您可以从文件中获取视频的帧速率,则以下内容应该有效(您可能需要检查语法,因为我还没有测试过):

import cv2

cap = cv2.VideoCapture('Standoff.avi')
framerate = cap.get(cv2.cv.CV_CAP_PROP_FPS)
framecount = 0

while(True):
    # Capture frame-by-frame
    success, image = cap.read()
    framecount += 1

    # Check if this is the frame closest to 10 seconds
    if framecount == (framerate * 10)
      framecount = 0
      cv2.imshow('image',image)

    # Check end of video
    if cv2.waitKey(1) & 0xFF == ord('q'):
          break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

答案 1 :(得分:2)

这里我在10帧转换后捕获帧。你可以使用时间函数,类似地在if条件语句中捕获帧

import cv2

vidcap = cv2.VideoCapture('testing.mp4')
count = 0
success = True
fps = int(vidcap.get(cv2.CAP_PROP_FPS))

while success:
    success,image = vidcap.read()
    print('read a new frame:',success)
    if count%(10*fps) == 0 :
         cv2.imwrite('frame%d.jpg'%count,image)
         print('successfully written 10th frame')
    count+=1

答案 2 :(得分:0)

您应该使用此代码。代码获取fps并将其乘以10,这定义了多少帧可以创建10秒的视频。让我们称之为乘数。脚本捕获第一帧,然后将帧计数器乘以乘法器。脚本每10秒捕获一次帧。

@mixin