1 #!/bin/bash
2 # Process IP cam recordings: condense all but newest, merge into motion.mkv
3 # Intended to run periodically via cron
4 LOCK="/tmp/ipcam-process.lock"
5 exec 9>"$LOCK"
6 flock -n 9 || exit 0
7 DIR="$(cd "$(dirname "$0")" && pwd)/ipcam"
8 CONDENSE="$(dirname "$0")/ipcam-condense.sh"
9 MERGE="$(dirname "$0")/ipcam-merge.sh"
10 11 # find raw recordings (not condensed, not motion.mkv), skip newest per camera
12 RAWS=($(ls "$DIR"/*.mkv 2>/dev/null | grep -v _condensed | grep -v motion.mkv | sort))
13 14 if [ ${#RAWS[@]} -le 1 ]; then
15 exit 0
16 fi
17 18 # group by camera prefix, skip newest from each group
19 declare -A NEWEST
20 for f in "${RAWS[@]}"; do
21 base=$(basename "$f")
22 # extract camera name (everything before the timestamp)
23 cam=$(echo "$base" | sed 's/_[0-9]\{8\}_[0-9]\{6\}\.mkv$//')
24 NEWEST["$cam"]="$f"
25 done
26 27 for f in "${RAWS[@]}"; do
28 base=$(basename "$f")
29 cam=$(echo "$base" | sed 's/_[0-9]\{8\}_[0-9]\{6\}\.mkv$//')
30 [ "$f" = "${NEWEST[$cam]}" ] && continue
31 bash "$CONDENSE" "$f"
32 done
33 34 bash "$MERGE"
35