如何在 Python OpenCV 中保存视频 [英] How to save a video in Python OpenCV
本文介绍了如何在 Python OpenCV 中保存视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我使用 CV2 打开了一个视频,使用 cv2.rectangle
做了一些更改.
现在,当我执行 cv2.imshow('frame',frame)
时,它会播放视频.
我想将视频以原始大小和帧速率保存在某处,而不是这样.
解决方案
您可以逐帧保存视频.基于文档示例:
<预><代码>将 numpy 导入为 np导入 cv2帽 = cv2.VideoCapture(0)# 定义编解码器并创建 VideoWriter 对象Fourcc = cv2.VideoWriter_fourcc(*'XVID')out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))而(cap.isOpened()):ret, frame = cap.read()如果 ret==True:框架 = cv2.flip(frame,0)# 写入翻转的帧写出(帧)cv2.imshow('frame',frame)如果 cv2.waitKey(1) &0xFF == ord('q'):休息别的:休息# 如果作业完成,则释放所有内容cap.release()out.release()cv2.destroyAllWindows()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.
解决方案
You can save video frame by frame. Based on example on docs:
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()
这篇关于如何在 Python OpenCV 中保存视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文