How to save a video in Python OpenCV

烈酒焚心 提交于 2021-02-17 03:35:36

问题


I have opened a Video using CV2, made some changes using cv2.rectangle.

Now, when I do cv2.imshow('frame',frame), it plays the video.

Instead of this, I want to save the video somewhere, in the original size and frame rate.


回答1:


You can save video frame by frame. Based on example on docs:

Open Cv video capture


import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()



来源:https://stackoverflow.com/questions/57216693/how-to-save-a-video-in-python-opencv

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!