我就废话不多说了,直接上代码吧!
# 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()
效果
以上这篇python cv2读取rtsp实时码流按时生成连续视频文件方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。