简体   繁体   中英

Appending video frame to list using OpenCV take a lot of resources in RAM

I read a video of size 47 mb in disk using opencv, and save it to the disk. The size of resulting video is 25 mb. Meanwhile I create a list, where I append frames from videos. When converting this list to an array the size of array is 2449.51. Why is this so? May be there is some compression algorithm which reduced size while saving, can I apply those on my array?

def read_face(vid_path):
    cap = cv2.VideoCapture(vid_path) 
    fps = cap.get(cv2.CAP_PROP_FPS)
    width  = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH ))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT ))
    
    total_frames=[]
    fourcc = cv2.VideoWriter_fourcc(*"XVID")
    temp_name='outvide.mp4'
    out = cv2.VideoWriter(temp_name,fourcc, fps, (width,height))
    
    while(cap.isOpened()):  # Read until the video is completed
        ret, frame = cap.read()    # Capture frame by frame
        if ret:
                total_frames.append(frame)
                out.write(frame)
        else:
            break

    cap.release() 
    return np.array(total_frames)

This is how I calculate size of frame array in MBs. round(frames.nbytes / 1024 / 1024,2)

How can I reduce this size?

videos are saved in a compressed format. MP4, MOV etc are codecs that represent the compression format in addition to other information like audio etc.

If you would like to reduce the size of the frames-list, you can resize the frames before appending them.

if ret:
    cv2.resize(frame, 0.5, 0.5)
    total_frames.append(frame)

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