使用 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()
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
当我手动设置编解码器而不是
-1
时,代码会为我创建文件,我从未见过使用
-1
的代码。也许这不起作用。Code creates file for me when I set manually codec instead of
-1
I never saw code with
-1
. Maybe it doesn't work.