使用python CV2保存编辑后的视频



我正在使用以下代码创建一个所有帧中都有矩形的视频。但是,视频在创建后不会保存。如何编辑代码以将视频保存在我的一个文件夹中。

import cv2
#Reads the video and collects it information
cap = cv2.VideoCapture('20150326_060700_062957_themis_rank_fisheye.mp4')
fps = cap.get(cv2.CAP_PROP_FPS)
width  = cap.get(cv2.CAP_PROP_FRAME_WIDTH)   # float
height = cap.get(cv2.CAP_PROP_FRAME_HEIGHT)  # float
output = cv2.VideoWriter("output.mp4", -1, fps,(int(width),int(height)))
while (cap.isOpened()):
ret, frame = cap.read()
if (ret):
# Adds the rectangles in all frames
rect1 = cv2.rectangle(frame, (135, 510), (200,450), (255, 0, 0), 1)
rect2 = cv2.rectangle(frame, (365, 365), (430, 430), (255, 0, 0),1)

# writing the new frame in output
output.write(frame)
cv2.imshow("output", frame)
if cv2.waitKey(1) & 0xFF == ord('s'):
break
else:
break
cv2.destroyAllWindows()  
output.release()  
cap.release()

当我手动设置编解码器而不是-1时,代码会为我创建文件

#fourcc = -1
fourcc = cv2.VideoWriter_fourcc(*'MP4V')
output = cv2.VideoWriter("output.mp4", fourcc, fps,(int(width),int(height)))

我从未见过-1的代码。也许它不起作用。

最新更新