#!/bin/bash # Process IP cam recordings: condense all but newest, merge into motion.mkv # Intended to run periodically via cron LOCK="/tmp/ipcam-process.lock" exec 9>"$LOCK" flock -n 9 || exit 0 DIR="$(cd "$(dirname "$0")" && pwd)/ipcam" CONDENSE="$(dirname "$0")/ipcam-condense.sh" MERGE="$(dirname "$0")/ipcam-merge.sh" # find raw recordings (not condensed, not motion.mkv), skip newest per camera RAWS=($(ls "$DIR"/*.mkv 2>/dev/null | grep -v _condensed | grep -v motion.mkv | sort)) if [ ${#RAWS[@]} -le 1 ]; then exit 0 fi # group by camera prefix, skip newest from each group declare -A NEWEST for f in "${RAWS[@]}"; do base=$(basename "$f") # extract camera name (everything before the timestamp) cam=$(echo "$base" | sed 's/_[0-9]\{8\}_[0-9]\{6\}\.mkv$//') NEWEST["$cam"]="$f" done for f in "${RAWS[@]}"; do base=$(basename "$f") cam=$(echo "$base" | sed 's/_[0-9]\{8\}_[0-9]\{6\}\.mkv$//') [ "$f" = "${NEWEST[$cam]}" ] && continue bash "$CONDENSE" "$f" done bash "$MERGE"