A Python automation toolkit for EZVIZ and RTSP IP camera discovery, MySQL-backed camera control, frame quality validation, and FFmpeg timelapse video recording. It scans local networks for RTSP cameras, synchronizes IP/MAC inventory to MySQL, processes queued camera actions, and provides a resilient timelapse worker with sharpness-based frame selection and disk space management.
- Fast LAN Camera Discovery & ARP Mapping: Concurrently scans a local IPv4 subnet (default
192.168.1.0/24) on RTSP port554usingThreadPoolExecutor, resolves MAC addresses via the OS ARP table (arp -a), and caches results for 1 hour. - MySQL Camera Inventory & Action Queue: Automatically upserts discovered cameras (
ip_address,ip_type,mac_address) intoxcam_camerasand pollsxcam_actionsforpendingjobs, updating status transitions (pending→in_progress→done/failed). - RTSP Stream Testing & Resilient Frame Capture: Verifies RTSP stream health via FFmpeg (
test_rtsp_connection) and captures frames with automatic retries across three fallback FFmpeg strategies (TCP MJPEG, UDP MJPEG, and TCP+genpts/yuv420p). - Automated Frame Validation & Sharpness Selection: Validates captured frames for file size, minimum dimensions, and grayscale brightness using Pillow, and selects the sharpest frame from multi-shot bursts via NumPy gradient variance.
- FFmpeg Timelapse Worker (
TimelapseWorker): Records interval snapshots at configurable resolutions (480p,720p,1080p), maintains rolling frame backups, monitors free disk space, and compiles continuous frame sequences into MP4 videos usinglibx264(h264) orlibx265(h265) with+faststart.
- Language: Python 3
- Media Processing: FFmpeg (CLI via
subprocess) - Database: MySQL (
mysql-connector-python) - Imaging & Networking: Pillow (
PIL), NumPy,requests, standard library (socket,ipaddress,concurrent.futures)
my-cam-control/
├── main.py # Main worker loop: camera discovery sync + MySQL action polling
├── CamHelper.py # RTSP URL builder, connectivity tests, multi-strategy frame capture & validation
├── EzvizCam.py # TimelapseWorker class for scheduled capture, disk checks & MP4 encoding
├── IPHelper.py # Concurrent subnet port 554 scanner, EZVIZ HTTP check & ARP MAC resolver
├── DbHelper.py # MySQL wrapper with CRUD, batch upserts, and schema/action constants
├── FrameHelper.py # Session frame cleanup and rolling backup retention (latest 100 backups)
├── SysConfig.py # Loader/writer for MySQL connection settings (sys_config.json)
├── common.py # ~/.mycamcontrol workspace setup, logging, status/config JSON & frame renumbering
├── db.sql # MySQL schema for xcam_actions and xcam_cameras tables
├── db.sample.txt # Example DbHelper CRUD usage snippets
└── requirements.txt # Python dependencies
At runtime, common.py initializes a working directory at ~/.mycamcontrol/:
~/.mycamcontrol/
├── config/
│ ├── sys_config.json # MySQL connection settings
│ ├── cam_info.json # Cached LAN camera discovery results
│ ├── config.json # TimelapseWorker settings (RTSP URL, FPS, codec, quality, interval)
│ └── control.json # TimelapseWorker runtime control state (start/stop, current_video, processing)
├── frames/ # Active session JPEG frames (frame_000001.jpg, ...)
├── backups/ # Rolling backup frames (up to 100 kept)
├── videos/ # Rendered MP4 timelapse videos
└── logs/
└── app.log # Application log file
- Python 3.10+ (compatible with
numpy~=2.3.2,pillow~=11.3.0,requests~=2.32.5) - FFmpeg installed and available in your system
PATH - MySQL 8.0+ (or compatible InnoDB server)
arpcommand-line utility available on the host OS for IP-to-MAC resolution
git clone https://github.com/sinhsac/my-cam-control.git cd my-cam-control python -m venv .venv source .venv/bin/activate # On Windows: .venv\Scripts\activate pip install -r requirements.txt
Create a database and import the schema from db.sql:
mysql -u <user> -p <database_name> < db.sql
This creates two tables:
xcam_cameras: stores discovered cameras (ip_address,cam_name,mac_address,ip_type,username,password,channel,status, timestamps).xcam_actions: stores queued camera commands (command,additionsJSON payload,status, timestamps).
Run python main.py once to generate the ~/.mycamcontrol/config/sys_config.json template (or create it manually) and populate your MySQL credentials:
{
"db_host": "127.0.0.1",
"db_port": "3306",
"db_user": "root",
"db_password": "your_password",
"db_name": "your_database"
}python main.py
The worker continuously:
- Scans the local subnet (
192.168.1.0/24) for open RTSP port554hosts when the cache incam_info.jsonexpires (every 3600 seconds), maps IPs to MAC addresses viaarp -a, and upserts them intoxcam_cameras. - Polls
xcam_actionsfor the most recent row withstatus = 'pending'. - Resolves target cameras by
mac_addressesfrom the JSONadditionscolumn and executes the action.
Ensure the target camera row in xcam_cameras has its username and password populated, then insert an action:
INSERT INTO xcam_actions (command, additions, status) VALUES ('check_config', '{"mac_addresses": ["aa-bb-cc-dd-ee-ff"]}', 'pending');
Note:
DbHelper.ActionTypedefines constants forcheck_config,capture_image,capture_and_stitch, andstart_recording;main.pycurrently implements thecheck_configRTSP connectivity check handler.
To run standalone timelapse recording using TimelapseWorker, configure ~/.mycamcontrol/config/config.json:
{
"rtsp_url": "rtsp://admin:[email protected]:554/h264/ch1/main/av_stream",
"interval": 10,
"quality": "720p",
"output_fps": 15,
"codec": "h264",
"disk_warning_threshold": 1024
}Control recording state via ~/.mycamcontrol/config/control.json:
{
"status": "start",
"current_video": "timelapse_session.mp4",
"processing": false
}Start the worker from Python:
python -c "from EzvizCam import TimelapseWorker; TimelapseWorker().run()"Setting "status": "stop" in control.json (or sending SIGINT/SIGTERM) finalizes the session frames into an MP4 file under ~/.mycamcontrol/videos/.
This project is licensed under the MIT License — see the LICENSE file for details.