Real-time RTSP video stream processor with motion detection, object recognition, and analysis capabilities. Built with Python and OpenCV.
Project description
RTSP Stream Processor
Real-time RTSP video stream processor with motion detection, object recognition, and analysis capabilities. Built with Python and OpenCV.
Author
Tom Sapletta
Repository
https://github.com/mediainspect/rtsp.git
๐ Features
-
RTSP Stream Handling
- Secure credential management
- Automatic reconnection
- Buffer management
- Support for TCP/UDP protocols
-
Network Scanning
- Port scanning without root privileges
- Service identification
- Common port detection
- Concurrent scanning
- Banner grabbing
-
Video Processing
- Motion detection
- Object recognition
- Frame analysis
- Real-time statistics
-
Monitoring
- Performance metrics
- Health checks
- Processing statistics
- Docker integration
๐ Network Scanning
The mediainspect_rtsp.network package provides powerful network scanning capabilities without requiring root privileges.
Key Components
SimpleNetworkScanner: Main scanner class for port and service detectionNetworkService: Dataclass representing a discovered network serviceScanResult: Container for scan results with metadata
Basic Usage
import asyncio
from mediainspect_rtsp.network import SimpleNetworkScanner, format_scan_results
async def main():
# Create a scanner instance
scanner = SimpleNetworkScanner(timeout=2.0)
# Scan common ports on a host
results = await scanner.scan_common_ports("example.com")
print(format_scan_results(results))
# Or scan specific ports
results = await scanner.scan_ports("example.com", [80, 443, 8080])
print(f"Found {len(results.services)} services")
# Run the async function
asyncio.run(main())
Command Line Interface
# Scan common ports on a host
python -m mediainspect_rtsp.network.main example.com --common
# Scan specific ports
python -m mediainspect_rtsp.network.main example.com --ports 80,443,8080-8090
# With custom timeout
python -m mediainspect_rtsp.network.main example.com --common --timeout 1.5
API Reference
SimpleNetworkScanner
__init__(self, timeout: float = 2.0): Initialize with connection timeoutscan_port(ip: str, port: int) -> NetworkService: Scan a single portscan_ports(ip: str, ports: List[int]) -> ScanResult: Scan multiple ports concurrentlyscan_common_ports(ip: str) -> ScanResult: Scan all common portscheck_port(ip: str, port: int) -> bool: Check if a port is openidentify_service(ip: str, port: int) -> Dict[str, Any]: Identify service on a port
NetworkService
ip: str: IP address of the serviceport: int: Port numberservice: str: Service name (e.g., 'http', 'ssh')protocol: str: Protocol (usually 'tcp' or 'udp')banner: str: Service banner if availableis_secure: bool: Whether the connection is secureis_up: bool: Whether the service is up
ScanResult
services: List[NetworkService]: List of discovered servicesduration: float: Scan duration in secondstotal_ports: int: Total number of ports scannedopen_ports: int: Number of open ports foundto_dict() -> Dict: Convert results to dictionary
Utility Functions
parse_ports(ports_str: str) -> List[int]: Parse port string (e.g., "80,443,8080-8090")format_scan_results(results: ScanResult) -> str: Format results as a string
Advanced Examples
Example 1: Network Inventory
Create an inventory of all services running on common ports across multiple hosts:
import asyncio
from mediainspect_rtsp.network import SimpleNetworkScanner
async def scan_hosts(hosts):
scanner = SimpleNetworkScanner()
tasks = [scanner.scan_common_ports(host) for host in hosts]
return await asyncio.gather(*tasks)
hosts = ["192.168.1.1", "192.168.1.100", "192.168.1.200"]
results = asyncio.run(scan_hosts(hosts))
for host, result in zip(hosts, results):
print(f"\n{host} - {result.open_ports} open ports:")
for service in result.services:
if service.is_up:
print(f" - {service.port}/tcp: {service.service}")
Example 2: Security Check for Common Vulnerable Services
Check for potentially vulnerable services that should be secured:
import asyncio
from mediainspect_rtsp.network import SimpleNetworkScanner
VULNERABLE_SERVICES = {
'ftp': 21,
'telnet': 23,
'http': 80,
'snmp': 161,
'mssql': 1433,
'oracle': 1521,
'mysql': 3306,
'rdp': 3389,
'vnc': [5900, 5901]
}
async def check_vulnerable_services(host):
scanner = SimpleNetworkScanner()
ports = []
# Get all ports from vulnerable services
for service in VULNERABLE_SERVICES.values():
if isinstance(service, list):
ports.extend(service)
else:
ports.append(service)
results = await scanner.scan_ports(host, ports)
print(f"\nSecurity check for {host}:")
for service in results.services:
if service.is_up:
status = "โ ๏ธ WARNING" if service.port in [21, 23, 161] else "โน๏ธ INFO"
print(f"{status} - {service.port}/tcp: {service.service} is running")
if service.banner:
print(f" Banner: {service.banner[:100]}...")
# Run the check
asyncio.run(check_vulnerable_services("example.com"))
Example 3: Monitor Service Availability
Continuously monitor services and alert on status changes:
import asyncio
import time
from datetime import datetime
from mediainspect_rtsp.network import SimpleNetworkScanner
class ServiceMonitor:
def __init__(self, host, ports, interval=300):
self.host = host
self.ports = ports
self.interval = interval
self.scanner = SimpleNetworkScanner()
self.known_services = {}
async def check_services(self):
results = await self.scanner.scan_ports(self.host, self.ports)
current_time = datetime.now().strftime("%Y-%m-%d %H:%M:%S")
# Check for new services
for service in results.services:
port_key = f"{service.port}/{service.protocol}"
if service.is_up and port_key not in self.known_services:
print(f"[{current_time}] NEW SERVICE: {port_key} - {service.service}")
self.known_services[port_key] = service
elif not service.is_up and port_key in self.known_services:
print(f"[{current_time}] SERVICE DOWN: {port_key} - {self.known_services[port_key].service}")
del self.known_services[port_key]
async def run(self):
print(f"Starting service monitor for {self.host}...")
while True:
try:
await self.check_services()
await asyncio.sleep(self.interval)
except KeyboardInterrupt:
print("\nStopping monitor...")
break
except Exception as e:
print(f"Error during scan: {e}")
await asyncio.sleep(5)
# Monitor common services
monitor = ServiceMonitor(
host="example.com",
ports=[21, 22, 23, 25, 53, 80, 443, 3306, 3389, 5432, 8080, 8443],
interval=300 # 5 minutes
)
asyncio.run(monitor.run())
Example 4: Find RTSP Cameras on Local Network
Scan for RTSP cameras on the local network:
import asyncio
import ipaddress
from mediainspect_rtsp.network import SimpleNetworkScanner
RTSP_PORTS = [554, 8554, 1935, 1936, 8080, 8081, 8082, 8888, 1935]
def get_local_ips():
"""Get all IPs in local network"""
# This is a simplified example - you might want to use a more robust method
network = ipaddress.IPv4Network("192.168.1.0/24", strict=False)
return [str(ip) for ip in network.hosts()]
async def find_rtsp_cameras():
scanner = SimpleNetworkScanner(timeout=1.0)
local_ips = get_local_ips()
print(f"Scanning {len(local_ips)} IPs for RTSP cameras...")
for ip in local_ips:
try:
results = await scanner.scan_ports(ip, RTSP_PORTS)
for service in results.services:
if service.is_up and service.service == 'rtsp':
print(f"\n๐ฅ Found RTSP camera at rtsp://{ip}:{service.port}")
if service.banner:
print(f" Banner: {service.banner}")
except Exception as e:
print(f"Error scanning {ip}: {e}")
asyncio.run(find_rtsp_cameras())
Best Practices
-
Rate Limiting:
- Add delays between scans to avoid overwhelming networks
- Use
asyncio.sleep()between batches of scans
-
Error Handling:
- Always wrap scans in try/except blocks
- Handle common exceptions like
ConnectionRefusedError,TimeoutError
-
Performance:
- Adjust the
timeoutparameter based on network conditions - For large scans, process results in batches
- Adjust the
-
Security:
- Only scan networks you have permission to scan
- Be aware of legal implications in your jurisdiction
- Consider adding authentication when accessing services
๐ Prerequisites
- Python 3.7+
- FFmpeg
- OpenCV dependencies
- Docker (optional)
๐ ๏ธ Makefile Usage
The Makefile provides convenient commands for common tasks. Run make help to see all available targets.
Project Management
make installโ Install dependenciesmake testโ Run testsmake lintโ Lint codemake runโ Run the main applicationmake cleanโ Remove caches
Network Scanning
make scan-networkโ Scan the default network for common servicesmake scan-camerasโ Scan for cameras and related servicesmake scan-camera IP=192.168.1.100โ Scan a specific camera IPmake scan-quickโ Quick scan of common portsmake scan-fullโ Comprehensive scanmake scan-localโ Scan common local network ranges
Printer Management
make scan-printersโ List all available printers
Shell & Interactive Clients
make shellโ Start a Python shell in the package context (now inscripts/)make interactiveโ Start the interactive command-line client (now inscripts/)
Help
make helpโ Show all available targets
๐ Shell Client
Start an interactive Python shell with project context:
make shell
(Uses scripts/shell_client.py)
๐ฅ๏ธ Interactive CLI
Launch the interactive command-line interface:
make interactive
(Uses scripts/interactive_client.py)
๐ Network Scanning & Printing
mediainspect includes powerful network scanning capabilities to discover devices like cameras and printers on your local network.
Scan for Network Devices
Scan your local network for various devices and services:
make scan-network
Discover Cameras
Find RTSP cameras on your network:
make scan-cameras
Print a Test Page
Send a test page to your default printer:
make print-test
Using the Network Scanner in Python
You can also use the network scanner directly in your Python code:
from mediainspect.scanner import NetworkScanner
import asyncio
async def scan_network():
scanner = NetworkScanner()
# Scan for all services
services = await scanner.scan_network()
# Or scan for specific service types
cameras = await scanner.scan_network(service_types=['rtsp'])
for service in services:
print(f"{service.ip}:{service.port} - {service.service} ({service.banner})")
# Run the scan
asyncio.run(scan_network())
๐จ๏ธ Printing Support
mediainspect includes basic printing capabilities using the CUPS (Common Unix Printing System) interface.
๐ง Installation
Using pip
# Clone the repository
git clone https://github.com/mediainspect/rtsp.git
cd rtsp
# Create virtual environment
python -m venv venv
source venv/bin/activate # Windows: .\venv\Scripts\activate
# Install dependencies
pip install -r requirements.txt
start
python main.py
Using Docker
# Build and run using docker-compose
docker-compose up -d
# View logs
docker-compose logs -f
โ๏ธ Configuration
- Create environment file:
cp .env.template .env
- Configure your settings:
# RTSP Credentials
RTSP_USER=your_username
RTSP_PASSWORD=your_password
# RTSP Stream Configuration
RTSP_HOST=stream_host_ip
RTSP_PORT=554
RTSP_PATH=/stream
# Processing Configuration
MOTION_THRESHOLD=25.0
BLUR_SIZE=21
๐ป Usage
Basic Usage
from rtsp_client import RTSPClient
# Initialize client
client = RTSPClient()
# Connect to stream
if client.connect():
try:
while True:
frame = client.read_frame()
# Process frame here
finally:
client.disconnect()
With Custom Processing
from video_processor import VideoProcessor
processor = VideoProcessor(
motion_threshold=25.0,
blur_size=21,
min_object_size=1000
)
def process_frame(frame):
processed_frame, stats = processor.process(frame)
return processed_frame
๐ฆ Python Package Information
- Package name: mediainspect-rtsp
- PyPI: https://pypi.org/project/mediainspect-rtsp/
- Source: https://github.com/mediainspect/rtsp
- License: Apache 2.0
- Author: Tom Sapletta
- Description: Real-time RTSP video stream processor with motion detection, object recognition, and analysis capabilities. Built with Python and OpenCV.
Installation
pip install mediainspect-rtsp
Usage Example
from mediainspect_rtsp.video_processor_rtsp_class import VideoProcessor
processor = VideoProcessor(rtsp_url="rtsp://...", motion_threshold=25.0)
processor.run()
For more details, see the PyPI page and documentation.
๐ Monitor and Debug
Health Checks
Access health metrics at:
Prometheus & Grafana
- Prometheus: http://localhost:9090
- Grafana: http://localhost:3000
๐ ๏ธ Development
Project Structure
rtsp-processor/
โโโ src/
โ โโโ rtsp_client.py # RTSP handling
โ โโโ video_processor.py # Video processing
โ โโโ config.py # Configuration management
โโโ tests/
โ โโโ test_*.py # Test files
โโโ docker/
โ โโโ Dockerfile # Container definition
โ โโโ docker-compose.yml
โโโ .env.template # Environment template
โโโ scripts/
โ โโโ shell_client.py # Shell client
โ โโโ interactive_client.py # Interactive client
โโโ README.md # This file
๐งช Running Tests
This project uses pytest for testing. To run all tests:
make test
Or directly with pytest:
pytest
All modules in mediainspect_rtsp/ are covered by basic import tests in tests/. Extend these with functional tests as needed.
๐ค Contributing
- Fork the repository
- Create feature branch (
git checkout -b feature/amazing-feature) - Commit changes (
git commit -m 'Add amazing feature') - Push to branch (
git push origin feature/amazing-feature) - Open Pull Request
๐ Versioning
We use SemVer for versioning. For available versions, see the CHANGELOG.md.
๐ Security
Credential Handling
- Credentials stored in .env file
- Passwords never logged
- URL encoding for special characters
- Secure connection handling
Best Practices
- Use environment variables
- Regularly update dependencies
- Follow security advisories
- Implement proper error handling
โ Common Issues
- OpenCV Import Error
# Install system dependencies
sudo ./install_opencv.sh
- RTSP Connection Failed
- Verify credentials
- Check network connectivity
- Confirm stream availability
- Performance Issues
- Adjust buffer size
- Modify processing parameters
- Check system resources
๐ Performance Tuning
Memory Usage
# Configure buffer size
client = RTSPClient(buffer_size=1024*1024)
Processing Speed
# Adjust processing parameters
processor = VideoProcessor(
skip_frames=2,
downscale_factor=0.5
)
๐ Documentation
๐ License
This project is licensed under the MIT License - see LICENSE file.
๐ Acknowledgments
- OpenCV community
- FFmpeg project
- Docker community
- All contributors
๐ Support
For support, please:
- Check documentation
- Search existing issues
- Create new issue if needed
๐ Changelog
See CHANGELOG.md for all changes.
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file mediainspect_rtsp-0.1.4.tar.gz.
File metadata
- Download URL: mediainspect_rtsp-0.1.4.tar.gz
- Upload date:
- Size: 40.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.14.6-300.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
61be57a3b5873d182b6d782cf381301470fdee851efb48ff08471b87307e7524
|
|
| MD5 |
59bebbf7064d608e737391447e5a0adb
|
|
| BLAKE2b-256 |
a13bb7c81b433f27999bf2ec34226c850bbb6881f9f46d24af607ce996f62d10
|
File details
Details for the file mediainspect_rtsp-0.1.4-py3-none-any.whl.
File metadata
- Download URL: mediainspect_rtsp-0.1.4-py3-none-any.whl
- Upload date:
- Size: 53.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.1.3 CPython/3.11.12 Linux/6.14.6-300.fc42.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e4cae36593e91d1b30270053124372bf06c8c8e4c59c4c981157754f0895d20
|
|
| MD5 |
62d24d7c928b21271625c463fac1309a
|
|
| BLAKE2b-256 |
f37af590015519e3b776e160d40340384f9d5ca144f28b4e197aacebfa535087
|