Storing DVR-records on your server & Removing DVR-records

Discussion in 'Programming/Scripts' started by Alex Mamatuik, Dec 9, 2022.

  1. Alex Mamatuik

    Alex Mamatuik Member

    Sometimes it happens, when it is much better to keep your records of the surveillance system remotely.
    (and i have noticed, that HowToForge still doesn't have a such tutorial)

    My brief variant:
    1. cd /
    2. mkdir REPOSITORY && cd REPOSITORY
    3. mkdir DVR && cd DVR

      Creating script, which tells to record segments, splitting them hourly.
    4. (bash script: dvr_rec.sh) nano dvr_rec.sh
      #!/bin/bash
      ######### Lock checker ###########

      myName="`echo $0 | awk '{print $NF}' FS='/'`"

      lockDir="/var/lock/"

      lockFile=$lockDir$myName.pid

      currentPID=$$

      oldPID="`cat $lockFile`"

      oldderExist=` kill -0 $oldPID 2>/dev/null ; echo $? `

      if [ "$oldderExist" == "0" ]; then echo "An Other Instance Is Running...! PID:$oldPID" ; exit ; else echo $currentPID > $lockFile ; fi

      ######### Lock checker ###########

      while true
      do
      if [ -z "$1" ]; then
      IP_ADDRESS=<my_opened_ip_to_connect_with_camera>
      USER=<name>;
      PASS=<password>
      RTSPPORT=<enabled_port>

      else
      IP_ADDRESS=$1
      fi

      # FFMPEG command for creating segments from RTSP stream
      ffmpeg -i rtsp://${USER}:${PASS}@${IP_ADDRESS}:${RTSPPORT}/trackID=1 -map 0 \
      -codec:v libx264 -crf 21 -acodec copy -b:v 800k -b:a 96k -preset fast -tune zerolatency \
      -f segment \
      -strftime 1 -segment_time 3600 -segment_format mp4 "/REPOSITORY/DVR/records/%Y_%m_%d-%Hh%Mm%Ss.mp4"
      sleep 0.5
      done


      !verify, that you have a such path: /REPOSITORY/DVR/records (or create your own)

    5. (making service) nano dvr_rec.service
      [Unit]
      Description=DVR records

      [Service]
      ExecStart=/REPOSITORY/DVR/dvr_rec.sh
      Restart=on-failure

      [Install]
      WantedBy=multi-user.target
    6. chmod +x /REPOSITORY/DVR/dvr_rec.sh
    7. ln -s /REPOSITORY/DVR/dvr_rec.service /lib/systemd/system/dvr_rec.service
    8. systemctl enable dvr_rec.service
    9. systemctl start dvr_rec.service
    10. systemctl status dvr_rec.service

      The most important part - to remove staled data; your storage has not an unlimited capacity.
    11. Script - erasing not needed files
      nano remove_dvr_records.sh:
      #!/bin/bash

      while true
      do
      LOCATION=/REPOSITORY/DVR/records
      REQUIRED_FILES=*.mp4

      find $LOCATION -name $REQUIRED_FILES -type f -mtime +2 -delete
      sleep 21600
      done


    12. remove_dvr_rec.service
      [Unit]
      Description=Remove old DVR records

      [Service]
      ExecStart=/REPOSITORY/DVR/remove_dvr_records.sh
      Restart=on-failure

      [Install]
      WantedBy=multi-user.target
     

Share This Page