Python_OpenCV video capture and save

Prior to image processing, the data we need to get our hands were screened for video, we need what we need from the interception of a few paragraphs or

The whole idea is relatively simple, a video taken by setting the start and end times (frames), the image can be saved as a new video period

Directly on the code:

" '" 
[13] intercept the video function name 
Parameters input parameters of the video file name 
[Details] enter different time interception splicing 
[creation date] 20191128 by wangxioabei 
[Date Modified] NOTE.1: 
"" " 
DEF CutVideoFromFile ( video_file_name, windows_name = ' videoShowing ' ): 
    CAP = cv2.VideoCapture (video_file_name)   # open the video file 
    # need to clear the saved video format 
    fourcc = cv2.VideoWriter_fourcc (* ' XVID ' ) 
    FPS = cap.get (cv2.CAP_PROP_FPS)
     Print ( " frame rate:% D ' % FPS) 
    size =(int (cap.get (cv2.CAP_PROP_FRAME_WIDTH)), int (cap.get (cv2.CAP_PROP_FRAME_HEIGHT))) 
    OUT = cv2.VideoWriter ( ' BeltTear.avi ' , the fourcc, FPS, size) 
    SAVETIME = [[60 * 38 is * 60 + 59 + 38, 39], [54, 44 * 42 * 60 + 60 + 11], [47 * 8, 48 * 60 + 60 + 24], [51 * 60 * 60 + 39 + 20.52 ]]
     Print (SAVETIME [0] [0]) 
    now_frame = 0
     the while (cap.isOpened ()): 
        RET, frame = cap.read ()   # capture an image 
        img_h, img_w, img_ch = frame.shape
         # Print ( frame.shape) 
        IF RET:
             # [1] can not be directly saved binary or gray-scale images into video, need to be converted into color
            if img_ch==1:
                frame = cv2.cvtColor(frame, cv2.COLOR_GRAY2BGR)
            # cv2.imshow(windows_name, frame)
            for i in range(len(SaveTime)):
                if now_frame > SaveTime[i][0]*fps and now_frame < SaveTime[i][1]*fps:
                    out.write(frame)
                    print(now_frame)
            now_frame += 1
            if now_frame > SaveTime[2][1]*fps:
                break;
            k = cv2.waitKey(1) & 0xFF
            if k == 27:
                break
            # cv2.waitKey(25)
        else:
            break
    cap.release()
    out.release()
    cv2.destroyAllWindows()

Wherein SaveTime nested list is a list of video time taken s, which also can be passed as a parameter, to encapsulate the entire function.

Guess you like

Origin www.cnblogs.com/wangxiaobei2019/p/11951708.html
Recommended