#read_data.py import json import subprocess from datetime import datetime, timedelta import os # === CONFIGURATION === JSON_DIR = os.path.expanduser("/path/to/uhf-server/data") RECORDINGS_DIR = os.path.expanduser("/path/to/recordings") # ====================== # ANSI color codes GREEN = "\033[92m" RED = "\033[91m" RESET = "\033[0m" def get_actual_duration(full_path): try: result = subprocess.run( ['ffprobe', '-v', 'error', '-show_entries', 'format=duration', '-of', 'default=noprint_wrappers=1:nokey=1', full_path], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True ) return int(float(result.stdout.strip())) except Exception as e: print(f"{RED}Error getting duration for {full_path}: {e}{RESET}") return None def format_seconds(seconds): try: return str(timedelta(seconds=int(seconds))) except: return "N/A" def get_file_size_label(full_path): try: size_bytes = os.path.getsize(full_path) size_mb = size_bytes / (1024 * 1024) if size_mb >= 1000: size_gb = size_mb / 1024 return f"{size_gb:.1f} GB" else: return f"{int(size_mb)} MB" except: return "N/A" # Load the JSON data json_path = os.path.join(JSON_DIR, "db.json") with open(json_path) as f: data = json.load(f) print("== Completed Recordings with File Paths and Durations ==") recordings = data.get("recordings", {}) # Sort by start time if present sorted_recs = sorted(recordings.values(), key=lambda r: r.get("start_time", "")) for rec in sorted_recs: status = rec.get("status") path = rec.get("file_path") if status != "completed" or not path: continue name = rec.get("name", "N/A") start = rec.get("start_time", "N/A") scheduled_sec = rec.get("duration_seconds", 0) try: start_fmt = datetime.fromisoformat(start.replace("Z", "+00:00")).strftime('%Y-%m-%d %H:%M:%S') except: start_fmt = start full_path = os.path.join(RECORDINGS_DIR, os.path.basename(path)) actual_sec = get_actual_duration(full_path) file_size = get_file_size_label(full_path) if actual_sec is None: status_label = f"{RED}Not Complete{RESET}" elif abs(actual_sec - int(scheduled_sec)) <= 2: status_label = f"{GREEN}Complete{RESET}" else: status_label = f"{RED}Not Complete{RESET}" print(f"- {name}") print(f" Start Time : {start_fmt}") print(f" File Path : {path}") print(f" Scheduled Duration : {format_seconds(scheduled_sec)}") print(f" Actual Duration : {format_seconds(actual_sec) if actual_sec is not None else 'N/A'}") print(f" File Size : {file_size}") print(f" Status : {status_label}") print()