从python视频中获取特定的帧序列

时间:2019-02-19 11:21:37

标签: python opencv ffmpeg opencv3.0

例如,我需要从视频中提取特定的帧序列

例如,每10帧提取一次(frame_1,frame_10,frame_20,...)。我是

使用下面的代码,但它提取所有帧,知道如何执行此操作吗?

import cv2
vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:

   cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
   success,image = vidcap.read()

count += 1

2 个答案:

答案 0 :(得分:1)

使用modulo的帧数:

import cv2
vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
success,image = vidcap.read()
count = 0
frame = 0 #after first frame read, so frame 0 will be saved, next every 10th
success = True
while success:

   if frame % 10 == 0:
       cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
       count += 1
   success,image = vidcap.read()
   frame += 1

答案 1 :(得分:1)

只需添加另一种解决方案,您就可以使用set函数和CAP_PROP_POS_FRAMES属性ID来获得10帧,如下所示:

import cv2
vidcap = cv2.VideoCapture('big_buck_bunny_720p_5mb.mp4')
success,image = vidcap.read()
count = 0
success = True
while success:
   cv2.imwrite("frame%d.jpg" % count, image)     # save frame as JPEG file
   success,image = vidcap.read()
   count += 1
   vidcap.set(cv2.CAP_PROP_POS_FRAMES, count * 10 )

与其他答案相比,这可能会给您带来性能上的优势,因为它不会读取每一帧,但是,如果它是来自摄像机的视频,那么我认为此解决方案将无法执行任何操作,而另一解决方案则可以。

>