Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/core/media/direct_downloader.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ async def stop_download(self) -> None:
if self.download_task:
try:
await asyncio.wait_for(self.download_task, timeout=10.0)
except asyncio.TimeoutError:
except TimeoutError:
logger.warning(f"Download Timeout: {self.record_url}")
except Exception as e:
logger.error(f"Download Error: {e}")
Expand Down
8 changes: 8 additions & 0 deletions app/core/recording/record_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -496,6 +496,14 @@ async def delete_recording_cards(self, recordings: list[Recording]):
async def check_free_space(self, output_dir: str | None = None):
disk_space_limit = float(self.settings.user_config.get("recording_space_threshold") or 0)
output_dir = output_dir or self.settings.get_video_save_path()

if utils.check_disk_capacity(output_dir) < disk_space_limit and self.settings.user_config.get(
"auto_clean_old_recordings"
):
deleted = utils.clean_old_recordings(output_dir, disk_space_limit)
if deleted:
logger.info(f"Auto-cleaned {len(deleted)} old recording file(s) to free disk space")

if utils.check_disk_capacity(output_dir) < disk_space_limit:
self.services.recording_enabled = False
logger.error(f"Disk space remaining is below {disk_space_limit} GB. Recording function disabled")
Expand Down
2 changes: 1 addition & 1 deletion app/core/recording/stream_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -424,7 +424,7 @@ async def start_ffmpeg(
process.stdin.close()

await asyncio.wait_for(process.wait(), timeout=15.0)
except asyncio.TimeoutError:
except TimeoutError:
logger.warning(f"FFmpeg process did not exit gracefully, forcing termination: {live_url}")
process.kill()
await process.wait()
Expand Down
2 changes: 1 addition & 1 deletion app/core/runtime/process_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ async def cleanup(self):

try:
await asyncio.wait_for(process.wait(), timeout=5.0)
except asyncio.TimeoutError:
except TimeoutError:
logger.warning(f"Process {process.pid} did not terminate, killing it")
process.kill()
await process.wait()
Expand Down
2 changes: 1 addition & 1 deletion app/messages/notification_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def send_sync() -> dict[str, list[str]]:
try:
overall_timeout = max(timeout * 4, timeout + 5)
return await asyncio.wait_for(asyncio.to_thread(send_sync), timeout=overall_timeout)
except asyncio.TimeoutError:
except TimeoutError:
logger.error(f"Email push timed out, SMTP server: {email_host}")
except (smtplib.SMTPException, OSError, ValueError) as e:
logger.error(f"Email push failed, SMTP server: {email_host}, Error message: {e}")
Expand Down
9 changes: 9 additions & 0 deletions app/ui/views/settings_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,6 +457,15 @@ def create_recording_settings_tab(self):
on_change=self.on_change,
),
),
self.create_setting_row(
self._["auto_clean_old_recordings"],
ft.Switch(
value=self.get_config_value("auto_clean_old_recordings"),
data="auto_clean_old_recordings",
on_change=self.on_change,
tooltip=self._["auto_clean_old_recordings_tip"],
),
),
self.create_setting_row(
self._["segment_time"],
ft.TextField(
Expand Down
40 changes: 40 additions & 0 deletions app/utils/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,46 @@ def check_disk_capacity(file_path: str | Path, show: bool = False) -> float:
return free_space_gb


VIDEO_FILE_EXTENSIONS = {".mp4", ".flv", ".ts", ".mkv", ".aac", ".mp3", ".webm"}


def clean_old_recordings(directory: str | Path, target_free_gb: float, protect_minutes: int = 10) -> list[str]:
"""Delete the oldest recording files until at least ``target_free_gb`` remains.

Only video/audio files recorded under ``directory`` are considered. Files
modified within ``protect_minutes`` are never touched, so recordings that
are still in progress (or just finished) stay safe. Returns the list of
deleted file paths, oldest first.
"""
directory = os.path.abspath(directory)
now = datetime.now().timestamp()
candidates: list[tuple[float, str]] = []
for root, _dirs, files in os.walk(directory):
for name in files:
if os.path.splitext(name)[1].lower() not in VIDEO_FILE_EXTENSIONS:
continue
path = os.path.join(root, name)
try:
mtime = os.path.getmtime(path)
except OSError:
continue
if now - mtime < protect_minutes * 60:
continue
candidates.append((mtime, path))

deleted: list[str] = []
for _mtime, path in sorted(candidates):
if shutil.disk_usage(directory).free / (1024**3) >= target_free_gb:
break
try:
os.remove(path)
deleted.append(path)
logger.info(f"Auto-cleaned old recording file: {path}")
except OSError as e:
logger.warning(f"Failed to delete old recording file {path}: {e}")
return deleted


def handle_proxy_addr(proxy_addr):
if proxy_addr:
if not proxy_addr.startswith("http"):
Expand Down
1 change: 1 addition & 0 deletions config/default_settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
"default_live_source": "FLV",
"flv_use_direct_download": false,
"recording_space_threshold": "2.0",
"auto_clean_old_recordings": false,
"video_segment_time": "1800",
"convert_to_mp4": true,
"delete_original": false,
Expand Down
Loading
Loading