# main_start.py # 主启动脚本:读取配置并通过 subprocess 启动 rtsp_service_ws_kadian.py import json import yaml import base64 import subprocess import sys from typing import List from common.camera_config import CameraConfig from utils.logger import get_logger logger = get_logger(__name__) def load_cameras_from_yaml(config_path: str = "config.yaml") -> List[dict]: """从 YAML 文件加载摄像头配置""" with open(config_path, "r", encoding="utf-8") as f: cfg = yaml.safe_load(f) return cfg.get("cameras", []) def cameras_to_base64_json(cameras: List[dict]) -> str: """将摄像头配置转换为 base64 编码的 JSON 字符串""" json_str = json.dumps(cameras, ensure_ascii=False) return base64.b64encode(json_str.encode('utf-8')).decode('ascii') def start_rtsp_service(cameras_base64: str, script_path: str = "rtsp_service_ws_kadian.py"): """启动 RTSP 服务子进程""" cmd = [ sys.executable, # 当前 Python 解释器路径 script_path, "--cameras", cameras_base64 ] logger.info(f"[INFO] Starting RTSP service with command: python {script_path} --cameras ") # 使用 Popen 启动子进程,保持运行 process = subprocess.Popen(cmd) return process if __name__ == "__main__": config_path = "config.yaml" # 1. 读取配置 cameras_data = load_cameras_from_yaml(config_path) logger.info(f"[INFO] Loaded {len(cameras_data)} cameras from {config_path}") if not cameras_data: logger.error("[ERROR] No cameras found in config, exiting...") sys.exit(1) # 2. 转换为 base64 JSON cameras_base64 = cameras_to_base64_json(cameras_data) # 3. 启动子进程 process = start_rtsp_service(cameras_base64) # 4. 等待子进程结束 try: process.wait() except KeyboardInterrupt: logger.info("[INFO] Received interrupt, terminating subprocess...") process.terminate() process.wait() logger.info("[INFO] Subprocess terminated")