Filter to convert Events to h264 video and save to custom location

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
Justru
Posts: 3
Joined: Fri Mar 31, 2017 1:53 pm

Filter to convert Events to h264 video and save to custom location

Post by Justru »

Since ZoneMinder saves events as JPG pictures taking a lot of disk space, I came up with a filter to automatically convert old events to H264 video files, move them to a custom location (another disk) and delete the original JPG events.

The solution is for ZoneMinder 1.30.0 and only needs 2 lines of code modification in the original ZM Event.pm file (to save videos with date in the name) and a small bash script to move the files. The idea is based on the post by MartinBty that was updated and improved for current ZM version.

The filter screenshot is below. To make it work, follow my guide.

ArchiveVideo filter
ArchiveVideo filter
zm_video_filter.PNG (22.65 KiB) Viewed 10071 times

1. Edit original Event.pm file (mine is located in /usr/share/perl5/ZoneMinder directory).

Find the following piece of code, add one line and change one line as shown below.

Original code

Code: Select all

        elsif ( $size )
        {
                my $file_size = 'S'.$size;
                push( @file_parts, $file_size );
        }
       my $video_file = "$video_name-".$file_parts[0]."-".$file_parts[1].".$format";
        if ( $overwrite || !-s $video_file )
        {
                Info( "Creating video file $video_file for event $self->{Id}\n" );
Modified code

Code: Select all

        elsif ( $size )
        {
                my $file_size = 'S'.$size;
                push( @file_parts, $file_size );
        }
        ( my $video_starttime = $self->{StartTime} ) =~ s/\D//g;     # new line
        my $video_file = "$video_name-".$video_starttime."-".ceil($self->{FullLength})."sec.$format";   # modified line
        if ( $overwrite || !-s $video_file )
        {
                Info( "Creating video file $video_file for event $self->{Id}\n" );

2. Create a new bash script file movevideo.sh with the following content and put it in web server accessible directory. I put mine in /usr/share/zoneminder/www/tools/movevideo.sh
The script below will move the videos to /archive/video directory with dates sub-directories. Make sure it exists and has write permissions for web server user.

user@server:/$ cat /usr/share/zoneminder/www/tools/movevideo.sh

Code: Select all

#!/bin/bash

LOG=/var/log/zm/movevideo.log
EXT=mp4

if [ "$1" != "" ]
then
    for i in $(ls $1/*.$EXT)
    do
        DEST=$(sed 's/[^0-9]\+[0-9]/\/archive\/video/' <<< $1)
        DEST=${DEST:0:-8}
        echo  "`date '+%F %T'` Moving from $i to $DEST" >> $LOG
        mkdir -p $DEST
        mv -n $i $DEST >> $LOG 2>&1
    done
else
    echo  "`date '+%F %T'` ERROR: No input provided" >> $LOG
    echo "ERROR: No input provided"
    exit 1
fi
3. Make sure your ZoneMinder options are the following:
OPT_FFMPEG: checked
PATH_FFMPEG: /usr/bin/avconv (or ffmpeg location)
FFMPEG_OUTPUT_OPTIONS: -c:v h264 -crf 18 (this creates high quality h264 video)
FFMPEG_FORMATS: mpg mpeg wmv asf avi mp4* mov swf 3gp** (mp4 is added and checked as default).

4. Finally, create a new ZoneMinder filter that will find old events, convert them to H264 video, move to your custom location (/archive/video in my case):

Filter name: ArchiveVideo (background)
Disk percent: greater than or equal to 90
and Date: less than -3 months
Create video for all matches: checked
Execute command on all matches: /usr/share/zoneminder/www/tools/movevideo.sh
Delete all matches: checked

Save your filter and watch logs how it works!
trumee
Posts: 69
Joined: Tue Mar 08, 2011 3:33 pm

Re: Filter to convert Events to h264 video and save to custom location

Post by trumee »

I had to do following change in movevideo script otherwise all the events were getting put in video directory.

Code: Select all

 DEST=$(sed 's/[^0-9]\+/\/archive\/video/' <<< $1)
 
david786
Posts: 1
Joined: Tue Aug 07, 2018 10:37 am

Re: Filter to convert Events to h264 video and save to custom location

Post by david786 »

Hi i am getting following error

Code: Select all

zmfilter		1029	ERR	Command '/usr/share/zoneminder/www/tools/movevideo.sh /usr/share/zoneminder/www/events/1/18/08/07/15/49/31' exited with status: 72057594037927935	zmfilter.pl
i edit Ext=avi as video saved as avi format
Justru
Posts: 3
Joined: Fri Mar 31, 2017 1:53 pm

Re: Filter to convert Events to h264 video and save to custom location

Post by Justru »

david786 wrote: Tue Aug 07, 2018 10:58 am Hi i am getting following error

Code: Select all

zmfilter		1029	ERR	Command '/usr/share/zoneminder/www/tools/movevideo.sh /usr/share/zoneminder/www/events/1/18/08/07/15/49/31' exited with status: 72057594037927935	zmfilter.pl
i edit Ext=avi as video saved as avi format
EXT=avi is case sensitive
Also make sure you set ZM option according your format like below
FFMPEG_FORMATS: mpg mpeg wmv asf avi* mov swf 3gp**

You can also check the log for the issue root cause
/var/log/zm/movevideo.log
mavi
Posts: 3
Joined: Tue Nov 07, 2017 4:21 pm

Re: Filter to convert Events to h264 video and save to custom location

Post by mavi »

Exactly what I was looking for thanks!
trumee
Posts: 69
Joined: Tue Mar 08, 2011 3:33 pm

Re: Filter to convert Events to h264 video and save to custom location

Post by trumee »

Anybody tried this on ZM-1.32?
alabamatoy
Posts: 349
Joined: Sun Jun 05, 2016 2:53 pm

Re: Filter to convert Events to h264 video and save to custom location

Post by alabamatoy »

What does this do to CPU load?

I haven't tried this, but it certainly looks more efficient, simpler and easier to manage than my horrible asynchronous ffmpeg cron ugliness!
Post Reply