目录
每隔3秒截取一张图片:
python
import cv2
import time
import os
def capture_rtsp_frames(rtsp_url, output_dir="captures", interval=3):
# 创建输出目录
os.makedirs(output_dir, exist_ok=True)
# 打开RTSP流
cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
if not cap.isOpened():
print("无法打开RTSP流")
return
# 设置RTSP传输协议为TCP
cap.set(cv2.CAP_PROP_BUFFERSIZE, 1) # 减少缓冲
frame_count = 0
last_capture_time = time.time()
print(f"开始捕获,间隔{interval}秒...")
print("按 Ctrl+C 停止捕获")
try:
while True:
ret, frame = cap.read()
if not ret:
print("无法读取帧,尝试重新连接...")
cap.release()
time.sleep(1)
cap = cv2.VideoCapture(rtsp_url, cv2.CAP_FFMPEG)
continue
current_time = time.time()
# 检查是否到达捕获间隔
if current_time - last_capture_time >= interval:
# 保存图片
timestamp = time.strftime("%Y%m%d_%H%M%S")
filename = f"{output_dir}/capture_{timestamp}_{frame_count}.jpg"
cv2.imwrite(filename, frame)
print(f"捕获: {filename}")
frame_count += 1
last_capture_time = current_time
# 添加小延时避免CPU占用过高
time.sleep(0.01)
except KeyboardInterrupt:
print("\n用户中断捕获")
finally:
cap.release()
print(f"捕获完成,共保存 {frame_count} 张图片")
if __name__ == "__main__":
# RTSP配置
rtsp_url = "rtsps://bblp:xxx0@192.168.8.201/streaming/live/1"
# 开始捕获(无限循环,直到按Ctrl+C)
capture_rtsp_frames(rtsp_url, interval=3)
截图命令:
ffmpeg -rtsp_transport tcp -i "rtsps://bblp:xxxx@192.168.8.201/streaming/live/1" -y -frames:v 1 capture1.jpg