Script that moves snapshots from a Dahua camera to an easier to view network folder.

biggen

Known around here
Joined
May 6, 2018
Messages
2,563
Reaction score
2,837
At home, I run a simple one camera Dahua DH-SD22204UE-GN setup. It records 24x7x365 to a Linux (via NFS) machine/NAS. I then use the terrible Dahua DSS client/server combo on my desktop/laptop if I ever need to view footage. I run a full fledged Blue Iris server in my business with over a dozen cams but didn't really want to go that route at home since I'm using just a single camera.

What I've found wanting at home, though, is I'd like to have some type of "notification" and snapshot of the trip wire events I have configured in the camera that happen during the night. That way if I ever do need to go review the footage, at least I know the time that I'm looking for because I have the accompanying snapshot. So now instead of looking around and randomly fast forwarding through 8+ hours of nighttime footage I can go right to the event based on the timestamp in the snapshot. The main problem is the camera has limited options for notifying you a snapshot took place. Perhaps the DSS software has built-in options for notifications, but it's such a terrible piece of bloated software I don't even like using it if I don't have to so I never attempted to use it for this purpose. The camera itself does have email capability but I didn't want to have the camera email me when a tripwire event/snapshot occurs as it just fills up my phone with unneeded spam.

So I wrote a script that executes the following day that copies all snapshots taken from tripwire induced events during the previous night out of the camera's recording NFS folder and into another folder on the Linux machine that is then shared out via Samba. I can then connect to that folder from any Windows Computer in my home via File Explorer and view the snapshots right from there. The entire process is automated so all i do is grab breakfast, fire up the laptop, browse to the shared snapshot folder, and have a quick look and see what the camera took snapshots of the night before. Whole process takes less than 30 seconds of quickly clicking through the snapshots with the keyboard/mouse. I wanted to share the script I've been using for a year in case anyone else had a similar need:

Bash:
#!/bin/bash

# Sets variable to yesterday's date so we can get into the correct directory we need.
YEST=/mnt/cam_recordings/frontcam/5MF50Z6BPDC5286/$(date -d '-1 day' '+%Y-%m-%d')

# Renames all of yesterday's .jpgs in cam snapshot folders
# to creation time since the camera sometimes duplicates snapshot names.
files=($(find $YEST -type f -name '*.jpg'))
for f in "${files[@]}"; do
    mv -f -- "$f" "${f%/*}/$(date -r "$f" +%Y-%m-%d_%H-%M-%S-%N).jpg"
done

# Copies renamed .jpgs from cam snapshot directories
# into a SAMBA share.  A new directory with yesterday's
# date is created for easy viewing.
find $YEST -name \*.jpg | rsync -av --files-from - --no-relative / /mnt/camsnaps/$(date -d '-1 day' '+%Y-%m-%d')
Comments in the script are self explanatory but essentially:
  1. First we set a variable that contains the camera snapshot location for yesterdays_date. This is the native folder structure the camera uses to save recordings/snapshots in. It's saved in /storage_location/{camera_serial_number}/{date_in_YEAR_MONTH_DAY_format}.
  2. Then we recursively move through the subfolders of that location variable we assigned in step one renaming all .jpgs. This must be done prior to step 3 below. They are renamed to the date/time they were created and we also add a random 9 digit number to the end of the file name to make sure we don't ever get duplicates. We have to do all this wizardry because the camera has a very convoluted saving scheme when taking snapshots in its native format. It saves snapshots in a complicated folder hierarchy and then finally writes a snapshot name that is meaningless and could easily be duplicated with another .jpg snapshot in a different folder. During my testing, it did create duplicate names numerous times throughout all its subfolders so we must rename the .jpgs to prevent this from happening prior to copying the .jpgs to a different location.
  3. Finally, we use rsync to copy all the renamed .jpgs into another folder (named /mnt/camsnaps/{Yesterdays_Date}) that is shared out via Samba so Windows clients can view it easily in File Explorer.
Finally, we setup in crontab two simple lines in order to have this script run once a day. The first line kicks the script off daily at 5am while the other line deletes all folders/.jpg out of the shared Samba folder after 30 days has elapsed. That gives me 30 days retention of snapshots. 30 days worth of .jpg snapshots amounts to roughly 250MB of data. Basically nothing in this day and age. I could probably have years worth of retention if I really wanted to since I'm using drives in my Linux NAS that are 6TB+ for storage.

Bash:
# Runs camsnap rsync script daily at 5am
0 5 * * * /home/files/rsync_script.sh

# Deletes camsnap folders older than 30 days. Runs at 6am daily
0 6 * * * find /mnt/camsnaps/ -type d -mtime +30 -exec rm -rf {} +
Here is what I see when I fire up the desktop/laptop in the morning when I want to check what the camera took snapshots of the previous night:





Any way, I wanted to share this with the community in case anyone else finds it frustrating to know you have tons of footage but not a great way to wade through it all if something did happen. All my snapshots work off the basis of the tripwires in the Dahua camera setup and I've found them to work so much better than motion detection. I only have the tripwires/snapshots active during the hours of 10pm to 4am. I'm always recording 24x7x365 but only need tripwire/snapshot taken during this time as this is when the mischievousness really begins to shine in my community.

The one limitation the script has is that snapshots are taken by the camera from 10pm to 4am. Well the script works on the basis of {yesterdays_date} when performing the renaming and copying. So if any snapshot happened between 12am - 4am, then that isn't yesterday. That is today. So even though that event happened last night, the script won't see it since that wasn't on {yesterdays_date}. All this means is that you have to wait 24 hours for the script to rename/move it on the next day it runs. Its not a big deal and it would take a lot more coding logic to correct this behavior which I'm too lazy to learn how to do!
 
Last edited:
Top