简体   繁体   中英

How to play a video file from a particular timstamp in opencv using python?

The user gives an input of start time and end time(timestamps) in a video. The video file should be played within that time parameter, and not the whole thing. Any help would be appreciated.

I have 2 hour long video file, i just want to play the video where key events took place.

The set() method of the VideoCapture class allows one to jump to a specific frame and start reading from that point.

cap = cv2.VideoCapture("file_name.avi")
cap.set(cv2.CAP_PROP_POS_FRAMES, index)

Would set the video reader to start reading at the frame specified by index , and this is zero based.

The get() method would then allow you to check what the current frame index is, cap.get(cv2.CAP_PROP_POS_FRAMES) would return the current frame address.

All that is left is to convert the timestamps into frame indices, and to loop between the start and end frame, checking whether you want to read the frame or stop reading. The get() method can also be used to read the total number of frames in a file, which will help with this.

Looking at the options for get() and set() , you could probably also use cv.CAP_PROP_POS_MSEC which is the current position in the video in milliseconds.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM