Simple content, just used

video_capture.set(cv2.CAP_PROP_POS_FRAMES, start_fps)

To play the video from the specified location.


Let me record it here.

Import cv2 import OS video_path = 'D:/test/ CCC /mp4/1.mp4' Exist_ok =True) start_second= 760 # set start time (in seconds) end_second= 790 # set end time (in seconds) video_capture = v2.videocapture (video_path) Print (FPS) start_fps = start_second* FPS # start frame = start time * frame rate print(FPS) start_fps = start_second* FPS # End_fps = end_second* FPS # end frame = end time * frame rate Video_Capture. set(CV2.cap_prop_pos_frames, K = start_fps while True: success, frame = video_capture.read() if success: If k % (FPS *2) == 0 if k % (FPS *2) == 0 if k % (FPS *2) == 0 FPS m, s = divmod(seconds, 60) h, m = divmod(m, 60) print(" print ") 02 02%2 d: % d: % d "% (h, m, s)) time =" % % % d_ 02 d_ 02 d "% (h, m, s) # colon can't appear in the image save path, Cv2.imwrite (picSavePath + time+'.jpg', frame) if k > end_fps: Break print('end') video_capture.release()

If you want to edit more than one clip, just add a loop in the outermost layer

Import cv2 import OS video_path = 'D:/test/ CCC /mp4/1.mp4' Exist_ok =True) start_second= [760,800] # end_second= [790,830] # end_second= [790,830] # exist_second *3 Print (FPS) print(FPS) print(FPS) print(FPS) print(FPS) print(FPS) Start_fps = [I * FPS for I in start_second] # start_fps = [I * FPS for I in start_second] # end frame = end time * frame rate # Note: For I in range(len(start_fps)) for I in range(len(start_fps)): Video_capture.set (CV2.cap_prop_pos_frames, start_fps[I]) # Set the position of the video to read from k = start_fps[I] Frame = video_capture. Read () if success: k += 1 if k <= end_fps[I]: FPS m, s = divmod(seconds, 60) h, m = divmod(m, 60) print(" print ") %2d:%02d:%02d" % (h, m, s)) time = "%d_%02d_%02d" % (h, m, s) # Cv2.imwrite (picSavePath + time+'.jpg', frame) if k > end_fps[I]: Break print(% (I +1)) video_capture.release()