2019-09-24 00:54:27 +08:00
|
|
|
#!@PERL_EXECUTABLE@ -wT
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# ZoneMinder Video Creation Script, $Date$, $Revision$
|
2008-07-25 17:48:16 +08:00
|
|
|
# Copyright (C) 2001-2008 Philip Coombes
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU General Public License
|
|
|
|
# as published by the Free Software Foundation; either version 2
|
|
|
|
# of the License, or (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program; if not, write to the Free Software
|
2016-12-26 23:23:16 +08:00
|
|
|
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2005-12-16 18:05:29 +08:00
|
|
|
#
|
|
|
|
# ==========================================================================
|
2015-04-16 13:16:32 +08:00
|
|
|
|
|
|
|
=head1 NAME
|
|
|
|
|
|
|
|
zmvideo.pl - ZoneMinder Video Creation Script
|
|
|
|
|
|
|
|
=head1 SYNOPSIS
|
|
|
|
|
2017-07-15 03:55:08 +08:00
|
|
|
zmvideo.pl [ -e <event_id>,--event=<event_id> | --filter_name=<filter name> | --filter_id=<filter id> ]
|
|
|
|
[--concat=filename]
|
|
|
|
[--format <format>]
|
|
|
|
[--rate=<rate>]
|
|
|
|
[--scale=<scale>]
|
|
|
|
[--fps=<fps>]
|
|
|
|
[--size=<size>]
|
|
|
|
[--overwrite]
|
2015-04-16 13:16:32 +08:00
|
|
|
|
|
|
|
=head1 DESCRIPTION
|
|
|
|
|
|
|
|
This script is used to create MPEG videos of events for the web pages
|
|
|
|
or as email attachments.
|
|
|
|
|
|
|
|
=head1 OPTIONS
|
2015-12-18 03:36:40 +08:00
|
|
|
-c[=filename], --concat[=filename] - When creating videos for multiple events, create a concatenated video as well.
|
|
|
|
- If not specified, filename is taken from filter name.
|
|
|
|
-e<event_id>, --event=<event_id> - What event to create the video for
|
|
|
|
--filter_name=<filter name> - The name of a saved filter to generate a video for all events returned by it.
|
|
|
|
--filter_id=<filter id> - The id of a saved filter to generate a video for all events returned by it.
|
|
|
|
-f<format>, --format=<format> - What format to create the video in, default is mpg. For ffmpeg only.
|
|
|
|
-r<rate>, --rate=<rate> - Relative rate, 1 = realtime, 2 = double speed, 0.5 = half speed etc.
|
|
|
|
-s<scale>, --scale=<scale> - Scale, 1 = normal, 2 = double size, 0.5 = half size etc.
|
|
|
|
-F<fps>, --fps=<fps> - Absolute frame rate, in frames per second
|
|
|
|
-S<size>, --size=<size> - Absolute video size, WxH or other specification supported by ffmpeg
|
|
|
|
-o, --overwrite - Whether to overwrite an existing file, off by default.
|
|
|
|
-v, --version - Outputs the currently installed version of ZoneMinder
|
2015-04-16 13:16:32 +08:00
|
|
|
|
|
|
|
=cut
|
2005-12-16 18:05:29 +08:00
|
|
|
use strict;
|
|
|
|
use bytes;
|
|
|
|
|
|
|
|
# ==========================================================================
|
|
|
|
#
|
|
|
|
# You shouldn't need to change anything from here downwards
|
|
|
|
#
|
|
|
|
# ==========================================================================
|
|
|
|
|
2009-06-08 17:11:56 +08:00
|
|
|
@EXTRA_PERL_LIB@
|
2005-12-16 18:05:29 +08:00
|
|
|
use ZoneMinder;
|
2015-12-18 03:36:40 +08:00
|
|
|
require ZoneMinder::Filter;
|
|
|
|
require ZoneMinder::Event;
|
2005-12-16 18:05:29 +08:00
|
|
|
use DBI;
|
2015-04-16 13:42:56 +08:00
|
|
|
use autouse 'Data::Dumper'=>qw(Dumper);
|
2007-12-30 02:24:26 +08:00
|
|
|
use POSIX qw(strftime);
|
2005-12-16 18:05:29 +08:00
|
|
|
use Getopt::Long qw(:config no_ignore_case );
|
2015-12-18 03:36:40 +08:00
|
|
|
use Cwd;
|
2015-04-16 13:16:32 +08:00
|
|
|
use autouse 'Pod::Usage'=>qw(pod2usage);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
|
|
|
$| = 1;
|
|
|
|
|
2016-03-12 05:28:16 +08:00
|
|
|
$ENV{PATH} = '/bin:/usr/bin:/usr/local/bin';
|
2005-12-16 18:05:29 +08:00
|
|
|
$ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL};
|
|
|
|
delete @ENV{qw(IFS CDPATH ENV BASH_ENV)};
|
|
|
|
|
2011-06-21 17:19:10 +08:00
|
|
|
logInit();
|
2005-12-21 01:03:33 +08:00
|
|
|
|
2005-12-16 18:05:29 +08:00
|
|
|
my $event_id;
|
2015-12-18 03:36:40 +08:00
|
|
|
my $concat_name;
|
|
|
|
my $filter_name;
|
|
|
|
my $filter_id;
|
2005-12-16 18:05:29 +08:00
|
|
|
my $format = 'mpg';
|
|
|
|
my $rate = '';
|
|
|
|
my $scale = '';
|
|
|
|
my $fps = '';
|
|
|
|
my $size = '';
|
|
|
|
my $overwrite = 0;
|
2015-01-07 22:11:07 +08:00
|
|
|
my $version = 0;
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2015-04-16 13:16:32 +08:00
|
|
|
GetOptions(
|
2017-06-23 21:58:54 +08:00
|
|
|
'concat|c:s' =>\$concat_name,
|
2015-12-18 03:36:40 +08:00
|
|
|
'event|e=i' =>\$event_id,
|
|
|
|
'filter_name=s' =>\$filter_name,
|
2017-06-23 21:58:54 +08:00
|
|
|
'filter_id=i' =>\$filter_id,
|
2015-04-16 13:16:32 +08:00
|
|
|
'format|f=s' =>\$format,
|
|
|
|
'rate|r=f' =>\$rate,
|
|
|
|
'scale|s=f' =>\$scale,
|
|
|
|
'fps|F=f' =>\$fps,
|
|
|
|
'size|S=s' =>\$size,
|
|
|
|
'overwrite' =>\$overwrite,
|
|
|
|
'version' =>\$version
|
2017-06-23 21:58:54 +08:00
|
|
|
) or pod2usage(-exitstatus => -1);
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2015-01-07 22:11:07 +08:00
|
|
|
if ( $version ) {
|
2017-06-23 21:58:54 +08:00
|
|
|
print ZoneMinder::Base::ZM_VERSION . "\n";
|
|
|
|
exit(0);
|
2015-01-07 22:11:07 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:58:54 +08:00
|
|
|
if ( !( $filter_id or $filter_name or $event_id ) || ($event_id and ( $event_id < 0 ) ) ) {
|
|
|
|
print( STDERR "Please give a valid event id or filter name\n" );
|
|
|
|
pod2usage(-exitstatus => -1);
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:58:54 +08:00
|
|
|
if ( ! $Config{ZM_OPT_FFMPEG} ) {
|
|
|
|
print( STDERR "Mpeg encoding is not currently enabled\n" );
|
|
|
|
exit(-1);
|
|
|
|
}
|
|
|
|
|
|
|
|
my @formats = split( /\s+/, $Config{ZM_FFMPEG_FORMATS} );
|
|
|
|
for ( my $i = 0; $i < @formats; $i++ ) {
|
|
|
|
if ( $i =~ /^(.+)\*$/ ) {
|
|
|
|
$format = $formats[$i] = $1;
|
|
|
|
}
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:58:54 +08:00
|
|
|
if ( !$rate && !$fps ) {
|
|
|
|
$rate = 1;
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:58:54 +08:00
|
|
|
if ( !$scale && !$size ) {
|
|
|
|
$scale = 1;
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:58:54 +08:00
|
|
|
if ( $rate && ($rate < 0.25 || $rate > 100) ) {
|
|
|
|
print( STDERR "Rate is out of range, 0.25 >= rate <= 100\n" );
|
|
|
|
pod2usage(-exitstatus => -1);
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:58:54 +08:00
|
|
|
if ( $scale && ($scale < 0.25 || $scale > 4) ) {
|
|
|
|
print( STDERR "Scale is out of range, 0.25 >= scale <= 4\n" );
|
|
|
|
pod2usage(-exitstatus => -1);
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
2017-06-23 21:58:54 +08:00
|
|
|
if ( $fps && ($fps > 30) ) {
|
|
|
|
print( STDERR "FPS is out of range, <= 30\n" );
|
|
|
|
pod2usage(-exitstatus => -1);
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
my ( $detaint_format ) = $format =~ /^(\w+)$/;
|
|
|
|
my ( $detaint_rate ) = $rate =~ /^(-?\d+(?:\.\d+)?)$/;
|
|
|
|
my ( $detaint_scale ) = $scale =~ /^(-?\d+(?:\.\d+)?)$/;
|
|
|
|
my ( $detaint_fps ) = $fps =~ /^(-?\d+(?:\.\d+)?)$/;
|
|
|
|
my ( $detaint_size ) = $size =~ /^(\w+)$/;
|
|
|
|
|
|
|
|
$format = $detaint_format;
|
|
|
|
$rate = $detaint_rate;
|
|
|
|
$scale = $detaint_scale;
|
|
|
|
$fps = $detaint_fps;
|
|
|
|
$size = $detaint_size;
|
|
|
|
|
2007-09-07 23:39:44 +08:00
|
|
|
my $dbh = zmDbConnect();
|
2005-12-16 18:05:29 +08:00
|
|
|
|
2015-12-18 03:36:40 +08:00
|
|
|
my $cwd = getcwd;
|
|
|
|
|
|
|
|
my $video_name;
|
|
|
|
my @event_ids;
|
2018-07-17 19:57:38 +08:00
|
|
|
|
|
|
|
# Fail if the path to a valid ffmpeg binary is not set
|
|
|
|
if ( ! -x $Config{ZM_PATH_FFMPEG} ) {
|
|
|
|
Fatal("Ffmpeg binary not found or not executable. Verify ZM_PATH_FFMPEG points to ffmpeg, avconv, or a compatible binary.");
|
|
|
|
}
|
|
|
|
|
2015-12-18 03:36:40 +08:00
|
|
|
if ( $event_id ) {
|
2017-06-23 21:58:54 +08:00
|
|
|
@event_ids = ( $event_id );
|
|
|
|
|
2015-12-18 03:36:40 +08:00
|
|
|
} elsif ( $filter_name or $filter_id ) {
|
2017-06-23 21:58:54 +08:00
|
|
|
my $Filter = ZoneMinder::Filter->find_one(
|
|
|
|
($filter_name ? ( Name => $filter_name ) : () ),
|
|
|
|
($filter_id ? ( Id => $filter_name ) : () ),
|
|
|
|
);
|
|
|
|
if ( ! $Filter ) {
|
|
|
|
Fatal("Filter $filter_name $filter_id not found.");
|
|
|
|
}
|
|
|
|
@event_ids = map { $_->{Id} } $Filter->Execute();
|
|
|
|
if ( ! @event_ids ) {
|
|
|
|
Fatal( "No events found for $filter_name")
|
|
|
|
} else {
|
|
|
|
Debug(@event_ids . " events found for $filter_name");
|
|
|
|
}
|
|
|
|
$concat_name = $filter_name if $concat_name eq '';
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
2008-07-14 23:56:52 +08:00
|
|
|
|
2017-07-15 03:55:08 +08:00
|
|
|
my $sql = " SELECT (SELECT max(Delta) FROM Frames WHERE EventId=Events.Id)-(SELECT min(Delta) FROM Frames WHERE EventId=Events.Id) as FullLength,
|
|
|
|
Events.*,
|
2020-11-05 02:49:39 +08:00
|
|
|
unix_timestamp(Events.StartDateTime) as Time,
|
2015-12-18 03:36:40 +08:00
|
|
|
M.Name as MonitorName,
|
|
|
|
M.Palette
|
2017-07-15 03:55:08 +08:00
|
|
|
FROM Events
|
|
|
|
INNER JOIN Monitors as M on Events.MonitorId = M.Id
|
|
|
|
WHERE Events.Id = ?
|
|
|
|
";
|
2015-12-18 03:36:40 +08:00
|
|
|
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
|
2017-07-15 03:55:08 +08:00
|
|
|
Debug($sql);
|
2015-12-18 03:36:40 +08:00
|
|
|
|
|
|
|
my @video_files;
|
|
|
|
foreach my $event_id ( @event_ids ) {
|
2017-06-23 21:58:54 +08:00
|
|
|
my $res = $sth->execute( $event_id )
|
|
|
|
or Fatal( "Can't execute: ".$sth->errstr() );
|
|
|
|
my $event = $sth->fetchrow_hashref();
|
|
|
|
|
|
|
|
my $Event = new ZoneMinder::Event( $$event{Id}, $event );
|
|
|
|
my $video_file = $Event->GenerateVideo( $rate, $fps, $scale, $size, $overwrite, $format );
|
|
|
|
if ( $video_file ) {
|
|
|
|
push @video_files, $video_file;
|
|
|
|
print( STDOUT $video_file."\n" );
|
|
|
|
} else {
|
|
|
|
Warning("No video file generated for event $event_id");
|
|
|
|
}
|
2015-12-18 03:36:40 +08:00
|
|
|
} # end foreach event_id
|
|
|
|
|
|
|
|
if ( $concat_name ) {
|
2017-06-23 21:58:54 +08:00
|
|
|
($cwd) = $cwd =~ /(.*)/; # detaint
|
|
|
|
chdir( $cwd );
|
|
|
|
($concat_name ) = $concat_name =~ /([\-A-Za-z0-9_\.]*)/;
|
|
|
|
my $concat_list_file = "/tmp/$concat_name.concat.lst";
|
|
|
|
|
|
|
|
my $video_file = $concat_name . '.'. $detaint_format;
|
|
|
|
|
|
|
|
open( my $fd, '>', $concat_list_file ) or die "Can't open $concat_list_file: $!";
|
|
|
|
foreach ( @video_files ) {
|
|
|
|
print $fd "file '$_'\n";
|
|
|
|
}
|
|
|
|
close $fd;
|
|
|
|
my $command = $Config{ZM_PATH_FFMPEG}
|
2017-07-15 03:55:08 +08:00
|
|
|
. " -f concat -safe 0 -i $concat_list_file -c copy "
|
|
|
|
.$Config{ZM_FFMPEG_OUTPUT_OPTIONS}
|
|
|
|
." '$video_file' > $Config{ZM_PATH_LOGS}/ffmpeg_${concat_name}.log 2>&1"
|
2017-06-23 21:58:54 +08:00
|
|
|
;
|
|
|
|
Debug( $command."\n" );
|
|
|
|
my $output = qx($command);
|
|
|
|
|
|
|
|
my $status = $? >> 8;
|
|
|
|
|
|
|
|
unlink $concat_list_file;
|
|
|
|
if ( $status ) {
|
2017-07-15 03:55:08 +08:00
|
|
|
Error( "Unable to generate video, check $Config{ZM_PATH_LOGS}/ffmpeg_${concat_name}.log for details");
|
2017-06-23 21:58:54 +08:00
|
|
|
exit(-1);
|
|
|
|
}
|
2017-07-15 03:55:08 +08:00
|
|
|
print( STDOUT $video_file."\n" );
|
2005-12-16 18:05:29 +08:00
|
|
|
}
|
|
|
|
exit( 0 );
|
2017-06-23 21:58:54 +08:00
|
|
|
|
|
|
|
__END__
|