1768 fix email tokens for h264 storage (#1814)

* 1768-Fix-email-tokens-for-h264-storage Snapshot extraction impmemented

* 1768-Fix-email-tokens-for-h264-storage: Video file path fixed

* 1768-Fix-email-tokens-for-h264-storage: vframes 1 option added

* 1768-Fix-email-tokens-for-h264-storage: generateImage sub created

* 1768-Fix-email-tokens-for-h264-storage: Few typos fixed

* 1768-Fix-email-tokens-for-h264-storage: Code refactoring. EI1A and EIMA macros added
This commit is contained in:
Vladimir Poluyaktov 2017-05-16 10:04:42 -07:00 committed by Isaac Connor
parent d3115219d2
commit 2c9d21f59f
1 changed files with 73 additions and 17 deletions

View File

@ -420,6 +420,35 @@ sub generateVideo
return( 1 );
}
# Returns an image absolute path for given event and frame
# Optionally an analyse image path may be returned if an analyse image exists
# If neither capture nor analyse image exists it will try to extract a frame from .mp4 file if exists
# An empty string is returned if no one from methods above works
sub generateImage
{
my $event = shift;
my $frame = shift;
my $analyse = do { @_ ? shift : 0 }; # don't return analyse image by default
my $capture_image_path = sprintf("%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg", getEventPath($event), $frame->{FrameId});
my $analyse_image_path = sprintf("%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-analyse.jpg", getEventPath($event), $frame->{FrameId}) if $analyse;
my $video_path = sprintf("%s/%d-video.mp4", getEventPath($event), $event->{Id});
my $image_path = "";
# check if the image file exists. If the file doesn't exist and we use H264 try to extract it from .mp4 video
if ( $analyse && -r $analyse_image_path ) {
$image_path = $analyse_image_path;
} elsif ( -r $capture_image_path ) {
$image_path = $capture_image_path;
} elsif ( -e $video_path ) {
if ( !system("ffmpeg -y -v 0 -i ".$video_path." -vf 'select=gte(n\\,".$frame->{FrameId}."),setpts=PTS-STARTPTS' -vframes 1 -f image2 ".$capture_image_path) ) {
$image_path = $capture_image_path;
}
}
return $image_path;
}
sub uploadArchFile
{
my $filter = shift;
@ -623,7 +652,7 @@ sub substituteTags
}
# Do we need the image information too?
my $need_images = $text =~ /%(?:EPI1|EPIM|EI1|EIM)%/;
my $need_images = $text =~ /%(?:EPI1|EPIM|EI1|EIM|EI1A|EIMA)%/;
my $first_alarm_frame;
my $max_alarm_frame;
my $max_alarm_score = 0;
@ -684,17 +713,16 @@ sub substituteTags
$text =~ s/%EPIM%/$url?view=frame&mid=$event->{MonitorId}&eid=$event->{Id}&fid=$max_alarm_frame->{FrameId}/g;
if ( $attachments_ref && $text =~ s/%EI1%//g )
{
my $path = generateImage( $event, $first_alarm_frame);
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>sprintf(
"%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg",
getEventPath( $event ),
$first_alarm_frame->{FrameId}
)
path=>$path
}
);
}
}
if ( $attachments_ref && $text =~ s/%EIM%//g )
{
# Don't attach the same image twice
@ -702,19 +730,48 @@ sub substituteTags
|| ($first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId} )
)
{
my $path = generateImage( $event, $max_alarm_frame);
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>sprintf(
"%s/%0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg",
getEventPath( $event ),
$max_alarm_frame->{FrameId}
)
path=>$path
}
);
}
}
}
if ( $attachments_ref && $text =~ s/%EI1A%//g )
{
my $path = generateImage( $event, $first_alarm_frame, "analyse" );
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>$path
}
);
}
}
if ( $attachments_ref && $text =~ s/%EIMA%//g )
{
# Don't attach the same image twice
if ( !@$attachments_ref
|| ($first_alarm_frame->{FrameId} != $max_alarm_frame->{FrameId} )
)
{
my $path = generateImage( $event, $max_alarm_frame, "analyse");
if ( -e $path ) {
push( @$attachments_ref,
{
type=>"image/jpeg",
path=>$path
}
);
}
}
}
}
if ( $attachments_ref && $Config{ZM_OPT_FFMPEG} )
{
if ( $text =~ s/%EV%//g )
@ -1007,4 +1064,3 @@ sub executeCommand
}
return( 1 );
}