fix logic when snapshot.jpg doesn't exist. We need an actual frame, to get a Delta to tell ffmpeg which frame to use. So load the Frame with Score=MaxScore

pull/2112/head
Isaac Connor 2018-05-11 10:37:00 -04:00
parent b20cb5d0ae
commit bb76079d54
1 changed files with 99 additions and 88 deletions

View File

@ -62,67 +62,76 @@ if ( empty($_REQUEST['path']) ) {
$show = empty($_REQUEST['show']) ? 'capture' : $_REQUEST['show'];
if ( ! empty($_REQUEST['fid']) ) {
if ( $_REQUEST['fid'] == 'snapshot' ) {
$Event = new Event($_REQUEST['eid']);
$Frame = new Frame();
$Frame->FrameId('snapshot');
$path = $Event->Path().'/snapshot.jpg';
} else {
if ( !empty($_REQUEST['eid']) ) {
$Event = Event::find_one(array('Id'=>$_REQUEST['eid']));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $_REQUEST['eid'].' Not found');
return;
}
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'FrameId'=>$_REQUEST['fid']));
if ( ! $Frame ) {
$previousBulkFrame = dbFetchOne('SELECT * FROM Frames WHERE EventId=? AND FrameId < ? ORDER BY FrameID DESC LIMIT 1', NULL, array($_REQUEST['eid'], $_REQUEST['fid']));
$nextBulkFrame = dbFetchOne('SELECT * FROM Frames WHERE EventId=? AND FrameId > ? ORDER BY FrameID ASC LIMIT 1', NULL, array($_REQUEST['eid'], $_REQUEST['fid']));
if ( $previousBulkFrame and $nextBulkFrame ) {
$Frame = new Frame($previousBulkFrame);
$Frame->FrameId($_REQUEST['fid']);
$percentage = ($Frame->FrameId() - $previousBulkFrame['FrameId']) / ($nextBulkFrame['FrameId'] - $previousBulkFrame['FrameId']);
$Frame->Delta($previousBulkFrame['Delta'] + floor( 100* ( $nextBulkFrame['Delta'] - $previousBulkFrame['Delta'] ) * $percentage )/100);
Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousBulkFrame['Delta'] . " + nextdelta:" . $nextBulkFrame['Delta'] . ' - ' . $previousBulkFrame['Delta'] . ' * ' . $percentage );
} else {
Fatal('No Frame found for event('.$_REQUEST['eid'].') and frame id('.$_REQUEST['fid'].')');
}
}
// Frame can be non-existent. We have Bulk frames. So now we should try to load the bulk frame
} else {
# If we are only specifying fid, then the fid must be the primary key into the frames table. But when the event is specified, then it is the frame #
$Frame = Frame::find_one(array('Id'=>$_REQUEST['fid']));
if ( ! $Frame ) {
header('HTTP/1.0 404 Not Found');
Fatal('Frame ' . $_REQUEST['fid'] . ' Not Found');
return;
}
$Event = Event::find_one(array('Id'=>$Frame->EventId()));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $Frame->EventId() . ' Not Found');
return;
}
}
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d',$Frame->FrameId()).'-'.$show.'.jpg';
}
} else {
if ( empty($_REQUEST['fid']) ) {
header('HTTP/1.0 404 Not Found');
Fatal('No Frame ID specified');
return;
}
if ( !empty($_REQUEST['eid']) ) {
$Event = Event::find_one(array('Id'=>$_REQUEST['eid']));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $_REQUEST['eid'].' Not found');
return;
}
if ( $_REQUEST['fid'] == 'snapshot' ) {
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'Score'=>$Event->MaxScore()));
if ( ! $Frame )
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid']));
if ( ! $Frame ) {
$Frame = new Frame();
$Frame->Delta(1);
$Frame->FrameId('snapshot');
}
$path = $Event->Path().'/snapshot.jpg';
} else {
$Frame = Frame::find_one(array('EventId'=>$_REQUEST['eid'], 'FrameId'=>$_REQUEST['fid']));
if ( ! $Frame ) {
$previousBulkFrame = dbFetchOne(
'SELECT * FROM Frames WHERE EventId=? AND FrameId < ? ORDER BY FrameID DESC LIMIT 1',
NULL, array($_REQUEST['eid'], $_REQUEST['fid'])
);
$nextBulkFrame = dbFetchOne(
'SELECT * FROM Frames WHERE EventId=? AND FrameId > ? ORDER BY FrameID ASC LIMIT 1',
NULL, array($_REQUEST['eid'], $_REQUEST['fid'])
);
if ( $previousBulkFrame and $nextBulkFrame ) {
$Frame = new Frame($previousBulkFrame);
$Frame->FrameId($_REQUEST['fid']);
$percentage = ($Frame->FrameId() - $previousBulkFrame['FrameId']) / ($nextBulkFrame['FrameId'] - $previousBulkFrame['FrameId']);
$Frame->Delta($previousBulkFrame['Delta'] + floor( 100* ( $nextBulkFrame['Delta'] - $previousBulkFrame['Delta'] ) * $percentage )/100);
Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousBulkFrame['Delta'] . " + nextdelta:" . $nextBulkFrame['Delta'] . ' - ' . $previousBulkFrame['Delta'] . ' * ' . $percentage );
} else {
Fatal('No Frame found for event('.$_REQUEST['eid'].') and frame id('.$_REQUEST['fid'].')');
}
}
// Frame can be non-existent. We have Bulk frames. So now we should try to load the bulk frame
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d',$Frame->FrameId()).'-'.$show.'.jpg';
}
} else {
# If we are only specifying fid, then the fid must be the primary key into the frames table. But when the event is specified, then it is the frame #
$Frame = Frame::find_one(array('Id'=>$_REQUEST['fid']));
if ( ! $Frame ) {
header('HTTP/1.0 404 Not Found');
Fatal('Frame ' . $_REQUEST['fid'] . ' Not Found');
return;
}
$Event = Event::find_one(array('Id'=>$Frame->EventId()));
if ( ! $Event ) {
header('HTTP/1.0 404 Not Found');
Fatal('Event ' . $Frame->EventId() . ' Not Found');
return;
}
$path = $Event->Path().'/'.sprintf('%0'.ZM_EVENT_IMAGE_DIGITS.'d',$Frame->FrameId()).'-'.$show.'.jpg';
} # end if have eid
if ( ! file_exists($path) ) {
Logger::Debug("$path does not exist");
# Generate the frame JPG
@ -134,7 +143,7 @@ Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousB
$command ='ffmpeg -ss '. $Frame->Delta() .' -i '.$Event->Path().'/'.$Event->DefaultVideo().' -frames:v 1 '.$path;
#$command ='ffmpeg -ss '. $Frame->Delta() .' -i '.$Event->Path().'/'.$Event->DefaultVideo().' -vf "select=gte(n\\,'.$Frame->FrameId().'),setpts=PTS-STARTPTS" '.$path;
#$command ='ffmpeg -v 0 -i '.$Storage->Path().'/'.$Event->Path().'/'.$Event->DefaultVideo().' -vf "select=gte(n\\,'.$Frame->FrameId().'),setpts=PTS-STARTPTS" '.$path;
Logger::Debug( "Running $command" );
Logger::Debug("Running $command");
$output = array();
$retval = 0;
exec( $command, $output, $retval );
@ -143,13 +152,15 @@ Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousB
header('HTTP/1.0 404 Not Found');
Fatal("Can't create frame images from video for this event (".$Event->DefaultVideo() );
}
$Event->DiskSpace( null );
# Generating an image file will use up more disk space, so update the Event record.
$Event->DiskSpace(null);
$Event->save();
} else {
header('HTTP/1.0 404 Not Found');
Fatal("Can't create frame $show images from video because there is no video file for this event (".$Event->DefaultVideo() );
Fatal("Can't create frame $show images from video because there is no video file for this event at ".
$Event->Path().'/'.$Event->DefaultVideo() );
}
}
} # end if ! file_exists($path)
} else {
Warning('Loading images by path is deprecated');
@ -158,10 +169,10 @@ Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousB
$pos = strpos($path, $dir_events);
if ( $pos == 0 && $pos !== false ) {
if ( ! empty( $user['MonitorIds'] ) ) {
if ( ! empty($user['MonitorIds']) ) {
$imageOk = false;
$pathMonId = substr( $path, 0, strspn( $path, '1234567890' ) );
foreach ( preg_split( '/["\'\s]*,["\'\s]*/', $user['MonitorIds'] ) as $monId ) {
$pathMonId = substr($path, 0, strspn($path, '1234567890'));
foreach ( preg_split('/["\'\s]*,["\'\s]*/', $user['MonitorIds']) as $monId ) {
if ( $pathMonId == $monId ) {
$imageOk = true;
break;
@ -173,39 +184,39 @@ Logger::Debug("Got virtual frame from Bulk Frames previous delta: " . $previousB
} else {
$errorText = 'Invalid image path';
}
if ( ! file_exists( $path ) ) {
if ( ! file_exists($path) ) {
header('HTTP/1.0 404 Not Found');
Fatal("Image not found at $path");
}
}
$scale=0;
if( !empty($_REQUEST['scale']) ) {
if (is_numeric($_REQUEST['scale'])) {
if ( !empty($_REQUEST['scale']) ) {
if ( is_numeric($_REQUEST['scale']) ) {
$x = $_REQUEST['scale'];
if($x >= 1 and $x <= 400)
if ( $x >= 1 and $x <= 400 )
$scale=$x;
}
}
$width=0;
if ( !empty($_REQUEST['width']) ) {
if (is_numeric($_REQUEST['width'])) {
if ( is_numeric($_REQUEST['width']) ) {
$x = $_REQUEST['width'];
if($x >= 10 and $x <= 8000)
if ( $x >= 10 and $x <= 8000 )
$width=$x;
}
}
$height=0;
if( !empty($_REQUEST['height']) ) {
if (is_numeric($_REQUEST['height'])) {
if ( !empty($_REQUEST['height']) ) {
if ( is_numeric($_REQUEST['height']) ) {
$x = $_REQUEST['height'];
if($x >= 10 and $x <= 8000)
if ( $x >= 10 and $x <= 8000 )
$height=$x;
}
}
header( 'Content-type: image/jpeg' );
header('Content-type: image/jpeg');
# This is so that Save Image As give a useful filename
if ( $Event ) {
@ -216,19 +227,19 @@ ob_clean();
flush();
if ( $errorText ) {
Error( $errorText );
Error($errorText);
} else {
if ( ( $scale==0 || $scale==100 ) && $width==0 && $height==0 ) {
if ( ! readfile( $path ) ) {
Error("No bytes read from ". $path );
if ( !readfile($path) ) {
Error('No bytes read from '. $path);
}
} else {
Logger::Debug("Doing a scaled image: scale($scale) width($width) height($height)");
$i = 0;
if ( ! ( $width && $height ) ) {
$i = imagecreatefromjpeg( $path );
$oldWidth = imagesx( $i );
$oldHeight = imagesy( $i );
$i = imagecreatefromjpeg($path);
$oldWidth = imagesx($i);
$oldHeight = imagesy($i);
if ( $width == 0 && $height == 0 ) { // scale has to be set to get here with both zero
$width = $oldWidth * $scale / 100.0;
$height= $oldHeight * $scale / 100.0;
@ -238,23 +249,23 @@ if ( $errorText ) {
$height = ($width * $oldHeight) / $oldWidth;
}
if ( $width == $oldWidth && $height == $oldHeight) {
Warning( 'No change to width despite scaling.' );
Warning('No change to width despite scaling.');
}
}
# Slight optimisation, thumbnails always specify width and height, so we can cache them.
$scaled_path = preg_replace('/\.jpg$/', "-${width}x${height}.jpg", $path );
if ( ! file_exists( $scaled_path ) or ! readfile( $scaled_path ) ) {
Logger::Debug( "Cached scaled image does not exist at $scaled_path or is no good.. Creating it");
if ( ! file_exists($scaled_path) or ! readfile($scaled_path) ) {
Logger::Debug("Cached scaled image does not exist at $scaled_path or is no good.. Creating it");
ob_start();
if ( ! $i )
$i = imagecreatefromjpeg( $path );
$iScale = imagescale( $i, $width, $height );
imagejpeg( $iScale );
imagedestroy( $i );
imagedestroy( $iScale );
$i = imagecreatefromjpeg($path);
$iScale = imagescale($i, $width, $height);
imagejpeg($iScale);
imagedestroy($i);
imagedestroy($iScale);
$scaled_jpeg_data = ob_get_contents();
file_put_contents( $scaled_path, $scaled_jpeg_data );
file_put_contents($scaled_path, $scaled_jpeg_data);
ob_end_clean();
echo $scaled_jpeg_data;
}