sinhsac.github.io / my-cam-control

My Cam Control

Python automation toolkit for EZVIZ & RTSP IP camera discovery, MySQL-backed inventory & action queueing, sharpness-based frame validation, and resilient FFmpeg H.264/H.265 timelapse recording.

Python 3.10+ FFmpeg · H.264 / H.265 MySQL 8.0+ EZVIZ & RTSP MIT License

Key Capabilities

  • Concurrent LAN Discovery & ARP Mapping (IPHelper.py): Scans the local IPv4 subnet (default 192.168.1.0/24) on RTSP port 554 using ThreadPoolExecutor, resolves MAC addresses via arp -a, and caches results in cam_info.json for 1 hour.
  • MySQL Camera Inventory & Action Queue (DbHelper.py, main.py): Upserts discovered cameras into xcam_cameras and continuously polls xcam_actions for pending jobs, tracking status transitions (pending → in_progress → done / failed).
  • 3-Strategy Resilient Frame Capture (CamHelper.py): Tests RTSP health via FFmpeg and automatically falls back across TCP MJPEG, UDP MJPEG, and TCP +genpts / yuv420p.
  • Frame Quality & Sharpness Selection: Rejects corrupted, undersized, or dark/blank frames using Pillow, and selects the sharpest shot from bursts via NumPy gradient variance.
  • FFmpeg Timelapse Worker (EzvizCam.py): Captures scheduled snapshots at 480p, 720p, or 1080p, keeps rolling frame backups (latest 100), enforces free disk thresholds, and encodes MP4 videos with libx264 or libx265 + +faststart.

Prerequisites

  • Python 3.10+ (numpy, pillow, requests, mysql-connector-python)
  • FFmpeg installed and available on your system PATH
  • MySQL 8.0+ (or compatible InnoDB server)
  • Host OS arp CLI utility for IP-to-MAC resolution

Setup & Configuration

1. Clone and Install Dependencies

git clone https://github.com/sinhsac/my-cam-control.git
cd my-cam-control
python -m venv .venv
source .venv/bin/activate   # Windows: .venv\Scripts\activate
pip install -r requirements.txt

2. Import the MySQL Schema

mysql -u <user> -p <database_name> < db.sql

This creates two tables: xcam_cameras (camera inventory & credentials) and xcam_actions (queued command jobs).

3. Configure MySQL Credentials

Run python main.py once to scaffold ~/.mycamcontrol/, then edit ~/.mycamcontrol/config/sys_config.json:

{
  "db_host": "127.0.0.1",
  "db_port": "3306",
  "db_user": "root",
  "db_password": "your_password",
  "db_name": "your_database"
}

4. Start the Main Discovery & Action Worker

python main.py

Queueing a Camera Health Check (check_config)

After populating username and password for a camera row in xcam_cameras, queue an action:

INSERT INTO xcam_actions (command, additions, status)
VALUES ('check_config', '{"mac_addresses": ["aa-bb-cc-dd-ee-ff"]}', 'pending');
Implemented Action Handlers

DbHelper.ActionType defines constants for check_config, capture_image, capture_and_stitch, and start_recording. Out of the box, main.py executes the check_config RTSP verification handler and can be extended for custom action types.

Running the FFmpeg Timelapse Worker

Configure your RTSP stream, interval, resolution, and codec in ~/.mycamcontrol/config/config.json:

{
  "rtsp_url": "rtsp://admin:password@192.168.1.100:554/h264/ch1/main/av_stream",
  "interval": 10,
  "quality": "720p",
  "output_fps": 15,
  "codec": "h264",
  "disk_warning_threshold": 1024
}

Control the recording session via ~/.mycamcontrol/config/control.json:

{
  "status": "start",
  "current_video": "timelapse_session.mp4",
  "processing": false
}

Launch the worker:

python -c "from EzvizCam import TimelapseWorker; TimelapseWorker().run()"

When you change "status" to "stop" in control.json (or press Ctrl+C / send SIGTERM), the worker cleanly compiles all captured session frames into ~/.mycamcontrol/videos/timelapse_session.mp4.

Runtime Workspace Layout

~/.mycamcontrol/
├── config/
│   ├── sys_config.json  # MySQL connection settings
│   ├── cam_info.json    # Cached LAN discovery results (1-hour TTL)
│   ├── config.json      # TimelapseWorker RTSP URL, interval, FPS, codec
│   └── control.json     # Runtime start/stop & output video state
├── frames/              # Active session JPEGs (frame_000001.jpg, ...)
├── backups/             # Rolling frame backups (latest 100 retained)
├── videos/              # Encoded MP4 timelapse videos
└── logs/
    └── app.log          # Worker logs