From 22490ebfacfa3f09a2dec23b4b1e90c3bc8f440d Mon Sep 17 00:00:00 2001 From: stan Date: Fri, 10 Oct 2003 15:46:36 +0000 Subject: [PATCH] Added video generation frame rate and scaling. git-svn-id: http://svn.zoneminder.com/svn/zm/trunk@651 e3e1d417-86f3-4887-817a-d78f3d33393f --- scripts/zmvideo.pl.z | 108 +++++++++++++++++++++++++++++++++---- web/zm_config.php.z | 8 +++ web/zm_funcs.php | 6 ++- web/zm_html_view_video.php | 76 ++++++++++++++++---------- 4 files changed, 160 insertions(+), 38 deletions(-) diff --git a/scripts/zmvideo.pl.z b/scripts/zmvideo.pl.z index 5ce34592d..9ea326380 100644 --- a/scripts/zmvideo.pl.z +++ b/scripts/zmvideo.pl.z @@ -77,18 +77,24 @@ $ENV{SHELL} = '/bin/sh' if exists $ENV{SHELL}; delete @ENV{qw(IFS CDPATH ENV BASH_ENV)}; my $event_id; +my $rate = 1; +my $scale = 1; +my $overwrite = 0; sub Usage { print( " -Usage: zmvideo.pl -e ,--event= +Usage: zmvideo.pl -e ,--event= [-r ,--rate=] [-s ,--scale=] [-o,--overwrite] Parameters are :- --e, --event= - What event to start create the video for +-e, --event= - What event to create the video for +-r, --rate= - Relative rate to use, 1 = realtime, 2 = double speed , -2 = half speed etc +-s, --scale= - Scale to use, 1 = normal, 2 = double size, -2 = half size etc +-o, --overwrite - Whether to overwrite an existing file, off by default. "); exit( -1 ); } -if ( !GetOptions( 'event=i'=>\$event_id ) ) +if ( !GetOptions( 'event=i'=>\$event_id, 'rate=i'=>\$rate, 'scale=i'=>\$scale, 'overwrite'=>\$overwrite ) ) { Usage(); } @@ -105,6 +111,30 @@ if ( ZM_OPT_MPEG eq "no" ) exit(-1); } +if ( ZM_OPT_MPEG eq "mpeg_encode" && $rate != 1 ) +{ + print( STDERR "Variable rate not supported with mpeg_encode\n" ); + exit(-1); +} + +if ( $rate < -4 || $rate > 10 ) +{ + print( STDERR "Rate is out of range, -4 >= rate <= 10\n" ); + Usage(); +} + +if ( !$scale || $scale < -4 || $scale > 4 ) +{ + print( STDERR "Scale is out of range, -4 >= scale <= 4\n" ); + Usage(); +} + +my ( $detaint_rate ) = $rate =~ /^(-?\d+(?:\.\d+)?)$/; +my ( $detaint_scale ) = $scale =~ /^(-?\d+(?:\.\d+)?)$/; + +$rate = $detaint_rate; +$scale = $detaint_scale; + my $log_file = LOG_FILE; open( LOG, ">>$log_file" ) or die( "Can't open log file: $!" ); #open( STDOUT, ">&LOG" ) || die( "Can't dup stdout: $!" ); @@ -116,7 +146,7 @@ select( LOG ); $| = 1; my $dbh = DBI->connect( "DBI:mysql:database=".ZM_DB_NAME.";host=".ZM_DB_SERVER, ZM_DB_USERB, ZM_DB_PASSB ); my @filters; -my $sql = "select max(F.Delta)-min(F.Delta) as FullLength,E.*,M.Name as MonitorName, 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, E.*, 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 die( "Can't prepare '$sql': ".$dbh->errstr() ); my $res = $sth->execute() or die( "Can't execute: ".$sth->errstr() ); my $event = $sth->fetchrow_hashref(); @@ -125,7 +155,7 @@ chdir( ZM_PATH_WEB.'/'.ZM_DIR_EVENTS.'/'.$event->{MonitorName}.'/'.$event->{Id} ( my $video_name = $event->{Name} ) =~ s/\s/_/g; my $video_file = "$video_name.mpg"; -if ( !-s $video_file ) +if ( $overwrite || !-s $video_file ) { print( LOG "Creating video file $video_file for event $event->{Id}\n" ); @@ -153,13 +183,36 @@ if ( !-s $video_file ) print( PARAMS "REFERENCE_FRAME ORIGINAL\n" ); print( PARAMS "FRAME_RATE 24\n" ); - if ( $event->{Palette} == 1 && !ZM_COLOUR_JPEG_FILES ) + my $scale_conversion = ""; + if ( $scale != 1 ) { - print( PARAMS "INPUT_CONVERT ".ZM_PATH_NETPBM."/jpegtopnm * | ".ZM_PATH_NETPBM."/pgmtoppm white | ".ZM_PATH_NETPBM."/ppmtojpeg\n" ); + if ( $scale > 1 ) + { + $scale_conversion = ZM_PATH_NETPBM."/pnmscale $scale"; + } + else + { + $scale_conversion = ZM_PATH_NETPBM."/pnmscale ".(1/$scale); + } + if ( $event->{Palette} == 1 && !ZM_COLOUR_JPEG_FILES ) + { + print( PARAMS "INPUT_CONVERT ".ZM_PATH_NETPBM."/jpegtopnm * | ".$scale_conversion." | ".ZM_PATH_NETPBM."/pgmtoppm white | ".ZM_PATH_NETPBM."/ppmtojpeg\n" ); + } + else + { + print( PARAMS "INPUT_CONVERT ".ZM_PATH_NETPBM."/jpegtopnm * | ".$scale_conversion." | ".ZM_PATH_NETPBM."/ppmtojpeg\n" ); + } } else { - print( PARAMS "INPUT_CONVERT *\n" ); + if ( $event->{Palette} == 1 && !ZM_COLOUR_JPEG_FILES ) + { + print( PARAMS "INPUT_CONVERT ".ZM_PATH_NETPBM."/jpegtopnm * | ".ZM_PATH_NETPBM."/pgmtoppm white | ".ZM_PATH_NETPBM."/ppmtojpeg\n" ); + } + else + { + print( PARAMS "INPUT_CONVERT *\n" ); + } } print( PARAMS "INPUT_DIR .\n" ); @@ -172,13 +225,50 @@ if ( !-s $video_file ) close( PARAMS ); my $command = ZM_PATH_MPEG_ENCODE." $param_file >mpeg.log"; + print( LOG $command."\n" ); my $output = qx($command); + print( LOG $output."\n" ); } elsif ( ZM_OPT_MPEG eq "ffmpeg" ) { my $frame_rate = sprintf( "%.2f", $event->{Frames}/$event->{FullLength} ); - my $command = ZM_PATH_FFMPEG." -y ".ZM_FFMPEG_OPTIONS." -r $frame_rate -i capture-%03d.jpg $video_file > mpeg.log"; + if ( $rate ) + { + if ( $rate != 1 ) + { + if ( $rate > 1 ) + { + $frame_rate *= $rate; + } + else + { + $frame_rate /= $rate; + } + } + } + else + { + $frame_rate = 25.0; + } + my $width = $event->{MonitorWidth}; + my $height = $event->{MonitorHeight}; + if ( $scale != 1 ) + { + if ( $scale > 1 ) + { + $width = int($width*$scale); + $height = int($height*$scale); + } + else + { + $width = int($width/$scale); + $height = int($height/$scale); + } + } + my $command = ZM_PATH_FFMPEG." -y ".ZM_FFMPEG_OPTIONS." -r $frame_rate -s ${width}x${height} -i capture-%03d.jpg $video_file > mpeg.log"; + print( LOG $command."\n" ); my $output = qx($command); + print( LOG $output."\n" ); } else { diff --git a/web/zm_config.php.z b/web/zm_config.php.z index c8ca450dd..7c61179b5 100644 --- a/web/zm_config.php.z +++ b/web/zm_config.php.z @@ -52,6 +52,14 @@ $rates = array( "-4" => "1/4x", ); +$scales = array( + "4" => "4x", + "2" => "2x", + "1" => "Real", + "-2" => "1/2x", + "-4" => "1/4x", +); + require_once( 'zm_db.php' ); loadConfig(); diff --git a/web/zm_funcs.php b/web/zm_funcs.php index 37d3ac6b8..ae2321977 100644 --- a/web/zm_funcs.php +++ b/web/zm_funcs.php @@ -307,9 +307,11 @@ function zmaCheck( $monitor ) return( daemonCheck( "zma", "-m $monitor" ) ); } -function createVideo( $event ) +function createVideo( $event, $rate, $scale, $overwrite=0 ) { - $command = ZM_PATH_BIN."/zmvideo.pl -e $event[Id]"; + $command = ZM_PATH_BIN."/zmvideo.pl -e $event[Id] -r $rate -s $scale"; + if ( $overwite ) + $command .= " -o"; $result = exec( $command, $output, $status ); return( $status?"":rtrim($result) ); } diff --git a/web/zm_html_view_video.php b/web/zm_html_view_video.php index 6b23d7128..ba0790d83 100644 --- a/web/zm_html_view_video.php +++ b/web/zm_html_view_video.php @@ -9,9 +9,12 @@ die( mysql_error() ); $event = mysql_fetch_assoc( $result ); - ob_start(); + if ( !isset( $scale ) ) + $scale = 1; + if ( !isset( $rate ) ) + $rate = 1; - // Note this all has a bunch of extra padding as IE won't flush less than 1024 chars + ob_start(); ?> @@ -19,12 +22,28 @@ +
+ + + + + + + + + + + + +
Video Generation Parameters
 
Frame Rate
Video Size
Overwite Existing checked>
 
+
+ - - - @@ -32,45 +51,48 @@
 
 
 
 
 
Generating Video
 
-"; - for ( $i = 0; $i < 4096/strlen($buffer_string); $i++ ) - { - echo $buffer_string."\n"; - } -?> "; + for ( $i = 0; $i < 4096/strlen($buffer_string); $i++ ) + { + echo $buffer_string."\n"; + } +?> + -ZM - Video - <?= $event[Name] ?> - -ZM - Video - <?= $event[Name] ?> -




Video Generation Failed!


- - +



Video Generation Failed!

+ +