【发布时间】:2020-06-26 19:06:46
【问题描述】:
我尝试从视频中获取第一帧、中间帧和最后一帧,但只能获取第一帧和中间帧。
这是我的代码:
import cv2
import math
vidcap = cv2.VideoCapture("/home/data/input/video.MOV")
# Get number of frame of the video
length = int(vidcap.get(cv2.CAP_PROP_FRAME_COUNT))
print(length)
# Define a tuple of frame need to get
frame = (0, math.ceil(length/2), length - 1)
# Get the frame
for f in frame:
vidcap.set(1, f)
_, image = vidcap.read()
cv2.imwrite("/home/data/output/frame%d.jpg" % f, image)
它确实保存了最后一帧图像,但它没有任何数据。
我的代码有什么问题吗?
更新:
根据这个post,似乎cv2.CAP_PROP_FRAME_COUNT 可以计算错误的帧数。
那么有没有更好的方法来获取帧数而不读取整个帧?
【问题讨论】: