From 14a07782e37562bb3fed546791a8aea0e215319c Mon Sep 17 00:00:00 2001 From: zqc <835569504@qq.com> Date: Fri, 23 Jan 2026 10:28:54 +0800 Subject: [PATCH] =?UTF-8?q?=E5=BC=95=E5=85=A5test=5Fcam=E6=96=87=E4=BB=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test_cam.py | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 53 insertions(+) create mode 100644 test_cam.py diff --git a/test_cam.py b/test_cam.py new file mode 100644 index 0000000..55249b3 --- /dev/null +++ b/test_cam.py @@ -0,0 +1,53 @@ +import requests +import urllib3 +import yaml +from hikvision_openapi_signer import HikvisionOpenAPISigner + +urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning) + +# 初始化签名器 +signer = HikvisionOpenAPISigner( + "https://29.2.19.19:443", + "28031410", + "Y79rv6LqBahxoEH6adLv", + headers={'tagId': '0'} # 根据平台要求设置 +) + +def get_camera_preview_url(camera_index_code): + # 签名一个请求 + request = signer.sign( + 'POST', + '/api/video/v1/cameras/previewURLs', + jsons={'cameraIndexCode': camera_index_code, 'protocol': 'rtsp', 'expand': 'streamform=rtp'}, + accept='application/json' + ) + method, url, headers, body = request + # 发送请求 + response = requests.request(method, url, headers=headers, data=body, verify=False) + return response.json() + +# 读取 config.yaml +with open('config.yaml', 'r', encoding='utf-8') as f: + config = yaml.safe_load(f) + +# 遍历所有摄像头 +for camera in config['cameras']: + if 'index' in camera: + index = camera['index'] + print(f"正在获取摄像头 {camera['name']} (index: {index}) 的预览地址...") + result = get_camera_preview_url(index) + print(f"API返回结果: {result}") + + # 提取 url 并更新到 config + if 'data' in result and 'url' in result['data']: + rtsp_url = result['data']['url'] + camera['rtsp_url'] = rtsp_url + print(f"更新 rtsp_url: {rtsp_url}") + else: + print(f"未找到 url 在返回结果中") + +# 保存更新后的 config.yaml +with open('config.yaml', 'w', encoding='utf-8') as f: + yaml.dump(config, f, default_flow_style=False, allow_unicode=True) + +print("config.yaml 已更新")