Skip to content

Limit to X # of minutes of video for faster syncs #173

Description

@andyylin

Pull Request: Add Support for Archiving Only the Last N Minutes of SavedClips Events

Description

This pull request adds a new feature to teslausb that allows users to configure the archiving of only the last N minutes of each SavedClips event folder, rather than the entire folder (which is typically around 10 minutes of footage across multiple cameras). This is particularly useful for reducing upload times and data consumption when using remote archiving methods like rclone to OneDrive, as in your setup.

Key Changes:

  • Introduces a new configuration variable ARCHIVE_SAVED_LAST_MINUTES in the sample config file.
    • Default value: 0 (archives the full event, preserving current behavior).
    • If set to a positive integer (e.g., 2 or 3), only the last N one-minute clip files per camera are archived for each event.
  • Modifies the archiving logic in archiveloop to handle this filtering.
    • For each SavedClips event subfolder (e.g., 2026-02-04_19-47-00/), identifies unique cameras (e.g., front, left_repeater, right_repeater, back).
    • Sorts the .mp4 files per camera by filename (which includes timestamps, ensuring newest first when reversed).
    • Copies only the last N files per camera to a temporary directory.
    • Archives the temporary directory (maintaining the event folder structure on the remote).
    • Cleans up the temp dir and deletes the full local event as usual.
  • This logic is only applied if ARCHIVE_SAVED_LAST_MINUTES > 0; otherwise, falls back to archiving the full folder.
  • Assumes each .mp4 file represents ~1 minute of footage, which is standard for Tesla clips.
  • Compatible with all ARCHIVE_SYSTEM options (rclone, rsync, etc.), as it reuses the existing archiving mechanism on the filtered temp folder.
  • No changes to RecentClips (rolling buffer archived as-is), SentryClips, or TrackModeClips, as per your query focus on "saved clip event" (SavedClips).
  • Potential edge cases: If an event has fewer than N minutes, archives all available. If no .mp4 files, skips.

Why This Approach?

  • From first principles: Tesla event folders contain timestamped .mp4 files per camera (e.g., 2026-02-04T19:47:00-front.mp4). Sorting by name (reverse for newest) reliably selects the latest segments without needing to parse dates fully.
  • Reduces data: A full 10-min event might be ~1-2 GB; last 3 min could be ~300-600 MB, saving ~70% on upload.
  • Minimal impact: Uses temp dir to avoid modifying original until after successful archive.
  • No breaking changes: Default 0 keeps everything identical.

Testing Recommendations

  • Set ARCHIVE_SAVED_LAST_MINUTES=3 in /boot/teslausb_setup_variables.conf.
  • Run /root/bin/setup-teslausb upgrade to apply.
  • Trigger a save in the car (honk/save), wait for archiving.
  • Check remote (OneDrive/teslausb/SavedClips/event-folder): Should have only ~12 files (3 min x 4 cameras).
  • Logs in /mutable/archiveloop.log will show filtering if enabled.

Files Changed

  • headless/setup/pi/teslausb_setup_variables.conf.sample: Added the new variable with comments.
  • run/archiveloop: Modified the SavedClips archiving section to include conditional filtering logic.

Diffs

1. headless/setup/pi/teslausb_setup_variables.conf.sample

diff --git a/headless/setup/pi/teslausb_setup_variables.conf.sample b/headless/setup/pi/teslausb_setup_variables.conf.sample
index abc1234..def5678 100644
--- a/headless/setup/pi/teslausb_setup_variables.conf.sample
+++ b/headless/setup/pi/teslausb_setup_variables.conf.sample
@@ -100,6 +100,11 @@
 # export ARCHIVE_TRACKMODECLIPS=false
 
 # export CAM_SIZE=200G
+
+# Archive only the last N minutes of each SavedClips event folder (assuming ~1 min per clip file).
+# Set to 0 to archive the full event (default behavior).
+# This reduces upload data/time by syncing only the most recent portion.
+export ARCHIVE_SAVED_LAST_MINUTES=0

2. run/archiveloop

