59 lines
1.9 KiB
Python
59 lines
1.9 KiB
Python
#!/usr/bin/env python3
|
|
|
|
import argparse
|
|
import json
|
|
from copy import deepcopy
|
|
|
|
|
|
def main():
|
|
ap = argparse.ArgumentParser(description="Duplicate an instance N times to create multi-channel configs.")
|
|
ap.add_argument("--in", dest="inp", required=True, help="Input config.json")
|
|
ap.add_argument("--out", dest="out", required=True, help="Output config.json")
|
|
ap.add_argument("--instance", required=True, help="Base instance name to duplicate")
|
|
ap.add_argument("--count", type=int, required=True, help="Number of instances to generate")
|
|
ap.add_argument("--name-prefix", default=None, help="New instance name prefix (default: base name)")
|
|
ap.add_argument(
|
|
"--url-pattern",
|
|
default=None,
|
|
help="Optional url pattern with {i} (0-based) and {n} (1-based), e.g. rtsp://ip/stream{n}",
|
|
)
|
|
args = ap.parse_args()
|
|
|
|
with open(args.inp, "r", encoding="utf-8") as f:
|
|
cfg = json.load(f)
|
|
|
|
insts = cfg.get("instances")
|
|
if not isinstance(insts, list):
|
|
raise SystemExit("root.instances must be an array")
|
|
|
|
base = None
|
|
for it in insts:
|
|
if isinstance(it, dict) and it.get("name") == args.instance:
|
|
base = it
|
|
break
|
|
if base is None:
|
|
raise SystemExit(f"instance not found: {args.instance}")
|
|
|
|
prefix = args.name_prefix or args.instance
|
|
out_insts = []
|
|
for i in range(max(0, args.count)):
|
|
one = deepcopy(base)
|
|
one["name"] = f"{prefix}_{i+1:02d}"
|
|
params = one.get("params")
|
|
if not isinstance(params, dict):
|
|
params = {}
|
|
one["params"] = params
|
|
if args.url_pattern is not None:
|
|
params["url"] = args.url_pattern.format(i=i, n=i + 1)
|
|
out_insts.append(one)
|
|
|
|
cfg["instances"] = out_insts
|
|
|
|
with open(args.out, "w", encoding="utf-8") as f:
|
|
json.dump(cfg, f, ensure_ascii=False, indent=2)
|
|
f.write("\n")
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|