zoukankan      html  css  js  c++  java
  • python cv2读取rtsp实时码流按时生成连续视频文件

    代码实现

    # coding: utf-8
    import datetime
    import  cv2
    import os
     
    ip = '192.168.3.160'.replace(".", "_") 
    rtsp = 'rtsp://admin:admin@192.168.3.160:554/1/1' 
    # 初始化摄像头
    cap = cv2.VideoCapture(rtsp)
    
    fourcc = cv2.VideoWriter_fourcc(*'XVID')
    size = (int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)), int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)))
    
    
    frame_count = 0
    while cap.isOpened():
        isSuccess, frame = cap.read()
        if isSuccess: 
            if frame_count % 120 == 0 or frame_count == 0:  
                frame_count = 0 
                i = datetime.datetime.now().strftime("%Y%m%d%H%M%S") 
                filename = str(i) + '-' + ip + '.avi'
                print(filename)
                video_writer = cv2.VideoWriter(filename, fourcc, 24, size)
                
            video_writer.write(frame)
            frame_count = frame_count + 1
            print(frame_count)
            if frame_count % 120 == 0:  
                video_writer.release()
                portion = os.path.splitext(filename)
                newname = portion[0] + '.mp4'
                os.rename(filename, newname)
            cv2.imshow('show', frame)    
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    cap.release()
    cv2.destroyAllWindows()        
    

    效果

    在这里插入图片描述

  • 相关阅读:
    前端资源网址
    IDEA激活工具
    新建jsp项目
    jsp笔记
    iOS的SVN
    iOS学习网站
    测试接口工具
    MVP模式
    关于RxJava防抖操作(转)
    注释模板
  • 原文地址:https://www.cnblogs.com/gmhappy/p/11863933.html
Copyright © 2011-2022 走看看