Script to download recording from Dahua NVR by channle and time

d5775927

Getting comfortable
Dec 11, 2019
404
328
Israel
I have Dahua NVR (DHI-NVR4108-8P-4KS2), sometimes I want to download a video file in MP4 format, for a certain IPC in a given time interval.
I've created this bash script:
Bash:
#!/bin/bash
ipc_name="$1"
start="$2"
end="$3"

function usage() {
        echo 'rec_download <ipc name> <start time> <end time>'
        echo 'example:'
        echo -e "\trec_download north '2023-12-31 00:11:22' '2023-12-31 00:12:33'"
        echo "or:"
        echo -e "\trec_download south '2023-12-31 00:11:22' '2023-12-31 00:12:33'"
}

function validate_args() {
        if [[ ! $start =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
                echo "invalid start date, expected date format: yyyy-mm-dd hh:mm:ss"
                exit 2
        fi

        if [[ ! $end =~ ^[0-9]{4}-[0-9]{2}-[0-9]{2}\ [0-9]{2}:[0-9]{2}:[0-9]{2}$ ]]; then
                echo "invalid end date, expected dare format: yyyy-mm-dd hh:mm:ss"
                exit 3
        fi

        if [ -z "$ipc_index" ]; then
                echo "invalid ipc name, choose one of:"
                for key in "${!ipcs[@]}"; do echo -e "\t${key}"; done
                exit 3
        fi

}

declare -A ipcs=(["south"]="1" ["north"]="4")
if [ "$#" -lt 3 ]; then
        usage
        exit 1
fi
ipc_index="${ipcs["$ipc_name"]}"
validate_args
# encode start + end time - replace space with '%20'
start=$(echo "$start" | sed -e 's/ /%20/g')
end=$(echo "$end" | sed -e 's/ /%20/g')
url="http://192.168.7.7/cgi-bin/loadfile.cgi?action=startLoad&channel=$ipc_index&startTime=$start&endTime=$end&subtype=0"
echo "$url"
curl -o /tmp/rec.dav --digest --user user:password "$url"
ffmpeg -y -i /tmp/rec.dav -c:v libx264 -crf 24 /tmp/rec.mp4
echo 'result in: /tmp/rec.mp4'

This works well, however, the exported video sometimes contains a few frames from other IPC (other channel).
When I export videos from PSS, the exported video is perfect - only showing one IPC.
Any idea what i'm doing wrong? (I know about blue Iris, but I don't want it, since I have a NVR).