Pyhon cv2.VideoWriter 保存视频

本文代码主要是在对视频处理后(例如在图片上显示检测结果、添加某些文字等),想按原尺寸、帧率保存视频;也可以对批量处理后的图片进行自定义帧率的视频保存,方便查看结果。

import cv2

# open your target video
video = cv2.VideoCapture('/home/yasin/test_video.avi')
print("Loaded video ...")

# get size and fps of video
width = int(video.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(video.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = video.get(cv2.CAP_PROP_FPS)
fourcc = cv2.VideoWriter_fourcc('M', 'P', '4', '2')

# create VideoWriter for saving
outVideo = cv2.VideoWriter('save_test_video.avi', fourcc, fps, (width, height))

cnt = 0
while (True):

    ret, frame = video.read()
    if not ret:
        print("... end of video file reached")
        break

    # here is your own frame processing code, for example, put word "This is a test frame" on the erery frame
    cv2.putText(frame, 'This is a test frame', (int(width / 20), int(height / 8)),cv2.FONT_HERSHEY_SIMPLEX, 6, (0, 255, 0), 20, cv2.LINE_AA)
    cv2.namedWindow('frame', 0)
    cv2.imshow('frame', frame)
    cv2.waitKey(2)

    # write the frame after processing
    outVideo.write(frame)
    cnt = cnt + 1
  • 9
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 9
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南洲.

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值