Repository navigation
Expand file tree
/
Copy pathdownload_storage_for_camera_id.py
More file actions
110 lines (93 loc) · 3.03 KB
/
Copy pathdownload_storage_for_camera_id.py
File metadata and controls
110 lines (93 loc) · 3.03 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
import argparse
import requests
import json
import os
from urllib.parse import urlparse, unquote
def request_storage_api(args):
api_endpoint = f"https://{args.endpoint}"
path = "/api/v2/storage/data/"
headers = {
"Authorization": f"LKey {args.token}"
}
# print(f"Requesting storage API: {api_endpoint}{path}")
# print(f"Camera ID: {args.camera_id}, Start: {args.start}, End: {args.end}")
# print(f"Headers: {headers}")
response = requests.get(
url=f"{api_endpoint}{path}",
headers=headers,
params={
"camid": args.camera_id,
"start": args.start,
"end": args.end,
"order_by": "time",
},
timeout=30,
)
if response.status_code == 200:
return json.loads(response.text)
else:
raise Exception(
f"Error with API request: {response.status_code}, {response.text}"
)
def download_video_segment(url, save_dir="."):
try:
# Parse filename from URL
parsed_url = urlparse(url)
filename = os.path.basename(parsed_url.path)
filename = unquote(filename) # Decodes %20, etc.
# Full local path to save the file
local_path = os.path.join(save_dir, filename)
print(f"Downloading {url} → {local_path}")
# Stream download to avoid loading whole file in memory
with requests.get(url, stream=True, timeout=30) as r:
r.raise_for_status()
with open(local_path, "wb") as f:
for chunk in r.iter_content(chunk_size=8192):
if chunk:
f.write(chunk)
print(f"Saved: {local_path}")
except Exception as e:
print(f"Failed to download {url}: {e}")
def parse_args():
parser = argparse.ArgumentParser(description="Uses V2 storage API to download video for a camera.")
parser.add_argument(
"--camera_id",
type=int,
required=True,
help="The camera ID to download clips for. This is the ID of the camera in the V2 storage API.",
)
parser.add_argument(
"--start",
type=str,
required=True,
help="The start time for the clips to download (format: YYYY-MM-DDTHH:MM:SS).",
)
parser.add_argument(
"--end",
type=str,
required=True,
help="The end time for the clips to download (format: YYYY-MM-DDTHH:MM:SS).",
)
parser.add_argument(
"--token",
type=str,
required=True,
help="The channel access token for the camera.",
)
parser.add_argument(
"--endpoint",
type=str,
help="The API endpoint for the storage service.",
required=True
)
return parser.parse_args()
def main():
args = parse_args()
resp = request_storage_api(args)
if len(resp["objects"]) == 0:
print("No video segments found for given time range.")
else:
for segement in resp["objects"]:
download_video_segment(segement["url"])
if __name__ == "__main__":
main()