Video reading and writing in OpenCV

Hits: 0

read video

import  cv2 as cv 

# Video read and write 
In OpenCV, we want to read a video, we need to create a VideoCapture object, specify the video file you want to read

1. Create an object to read the video 
    cap = cv.VideoCapture(filepath) 
    parameter :
        filepath : video file path (can also be specified as a camera)

2. Attribute information of the 
    video 2.1. Get some attributes of the video 
        retval = cap.get(proId) 
    parameter :
        proId : a number from 0 to 18, each number represents the properties of the video. 
        Common properties are :
            Index       flags Meaning 
            0        cv.CAP_PROP_POS_MSEC The current position of the video file 
            1        cv.CAP_PROP_POS_FRAMES Index frame from 0, frame position 
            2        cv.CAP_PROP_POSAVI_RATRIO The relative position of the video file (0 means start, 1 means end) 
            3        cv.CAP_PROP_FRAME_WIDTH The frame of the video stream Width4 
            cv.CAP_PROP_FRAME_HEIGHT Frame height of video stream 5        cv.CAP_PROP_FPS Frame rate6 cv.CAP_PROP_FOURCC Codec four character code 7 cv.CAP_PROP_FRAME_COUNT Frame of video file

    2.2 Modify the video attribute information 
        cap.set(proid, value) 
        parameter :
            proid : the index of the attribute, corresponding to the above 
            value : the modified attribute value

3. When judging the image, the read is successful 
    isornot = cap.isOpened() 
    parameter :
        Returns True if the read is successful, otherwise False

4. Get each frame of video 
    ret, frame = 
    parameter :
        ret : Return True if the acquisition is successful, otherwise False 
        frame : A certain frame of image acquired

5. Call cv.imshow() to display the image, and use cv.waitkey() to set the appropriate duration when displaying the image. If it is too low, the 
  video will play very fast, and if it is too high, it will play very slowly, usually set to 25ms

6. Finally, call cap.release() to release the video


#Create an object to read the video 
cap = cv.VideoCapture('video/demo.mp4') #Read  the camera #Determine 
whether the reading is successful

while  cap.isOpened(): #Get 
    each frame of image 
    ret,frame = #If 

    the acquisition is successful, display the image 
    if  ret: 
        cv.imshow('frame',frame) #The 

    interval between each frame is 25ms or Press q to exit 
    if  cv.waitKey(25) & 0xFF == ord('q'): 
        break #Release the 

video object 

2. Write the video

import cv2 as cv

#video save 
Saving video in OpenCV uses the VideoWriter object, where you specify the name of the output file, like this:
1. Create a video write object
        filename: The location where the video is saved
        fourcc: 4-byte code specifying the video codec
        fps: frame rate
        frameSize: frame size

        Setting the codec for the video looks like this:
                c1,c2,c3,c4: are 4-byte codes for video codecs, find the list of available codes at
                closely related to the platform
            In windows: DIVX (.avi)
            In OS: MJPG(.mp4), DIVX(.avi), X264(.mkv)

        .Use to get each frame of image in the video, and use out.write() to write a frame of image into the video
        .Use cap.release() and out.release() to release resources


#Create video read object
cap=cv.VideoCapture() 'video/demo.mp4' ) #Open the video

#Get the width and height of the video and convert from integer 
frame_width=int(cap.get( 3 ))

#Create video writing object 
fourcc=cv.VideoWriter_fourcc( 'M' , 'J' , 'P' , 'G' ) #Encoding format 
out=cv.VideoWriter( 'video/save_test.mp4' ,fourcc, 25 ,( frame_width,frame_height))

#Determine whether the opening is successful 
while cap.isOpened():

    #read each frame of image

    #Determine whether the read is successful 
    if ret: #Write
         each frame of image
        # cv.imshow('frame',frame)

    # Delay 25ms display (that is, the frame rate is set to 25ms) 
    # cv.waitKey(25)

# release resources

More content will be updated simultaneously in the personal blog system: Video reading and writing in OpenCV

You may also like...

Leave a Reply

Your email address will not be published.