如何在特定时间以微秒为单位从具有cv2的视频中提取帧?

问题描述 投票:0回答:1

我有一些视频,需要抓取并存储为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()
python mp4 cv2
1个回答
0
投票

这听起来与我正在研究的内容相似。因此,对于此代码,只需在“ 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)
© www.soinside.com 2019 - 2024. All rights reserved.