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.
Post Reply
russell_i_brown
Posts: 13
Joined: Wed Mar 18, 2009 9:46 am
Location: Peterborough, England
Contact:

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

Post by russell_i_brown » Wed Sep 19, 2018 11:53 am

Even in RECORD mode with H264 passthrough enabled, Zoneminder extracts frames from the stream (confirmed by iconner in this thread). This obviously creates a system load and a significant one when there's a number of high-res cameras involved.

The attached script uses ffmpeg to record the high-res H264 stream from cameras in N second chunks and inserts event records into the Zoneminder database for every chunk so that they can be accessed and managed via the normal interface.

For my setup, I'm recording high-res streams from six IP cameras 24/7 in ten minute chunks and doing MODECT on the low-res streams. This gives me motion events on the low-res and I can go and look at the 3840x2160 mp4 for the appropriate time if needed. This does mean you end with two cameras (low-res MODECT and high-res NONE) in the zoneminder console but hey-ho, I can live with that.

To set this up, firstly get a Monitor for your camera working in MONITOR mode at high-res (the script extracts the Height, Width, Path and StoragePath for a given Monitor from the DB).

Once working, set the new Monitor to 'NONE' so Zoneminder isn't running zmc on it.

Install the attached script (zmrecord.sh) in /usr/local/bin with execute perms.

Test it by running "zmrecord.sh <Monitor_Id>" and you should see it recording into <your_monitors_storage_path>/Monitor_Id/Rec-XXXXX. As each chunk is finished (set the chunk size in seconds by changing RECORD_TIME at the top of the script) , the script moves the mp4 to the right place in your storage, tells Zoneminder's DB all about it and starts recording the next chunk.

Copy the attached systemd (Ugh!) zmrecord@.service script to /lib/systemd/system (or wherever your systemd scripts live) and enable it for each camera with

Code: Select all

systemctl enable zmrecord@<MonitorId>.service
For multiple cameras (for example 8, 12 and 14) you would do:

Code: Select all

systemctl enable zmrecord@8.service zmrecord@12.service zmrecord@14.service
This will tell your system to fire up the ffmpeg recording once it's booted to multi-user mode. You can, of course, start or stop the zmrecord process for individual cameras through the normal systemctl commands:

Code: Select all

systemctl start zmrecord@12.service
      or
systemctl stop zmrecord@12.service
Here's a 'top' on my system recording high-res streams from six cameras with zmc/zma running on various low-res streams (the highest load are some old Axis cameras pulling mjpgs). The ffmpegs reading the high-res hardly feature whereas before, trying to record these high-res streams through Zoneminder, my system was on it's knees.

top.png
top.png (42.17 KiB) Viewed 828 times
and here's a region of my Zoneminder console showing the 'None' monitors gathering events.

zm.png
zm.png (58.18 KiB) Viewed 828 times

Enjoy. Comments & suggestions welcome.


Note: the script will only work with Storage scheme 'Medium' (it's what I use).
Note2: You'll need to install the 'mediainfo' package as the script uses it to extract the frame count from the mp4.
Attachments
zmrecord.zip
(1.84 KiB) Downloaded 14 times

russell_i_brown
Posts: 13
Joined: Wed Mar 18, 2009 9:46 am
Location: Peterborough, England
Contact:

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

Post by russell_i_brown » Fri Sep 21, 2018 7:24 am

Small update to the script - I screwed up setting the StorageId in the event. This will only effect you if you're using multiple storage areas.

Attached is version 0.3

There's also an issue with zmaudit getting enthusiastic about clearing out the events as there are no associated Frame records (Frames don't exist as we're recording native H264).

I need to do a little thinking about the right solution so for now I've just set AUDIT_MIN_AGE to 604800 (a week) while I come up with a sensible solution.
Attachments
zmrecord.zip
(1.86 KiB) Downloaded 29 times

Eiserfeyer
Posts: 1
Joined: Thu Apr 13, 2017 9:17 am

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

Post by Eiserfeyer » Thu Oct 11, 2018 6:30 pm

Thanks for your work. Your thoughts seem to have gone in the same direction as mines... But you seem to have the solution :mrgreen:
I will try to check this script at weekend an will let you know if it also works for me.

tigerA
Posts: 7
Joined: Thu Sep 27, 2018 6:35 am

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

Post by tigerA » Wed Oct 17, 2018 12:55 pm

From my own experience.
I set: Monitor -> Source -> Ffmpeg. Protocol RTSP.
In Storage tab: Save JPEGs -> Disabled, VideoWriter - > H264 Camera Passtrough

Pedulla
Posts: 92
Joined: Thu Nov 27, 2014 11:16 am
Location: Portland, Or

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

Post by Pedulla » Mon Nov 12, 2018 9:29 pm

Are you getting audio with this too?

Post Reply

Who is online

Users browsing this forum: No registered users and 3 guests