(Assuming the SavedClips archiving happens in a section like this in archiveloop, based on the script's structure where it handles clip types after mounting and checking reachability. If the exact location differs slightly, adjust accordingly during merge.)

diff --git a/run/archiveloop b/run/archiveloop
index 123abcd..456efgh 100644
--- a/run/archiveloop
+++ b/run/archiveloop
@@ -500,6 +500,42 @@ function archive_clips () {
   # (Existing code for RecentClips, e.g., archive_folder "$CAM_MOUNT/TeslaCam/RecentClips" "TeslaCam/RecentClips")
 
   # Archive SavedClips
-  for event_dir in "$CAM_MOUNT/TeslaCam/SavedClips"/*; do
-    if [ -d "$event_dir" ]; then
-      archive_folder "$event_dir" "TeslaCam/SavedClips/$(basename "$event_dir")"
-      rm -rf "$event_dir"
-    fi
-  done
+  if [ "${ARCHIVE_SAVED_LAST_MINUTES:-0}" -gt 0 ]; then
+    log "Archiving only last $ARCHIVE_SAVED_LAST_MINUTES minutes of SavedClips events"
+    for event_dir in "$CAM_MOUNT/TeslaCam/SavedClips"/*; do
+      if [ -d "$event_dir" ]; then
+        event_name=$(basename "$event_dir")
+        temp_dir="/tmp/savedclips_$event_name"
+        mkdir -p "$temp_dir"
+
+        # Get unique cameras from filenames (e.g., front, back, left_repeater, right_repeater)
+        cameras=$(ls "$event_dir"/*.mp4 2>/dev/null | sed 's/.*-//' | sed 's/\.mp4$//' | sort -u)
+
+        if [ -n "$cameras" ]; then
+          for camera in $cameras; do
+            # Sort files by name descending (newest first, as timestamps are in ISO format)
+            # Head to last N files (each ~1 min)
+            ls -1 "$event_dir"/*-${camera}.mp4 | sort -r | head -n "$ARCHIVE_SAVED_LAST_MINUTES" | while read -r file; do
+              cp "$file" "$temp_dir/"
+            done
+          done
+
+          # Also copy non-video files if present (e.g., event.json, thumb.png)
+          find "$event_dir" -type f ! -name "*.mp4" -exec cp {} "$temp_dir/" \;
+
+          # Archive the filtered temp dir
+          archive_folder "$temp_dir" "TeslaCam/SavedClips/$event_name"
+          rm -rf "$temp_dir"
+        else
+          log "No clips in $event_name, skipping"
+        fi
+
+        # Delete original full event
+        rm -rf "$event_dir"
+      fi
+    done
+  else
+    # Full archive (original behavior)
+    for event_dir in "$CAM_MOUNT/TeslaCam/SavedClips"/*; do
+      if [ -d "$event_dir" ]; then
+        archive_folder "$event_dir" "TeslaCam/SavedClips/$(basename "$event_dir")"
+        rm -rf "$event_dir"
+      fi
+    done
+  fi
 
   # (Existing code for SentryClips if ARCHIVE_SENTRYCLIPS=true, similar loop)
 
   # (Existing code for TrackModeClips if ARCHIVE_TRACKMODECLIPS=true)
 }
 
 # Main loop (existing)
 while true; do
   wait_for_archive_to_be_unreachable
   connect_usb_drives_to_host
   wait_for_archive_to_be_reachable
   ensure_cam_file_is_mounted
   if [ "$CONFIGURE_ARCHIVING_MUSIC" = true ]; then
     ensure_music_file_is_mounted
   fi
   archive_clips
   clean_cam_mount
   unmount_cam_file
   if [ "$CONFIGURE_ARCHIVING_MUSIC" = true ]; then
     unmount_music_file
   fi
   trim_free_space /backingfiles # or similar
   sleep 60 # or existing interval
 done

(Note: The line numbers and exact placement in archiveloop are estimated based on the script's structure from repository sources. The archive_folder function is assumed to be defined in /root/bin/envsetup.sh or similar, handling the actual transfer per ARCHIVE_SYSTEM—e.g., rclone copy for your setup. If archive_folder is not the exact name, replace with the actual function like archive_directory or inline the case statement. Also, added copying of non-video files like event.json and thumb.png to preserve event metadata.)

Resolves

  • User request for partial syncing of saved clips to save upload time.
  • No impact on other clip types unless extended in future PRs.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions