繁体   English   中英

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

[英]Save edited video with 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的代码。 也许它不起作用。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM