Extracting image from video at a given time using OpenCV

后端 未结 3 2098
深忆病人
深忆病人 2021-02-01 09:39

My task is to make a utility that can take a video and time in seconds.

The utility should write out jpeg images from the video with the given input.

E.g. let th

3条回答
  •  没有蜡笔的小新
    2021-02-01 09:52

    import cv2
    
    cap = cv2.VideoCapture('bunny.mp4')
    cap.set(cv2.CAP_PROP_POS_MSEC,1000)      # Go to the 1 sec. position
    ret,frame = cap.read()                   # Retrieves the frame at the specified second
    cv2.imwrite("image.jpg", frame)          # Saves the frame as an image
    cv2.imshow("Frame Name",frame)           # Displays the frame on screen
    cv2.waitKey()                            # Waits For Input
    

    Here, cap.set(cv2.CAP_PROP_POS_MSEC,1000) is responsible for skipping directly to the 1st second in the video (1000th millisecond). Feel free to substitute the value of your choice.

    I have tested the code on OpenCV 3.1.0.

提交回复
热议问题