python 视频存储

    cap = cv2.VideoCapture("./host1.mp4") 

'''视频保存设置'''
    out = 'inference/video'    #视频保存目录
    p = time.strftime('%Y-%m-%d-%H_%M_%S', time.localtime(time.time()))
    save_path = str(Path(out) / Path(p))
    print("save_path:", save_path)

    vid_path, vid_writer = None, None
    Is_recover = True   # 是否复原图像
    save_vid = True     # 是否保存检测后的图像
    show_video = False   # 是否显示图像


    org_fps = cap.get(cv2.CAP_PROP_FPS)
    org_w = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    org_h = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))

    while True:
        ret, frame = cap.read()  # 读取帧

        if save_vid:
            if vid_path != save_path: 
                vid_path = save_path
                if isinstance(vid_writer, cv2.VideoWriter):
                    vid_writer.release()  # release previous video writer

                if Is_recover:  # video
                    fps = org_fps
                    print("fps",fps)
                    w = org_w
                    h = org_h
                    print("w h", w, h)

                else:  # stream
                    fps, w, h = 60, 640, 480
                    print("w h", w, h)

                vid_writer = cv2.VideoWriter(save_path+'.mp4', cv2.VideoWriter_fourcc(*'mp4v'), fps, (w, h))

            vid_writer.write(frame_copy if Is_recover else frame)

            if cv2.waitKey(1) & 0xFF == ord('q'):  # 按‘q’退出 也可以更换别的结束条件
                vid_writer.release()  # release previous video writer
                cv2.destroyAllWindows()
                break
     
    cap.release()
    

每次运行程序,保存的视频名字以本地时间为准
在这里插入图片描述
需要注意的是如果保存的视频无法打卡,一般是
vid_writer.release() # release previous video writer
没有正确运行,导致视频无法打开,只要运行了上面这句,就可以打开视频

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值