Record High-res H264 streams 24/7 with low CPU Load

If you've made a patch to quick fix a bug or to add a new feature not yet in the main tree then post it here so others can try it out.
tuxmos
Posts: 20
Joined: Fri Oct 30, 2015 11:13 am

Re: Record High-res H264 streams 24/7 with low CPU Load

Post by tuxmos » Wed Mar 04, 2020 7:15 pm

Oh. My cameras actually have no audio either, but offer in the encoder settings to encode an audio track in the stream.

There is still a difference.
If I start zmrecord.sh <MonitorId> alone in the console, the system load on a core is around 3% and the script does not even appear in HTOP, only ffmeg.
If I start the script for the same monitor as service, the system load on four cores is approx. 10% and the script can be seen as in my screenshot.

Could this be that version 0.7 accesses the database much more frequently than 0.3?
Attachments
Screenshot 2.jpg
This screenshot from HTOP with all my streams via zmrecord.sh <MonitorId>
But obviously there are no entries in the event database
Screenshot 2.jpg (347.56 KiB) Viewed 1031 times
Screenshot.jpg
This screenshot from HTOP with all my streams via zmrecord as service
Screenshot.jpg (339.25 KiB) Viewed 1033 times

evolotion
Posts: 46
Joined: Mon Aug 01, 2016 9:03 pm

Re: Record High-res H264 streams 24/7 with low CPU Load

Post by evolotion » Wed Apr 08, 2020 8:26 pm

Hi, my mysql database has a password (as I believe they all do? on a normal ZM installation)

I cannot get the script to work even if I put a -u and --password= in every mysql call in the script in plaintext .

Yes I realise its dumb to put cleartext passwords in a script, but wanted to see if this worked.

Im not a linux noob but i am also not great with scripting and am an absolute 100% noob with mysql.

If this is an intentional barrier to entry, no hassle will keep an eye on this and see if it makes it into the main zoneminder setup, Tho if someones inclined to give me a pointer Id sure appreciate it.

cheers,
denis.

evolotion
Posts: 46
Joined: Mon Aug 01, 2016 9:03 pm

Re: Record High-res H264 streams 24/7 with low CPU Load

Post by evolotion » Thu Apr 09, 2020 7:16 pm

ok, got it working by running by sudo -u www-data zmrecord.sh 7

and changing all the mysql lines to include login details

for example mysql -NBqr -u zmuser -pzmpass zm


anyways it records the camera and adds events to the log and the cpu usage is next to nothing, my concern before I add it as a service and set it up across my hi-res treams is this :

Code: Select all

mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
mysql: [Warning] Using a password on the command line interface can be insecure.
/usr/local/bin/zmrecord.sh: line 283: 0: Permission denied
mysql: [Warning] Using a password on the command line interface can be insecure.
line 283 is marked with the * .. not really sure whats happening here but when the script is running, but function changes to capturing, it shows the correct frame rate, but does not show the bandwidth.

Code: Select all

# Keep the monitor status up to date so fps etc can be viewed in the zm console
function updateMonitorStatus() {
        MONITOR_ID="$1"
        STATS_FILE="$2"
        SHUTTINGDOWN="$3"
        STATUS="NotRunning"
        FPS=0
        BW=0
        if ! [ "$SHUTTINGDOWN" = "1" ]; then
                if [ -f "$STATS_FILE" ]; then
                        IFS=' ' read -r TS FPS BW FRAME <<< `cat "$STATS_FILE"`
                        if  [ -z "${TS//[0-9]}" ] && [ -n "$TS" ]; then
                                AGE=$(($(date +%s)-TS))
                        else
                                AGE=1000
                        fi
                        if [ "$AGE" -lt 4 ]; then
                                STATUS="Connected"
                        fi
                fi
        fi
        # Calc CaptureBandwith in the same way as ZM in zm_monitor.cpp:
        #       unsigned int new_capture_bandwidth = (new_camera_bytes - last_camera_bytes)/(now-last_fps_time);
        # Unfortunately, BASH can't trap a divide by zero so we have to check
        # Get Filesize for Bandwidth calc.  Ffmpeg buffers quite a lot so see if the file has changed
        # size before calculating the Bandwidth.
        FILESIZE=`stat -c%s "$NEW_VIDEO_PATH" 2>/dev/null`
        if [ ${FILESIZE:-0} -gt ${LAST_FILESIZE:-0} ]
        then
                ELAPSED_SECONDS=$(($TS - $LAST_FPS_TIME))
*                if [ $ELAPSED_SECONDS > 0 ]
                then
                        FILESIZE_INCREASE=$(( ${FILESIZE:-0} - ${LAST_FILESIZE:-0} ))
                        LAST_BW=$(( $FILESIZE_INCREASE / $ELAPSED_SECONDS ))
                        LAST_FPS_TIME=$TS
                        LAST_FILESIZE=$FILESIZE
                fi
        fi
        SQL="   REPLACE INTO Monitor_Status (MonitorId, Status, CaptureFPS, CaptureBandwidth) 
                VALUES 
                ('$MONITOR_ID','$STATUS', '$FPS', ${LAST_BW:-0})"
        echo $SQL | mysql -NBqr -u zmuser -pzmpass zm
}
This is absolutely superb btw, thankyou!

EDIT :- ok running the script as root runs perfect and updates the bandwidth. superb :)

calmor15014
Posts: 18
Joined: Mon May 09, 2016 4:12 pm

Re: Record High-res H264 streams 24/7 with low CPU Load

Post by calmor15014 » Sat May 23, 2020 2:08 am

Thanks a ton for this.

I had to make a number of tweaks to get this to work on a remote MySQL installation, but was able to get it to run successfully with only recurring "Using a password on the command line interface can be insecure" warnings. It's pretty hack-ish but I'd be happy to share if you like.

I originally wanted to read the zm conf.d files but I settled for just adding some optional config variables at the head. I obviously couldn't test a local server, but the remote one works.

Post Reply

Who is online

Users browsing this forum: No registered users and 11 guests