Skip to content

Instantly share code, notes, and snippets.

@Critter
Forked from kdvlr/wyze_merge.sh
Created January 22, 2020 20:43
Show Gist options
  • Save Critter/c9844f208c6348f2b4a7c2ec3cd3b9cb to your computer and use it in GitHub Desktop.
Save Critter/c9844f208c6348f2b4a7c2ec3cd3b9cb to your computer and use it in GitHub Desktop.
Wyze Merge
#!/bin/bash
# Config
echo "---------Starting Wyze Merge at $(date)-----------"
SRC_DIR=/cameras/WyzeCams
# Assumes there is a camera specific folder with record folder inside it.
# E.g. SRC_DIR/<Camera>/record
TGT_DIR=/cameras/Archive
TMP_DIR=/tmp/wyze_merge
echo "Source Directory is $SRC_DIR"
echo "Target Directory is $TGT_DIR"
echo "Temp Directory is $TMP_DIR"
# Delete previous stuff
rm -rf $TMP_DIR
mkdir -p $TMP_DIR
# Find a list of directories for WyzeCams. Assumes recordings are stored under <WyzeCam_Name>\Record
find $SRC_DIR -path "*/record" >$TMP_DIR/dir_list.txt
echo "The List of cameras are"
cat $TMP_DIR/dir_list.txt
# Loop through and merge files
while IFS="" read -r p || [ -n "$p" ]
do
CAM_NAME=$(echo $p| awk -F '/record' '{printf "%s\n", $(NF-1) }'| awk -F '/' '{printf "%s\n", $(NF) }')
echo "Processing camera: $CAM_NAME"
cd $p
echo "In directory $p"
mkdir -p $TGT_DIR/$CAM_NAME
echo "Created $TGT_DIR/$CAM_NAME"
for day in $(find . -maxdepth 1 -type d | cut -c 3- | head -n -1 | tail -n +2)
do
cd $day
echo "Processing files in $day"
mkdir -p $TMP_DIR/$CAM_NAME/
find $PWD -name *.mp4 >$TMP_DIR/$CAM_NAME/$day.lst
sed -i -e 's/^/file /' $TMP_DIR/$CAM_NAME/$day.lst
ffmpeg -y -hide_banner -loglevel panic -safe 0 -nostdin -nostats -f concat -i $TMP_DIR/$CAM_NAME/$day.lst -c:v copy -c:a aac $TGT_DIR/$CAM_NAME/$day.mp4
echo "Created $TGT_DIR/$CAM_NAME/$day.mp4"
file_size=$(du $TGT_DIR/$CAM_NAME/$day.mp4 | cut -f 1)
if [ $file_size -gt 1000000 ]; then
cd ..
rm -rf $day
echo "Deleted $day folder"
else
echo "Merged file is small. Did not delete $day folder. Please review and delete manually."
cd ..
fi
done
done < $TMP_DIR/dir_list.txt
echo "Deleting files older than 30 days"
find $TGT_DIR/* -mtime +30 -exec rm {} \;
echo "---------Ended Wyze Merge at $(date)-----------"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment