Repository navigation
Expand file tree
/
Copy pathpyNvr.py
More file actions
152 lines (139 loc) · 4.76 KB
/
Copy pathpyNvr.py
File metadata and controls
152 lines (139 loc) · 4.76 KB
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# Copyright (C) 2022 wfxzf
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
import os
import cv2
import time
import threading
if True: #config
#config
##choose netdisk ( 1 for baidu netdisk ; 2 for ali netdisk )
netdisk = 1
##camera's name
camname='cam01'
##local file path
pwd='~//videos//'
##video stream url(see readme)
url='rtsp://baipiaoguai:xiaciyiding@192.168.3.42:8554/live'
##single video length(minute 1-1000)
blocktime=1
##up load to baidu netdisk? True or False.
uptoby = True
##remove after upload? True or False.
re_af_up = True
if True: #judge and pretreatment netdisk
if netdisk == 1:
from bypy import ByPy
elif netdisk == 2:
from aligo import Aligo
ali = Aligo()
if os.path.exists('config.conf'):
with open("config.conf", "r") as f:
floder_id = f.readline()
f.close()
else:
creat_res = ali.create_folder(name='IPcameras', parent_file_id='root')
creat_res = ali.create_folder(name='IPcameras', parent_file_id=creat_res.file_id)
floder_id = creat_res.file_id
with open("config.conf", "a") as f:
f.write(creat_res.file_id)
f.close()
else:
print('netdisk config error ,only for 1 or 2')
def bysync(file,path,i,re_af_up ):
if i >= 3:
print(file+"upload error,check the internet, netdisk account and path.")
return
time.sleep(10)
print(file+" uploading.......")
bp = ByPy()
code = bp.upload(file,'/'+path+'/')
if code == 0:
if re_af_up == True:
os.remove(file)
print(file+" successfuly uploaded!")
else:
i=i+1
print(file+"retry :"+str(i))
bysync(file,path,i,re_af_up)
def alisync(file,path,i,re_af_up):
if i >= 3:
print(file+"upload error,check the internet, netdisk account and path.")
return
time.sleep(10)
ali = Aligo()
code = ''
global floder_id
try:
code = ali.upload_files(file_paths=[file],parent_file_id=floder_id)
except Exception as e:
print(e)
i=i+1
print(file+"retry :"+str(i))
alisync(file,path,i,re_af_up)
if code != '':
if re_af_up == True:
os.remove(file)
print(file+" successfuly uploaded!")
else:
i=i+1
print(file+"retry :"+str(i))
alisync(file,path,i,re_af_up)
def capture(url,camname,pwd,blocktime,uptoby,re_af_up,netdisk):
try:
print("****")
cap = cv2.VideoCapture(url)
print("****")
except:
print("can not capture video stream, please check your url or internet.")
else:
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = int(cap.get(cv2.CAP_PROP_FPS))
if fps >= 30:
fps = 30
elif fps <=0:
fps = 15
print("fps:"+str(fps))
size = (int(int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))), int(int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))))
print("video size:"+str(size))
pwd = pwd + camname+'//'
if os.path.exists(pwd) == False:
try:
os.mkdir(pwd)
except:
print("please crate folder manually")
while True:
cu_pwd = pwd+str(time.strftime("%Y-%m-%d-%H-%M-%S",time.localtime()))+'.avi'
out = cv2.VideoWriter(cu_pwd, fourcc,fps, size)
ret,frame = cap.read()
start_time=int(time.time())
#brk = 0
while ret:
ret,frame = cap.read()
#cv2.imshow("frame",frame)
out.write(frame)
if (time.time()-start_time >= blocktime*30) or (not ret):
if uptoby == True and netdisk == 1:
sync = threading.Thread(target=bysync, args=(cu_pwd,camname,0,re_af_up))
sync.start()
if uptoby == True and netdisk == 2:
sync = threading.Thread(target=alisync, args=(cu_pwd,camname,0,re_af_up))
sync.start()
out.release()
break
#if cv2.waitKey(1) & 0xFF == ord('q'):
#brk = 1
#break
#if brk == 1:
#break
cv2.destroyAllWindows()
cap.release()
out.release()
if __name__ == '__main__':
capture(url,camname,pwd,blocktime,uptoby,re_af_up,netdisk)