【问题标题】:Save edited video with python CV2使用 python CV2 保存编辑后的视频
【发布时间】:2022-07-03 16:32:39
【问题描述】:

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

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

【问题讨论】:

  • 如果文件给出的图像与width, height 不同,那么您要保存,那么您必须在保存前resize()
  • 您是否收到错误或空文件?我们无法读懂您的想法 - 您必须添加所有有问题的详细信息(而不是在 cmets 中)
  • 首先放入缩进正确的代码,因为缩进错误的代码是没有用的。我们无法运行它,也许你所有的问题都是一些错误的缩进 - 但我们看不到它。
  • 您可能需要手动设置编解码器,而不是 -1。如果我使用-1,代码不会为我创建文件,但是当我使用cv2.VideoWriter_fourcc(*'MP4V')时我会得到文件

标签: python opencv video


【解决方案1】:

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

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

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

【讨论】:

  • 太棒了!这确实有效。谢谢!
猜你喜欢
  • 1970-01-01
  • 2022-06-14
  • 2018-11-26
  • 1970-01-01
  • 2020-07-07
  • 1970-01-01
  • 2018-01-18
  • 1970-01-01
  • 2020-08-04
相关资源
最近更新 更多