#!/usr/bin/env python3 import argparse import subprocess import os import sys # --- CONFIGURAZIONE HIKVISION --- RTSP_USER = "admin" RTSP_PASS = "@Dedelove1" RTSP_PORT = "554" RTSP_PATH = "/Streaming/channels/101" def get_url(ip): return f"rtsp://{RTSP_USER}:{RTSP_PASS}@{ip}:{RTSP_PORT}{RTSP_PATH}" CAM_CONFIG = { "matrimoniale": get_url("192.168.135.2"), "luca": get_url("192.168.135.3"), "ingresso": get_url("192.168.135.4"), "sala": get_url("192.168.135.5"), "taverna": get_url("192.168.135.6"), "retro": get_url("192.168.135.7"), } OUTPUT_PHOTO = "/tmp/cam_snapshot.jpg" OUTPUT_VIDEO = "/tmp/cam_video.mp4" def get_cam_key(cam_name): cam_key = cam_name.lower().strip() if cam_key.startswith("cam "): cam_key = cam_key.replace("cam ", "") if cam_key in CAM_CONFIG: return cam_key for key in CAM_CONFIG: if key in cam_key: return key return None def get_media(cam_name, is_video=False): key = get_cam_key(cam_name) if not key: return None, f"❌ Camera '{cam_name}' non trovata.\nDisponibili: {', '.join(CAM_CONFIG.keys())}" rtsp_url = CAM_CONFIG[key] output_file = OUTPUT_VIDEO if is_video else OUTPUT_PHOTO if is_video: # COMANDO VIDEO (10 SECONDI) # -t 10: Durata 10 secondi # -c:v copy: COPIA il flusso video senza ricodificarlo (Zero CPU, Istantaneo) # Se Telegram non legge il video, cambia "-c:v copy" in "-c:v libx264 -preset ultrafast" cmd = [ "ffmpeg", "-y", "-rtsp_transport", "tcp", "-i", rtsp_url, "-t", "10", # Durata clip "-c:v", "libx264", # Ricodifica leggera per compatibilità Telegram garantita "-preset", "ultrafast", # Velocissimo per non caricare la CPU "-an", # Rimuovi Audio (togli questa riga se vuoi l'audio) "-f", "mp4", output_file ] else: # COMANDO FOTO cmd = [ "ffmpeg", "-y", "-rtsp_transport", "tcp", "-i", rtsp_url, "-frames:v", "1", "-q:v", "2", output_file ] try: subprocess.run(cmd, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL, timeout=15, check=True) if os.path.exists(output_file) and os.path.getsize(output_file) > 0: return output_file, None else: return None, "❌ Errore: File output vuoto." except subprocess.TimeoutExpired: return None, "⏰ Timeout: La cam non risponde." except Exception as e: return None, f"❌ Errore: {e}" if __name__ == "__main__": parser = argparse.ArgumentParser() parser.add_argument("name", help="Nome della camera") parser.add_argument("--video", action="store_true", help="Registra video clip invece di foto") args = parser.parse_args() path, error = get_media(args.name, args.video) if path: print(f"OK:{path}") else: print(f"ERR:{error}")