zmvideo.pl: pod2usage + readability

This commit is contained in:
Dmitry Smirnov 2015-04-16 15:16:32 +10:00
parent fcdd9b6fb5
commit 7ba03132fc
1 changed files with 113 additions and 72 deletions

View File

@ -20,10 +20,37 @@
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
# #
# ========================================================================== # ==========================================================================
#
# This script is used to create MPEG videos of events for the web pages =head1 NAME
# or as email attachments.
# zmvideo.pl - ZoneMinder Video Creation Script
=head1 SYNOPSIS
zmvideo.pl -e <event_id>,--event=<event_id> [--format <format>]
[--rate=<rate>]
[--scale=<scale>]
[--fps=<fps>]
[--size=<size>]
[--overwrite]
=head1 DESCRIPTION
This script is used to create MPEG videos of events for the web pages
or as email attachments.
=head1 OPTIONS
-e<event_id>, --event=<event_id> - What event to create the video for
-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
=cut
use strict; use strict;
use bytes; use bytes;
@ -39,6 +66,7 @@ use DBI;
use Data::Dumper; use Data::Dumper;
use POSIX qw(strftime); use POSIX qw(strftime);
use Getopt::Long qw(:config no_ignore_case ); use Getopt::Long qw(:config no_ignore_case );
use autouse 'Pod::Usage'=>qw(pod2usage);
$| = 1; $| = 1;
@ -66,27 +94,16 @@ for ( my $i = 0; $i < @formats; $i++ )
} }
} }
sub Usage GetOptions(
{ 'event=i' =>\$event_id,
print( " 'format|f=s' =>\$format,
Usage: zmvideo.pl -e <event_id>,--event=<event_id> [--format <format>] [--rate=<rate>] [--scale=<scale>] [--fps=<fps>] [--size=<size>] [--overwrite] 'rate|r=f' =>\$rate,
Parameters are :- 'scale|s=f' =>\$scale,
-e<event_id>, --event=<event_id> - What event to create the video for 'fps|F=f' =>\$fps,
-f<format>, --format=<format> - What format to create the video in, default is mpg. For ffmpeg only. 'size|S=s' =>\$size,
-r<rate>, --rate=<rate> - Relative rate , 1 = realtime, 2 = double speed , 0.5 = half speed etc 'overwrite' =>\$overwrite,
-s<scale>, --scale=<scale> - Scale, 1 = normal, 2 = double size, 0.5 = half size etc 'version' =>\$version
-F<fps>, --fps=<fps> - Absolute frame rate, in frames per second ) or pod2usage(-exitstatus => -1);
-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
");
exit( -1 );
}
if ( !GetOptions( 'event=i'=>\$event_id, 'format|f=s'=>\$format, 'rate|r=f'=>\$rate, 'scale|s=f'=>\$scale, 'fps|F=f'=>\$fps, 'size|S=s'=>\$size, 'overwrite'=>\$overwrite, version=>\$version ) )
{
Usage();
}
if ( $version ) { if ( $version ) {
print ZoneMinder::Base::ZM_VERSION . "\n"; print ZoneMinder::Base::ZM_VERSION . "\n";
@ -96,7 +113,7 @@ if ( $version ) {
if ( !$event_id || $event_id < 0 ) if ( !$event_id || $event_id < 0 )
{ {
print( STDERR "Please give a valid event id\n" ); print( STDERR "Please give a valid event id\n" );
Usage(); pod2usage(-exitstatus => -1);
} }
if ( ! $Config{ZM_OPT_FFMPEG} ) if ( ! $Config{ZM_OPT_FFMPEG} )
@ -118,19 +135,19 @@ if ( !$scale && !$size )
if ( $rate && ($rate < 0.25 || $rate > 100) ) if ( $rate && ($rate < 0.25 || $rate > 100) )
{ {
print( STDERR "Rate is out of range, 0.25 >= rate <= 100\n" ); print( STDERR "Rate is out of range, 0.25 >= rate <= 100\n" );
Usage(); pod2usage(-exitstatus => -1);
} }
if ( $scale && ($scale < 0.25 || $scale > 4) ) if ( $scale && ($scale < 0.25 || $scale > 4) )
{ {
print( STDERR "Scale is out of range, 0.25 >= scale <= 4\n" ); print( STDERR "Scale is out of range, 0.25 >= scale <= 4\n" );
Usage(); pod2usage(-exitstatus => -1);
} }
if ( $fps && ($fps > 30) ) if ( $fps && ($fps > 30) )
{ {
print( STDERR "FPS is out of range, <= 30\n" ); print( STDERR "FPS is out of range, <= 30\n" );
Usage(); pod2usage(-exitstatus => -1);
} }
my ( $detaint_format ) = $format =~ /^(\w+)$/; my ( $detaint_format ) = $format =~ /^(\w+)$/;
@ -148,9 +165,23 @@ $size = $detaint_size;
my $dbh = zmDbConnect(); my $dbh = zmDbConnect();
my @filters; my @filters;
my $sql = "select max(F.Delta)-min(F.Delta) as FullLength, E.*, unix_timestamp(E.StartTime) as Time, M.Name as MonitorName, M.Width as MonitorWidth, M.Height as MonitorHeight, M.Palette from Frames as F inner join Events as E on F.EventId = E.Id inner join Monitors as M on E.MonitorId = M.Id where EventId = '$event_id' group by F.EventId"; my $sql = " SELECT max(F.Delta)-min(F.Delta) as FullLength,
my $sth = $dbh->prepare_cached( $sql ) or Fatal( "Can't prepare '$sql': ".$dbh->errstr() ); E.*,
my $res = $sth->execute() or Fatal( "Can't execute: ".$sth->errstr() ); unix_timestamp(E.StartTime) as Time,
M.Name as MonitorName,
M.Width as MonitorWidth,
M.Height as MonitorHeight,
M.Palette
FROM Frames as F
INNER JOIN Events as E on F.EventId = E.Id
INNER JOIN Monitors as M on E.MonitorId = M.Id
WHERE EventId = '$event_id'
GROUP BY F.EventId"
;
my $sth = $dbh->prepare_cached( $sql )
or Fatal( "Can't prepare '$sql': ".$dbh->errstr() );
my $res = $sth->execute()
or Fatal( "Can't execute: ".$sth->errstr() );
my $event = $sth->fetchrow_hashref(); my $event = $sth->fetchrow_hashref();
$sth->finish(); $sth->finish();
my $event_path = getEventPath( $event ); my $event_path = getEventPath( $event );
@ -228,14 +259,24 @@ if ( $overwrite || !-s $video_file )
$video_size = $size; $video_size = $size;
} }
my $command = $Config{ZM_PATH_FFMPEG}." -y -r $frame_rate ".$Config{ZM_FFMPEG_INPUT_OPTIONS}." -i %0".$Config{ZM_EVENT_IMAGE_DIGITS}."d-capture.jpg -s $video_size ".$Config{ZM_FFMPEG_OUTPUT_OPTIONS}." '$video_file' > ffmpeg.log 2>&1"; my $command = $Config{ZM_PATH_FFMPEG}
." -y -r $frame_rate "
.$Config{ZM_FFMPEG_INPUT_OPTIONS}
." -i %0"
.$Config{ZM_EVENT_IMAGE_DIGITS}
."d-capture.jpg -s $video_size "
.$Config{ZM_FFMPEG_OUTPUT_OPTIONS}
." '$video_file' > ffmpeg.log 2>&1"
;
Debug( $command."\n" ); Debug( $command."\n" );
my $output = qx($command); my $output = qx($command);
my $status = $? >> 8; my $status = $? >> 8;
if ( $status ) if ( $status )
{ {
Error( "Unable to generate video, check ".$event_path."/ffmpeg.log for details" ); Error( "Unable to generate video, check "
.$event_path."/ffmpeg.log for details"
);
exit( -1 ); exit( -1 );
} }