我有一些视频,需要抓取并存储为png图像。我只需要特定时间的帧。这些时间以微秒为单位。如何只抓取这些框架?
ret, frame = cap.read()
cv2.imshow("Video", frame)
cap = cv2.VideoCapture("video.mp4")
count = 0
while vidcap.isOpened():
if count == int(308608300 / 1000000):
cv2.imwrite(os.path.join(path_output_dir, '%d.png') % count)
cv2.destroyAllWindows()
这听起来与我正在研究的内容相似。因此,对于此代码,只需在“ frameRate”处更改数字即可。
import cv2
vidcap = cv2.VideoCapture('video.mp4')
def getFrame(sec):
vidcap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
hasFrames,image = vidcap.read()
if hasFrames:
cv2.imwrite("folder/"+str(sec)+" sec.png", image) # save frame as PNG file
return hasFrames
sec = 0
frameRate = 0.25#it will capture image in each 0.25 second
success = getFrame(sec)
while success:
sec = sec + frameRate
sec = round(sec, 2)
success = getFrame(sec)