python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > python cv2 rtsp生成连续视频

python cv2读取rtsp实时码流按时生成连续视频文件方式

作者:gmHappy

今天小编就为大家分享一篇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() 

效果

以上这篇python cv2读取rtsp实时码流按时生成连续视频文件方式就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文