背景
很多人都会买家用监控摄像头,比如小米摄像头。一般费用由两方面,一个是买摄像头的硬件成本,比如小米的也就100-200左右;另一方面是存储的服务费,如果你想看7天前的录像,或者看一个月前的录像,这个云服务是收费的,我刚好有一台台式机,我就把摄像头的视频取出来存储在本地
环境
摄像头:水星
操作系统:ubuntu18.04
过程
参考水星官方获取流地址的指导教程:https://security.tp-link.com.cn/service/detail_article_4432.html
代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33import cv2
import os
import shutil
from datetime import datetime,timedelta
if __name__ == '__main__':
save_root = "your path"
rtsp = "rtsp://admin:123456@xxx.xxx.xxx.xxx:554/stream1"
vid = cv2.VideoCapture(rtsp)
fps = vid.get(5)
flag = 0
TIME_DALTA = 7
while True:
time_now = datetime.now()
N_days_ago = time_now + timedelta(days=-TIME_DALTA)
year_month_day = str(time_now.year) + "-" + str(time_now.month) + "-" + str(time_now.day)
year_month_day_N = str(N_days_ago.year) + "-" + str(N_days_ago.month) + "-" + str(N_days_ago.day)
hour_min_sec = str(time_now.hour) + "-" + str(time_now.minute) + "-" + str(time_now.second)
root_dir = os.path.join(save_root, year_month_day)
root_dir_N = os.path.join(save_root, year_month_day_N)
if os.path.isdir(root_dir_N):
shutil.rmtree(root_dir_N,True)
save_path = os.path.join(root_dir, hour_min_sec) + ".jpg"
if not os.path.exists(root_dir):
os.mkdir(root_dir)
try:
ret, frame = vid.read()
if flag == fps:
cv2.imwrite(save_path, frame)
flag = 0
flag += 1
except:
print("无法读取图像")https://github.com/qingzhouzhen/video_save/blob/master/save_video_from_rtsp.py
启动
1
nohup python save_video_from_rtsp.py > save_log.txt 2>&1 &