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

发布于 2025-01-15 21:11:40 字数 961 浏览 0 评论 0原文

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

 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()

I'm using the following code to create a video with rectangles in all its frames. However, the video is not being saved after it is created. How can I edit the code to have the video save in one of my folders.

 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()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_失温 2025-01-22 21:11:40

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

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

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

Code creates file for me when I set manually codec instead of -1

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

I never saw code with -1. Maybe it doesn't work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文