#!/bin/bash # Merge all *_condensed.mkv files into a rolling accumulator via stream copy # Usage: ipcam-merge.sh [output.mkv] # The accumulator file is prepended (oldest first), new condensed files appended in order DIR="$(cd "$(dirname "$0")" && pwd)" OUT="${1:-$DIR/ipcam/motion.mkv}" TMP=$(mktemp -d) trap "rm -rf $TMP" EXIT # collect condensed files sorted by name (timestamp order) CONDENSED=($(ls "$DIR"/ipcam/*_condensed.mkv 2>/dev/null | sort)) if [ ${#CONDENSED[@]} -eq 0 ]; then echo "no condensed files to merge" exit 0 fi # build concat list: existing accumulator first, then new condensed files if [ -f "$OUT" ]; then echo "file '$OUT'" > "$TMP/concat.txt" fi for f in "${CONDENSED[@]}"; do echo "file '$f'" >> "$TMP/concat.txt" done echo "merging ${#CONDENSED[@]} condensed files" ffmpeg -y -loglevel error -f concat -safe 0 -i "$TMP/concat.txt" -c copy "$TMP/merged.mkv" mv "$TMP/merged.mkv" "$OUT" # remove merged condensed files for f in "${CONDENSED[@]}"; do rm "$f" done echo "wrote $OUT ($(du -h "$OUT" | cut -f1))"