I'm capturing an rtsp stream using ffmpeg every minute (underwater) and only want to discard any photos that are captured which is less than 10% light. This is my very simple cron script, which I'd like to augment.

ffmpeg -loglevel panic -rtsp_transport tcp -y -i "rtsp://user:pass@ipaddress/stream" -frames:v 1 -strftime 1 "/dir/%Y-%m-%d_%H-%M-%S_underwater_cam.jpg"
  • 1
    What is your question? How to detect black images? Please edit your question, as it stands it is unclear what you're asking. – Panki Nov 23 '20 at 9:48

I figured out a way, however it is not the most elegant solution from a processing standpoint. Adapted from this imagemagick discussion.


set -ea
DIRECTORY=/camera/$(date +%Y-%m-%d)

[ $test -d $DIRECTORY ] && : || mkdir -p $DIRECTORY;

#Loglevel panic is to ignore the transform errors this camera is putting out.  
#Forcing the RTSP transport as TCP to reduce corrupt images. 
#Using date with strftime, however this could probably be done as a variable.
ffmpeg -loglevel panic -rtsp_transport tcp -y -i "rtsp://user:pass@ipaddress/stream" -frames:v 1 -strftime 1 "$DIRECTORY/%Y-%m-%d_%H-%M-%S_underwater_cam.jpg"

#Find the most recent image, as it's assumed to be the picture above.
image=`ls -t $DIRECTORY | head -n1`

#threshold in percentage of light using imagemagick
mean=`convert $image -format "%[mean]" info:`
mean=`convert xc: -format "%[fx:100*$mean/quantumrange]" info:`
test=`convert xc: -format "%[fx: ($mean<$thresh)?1:0]" info:`

#example statement[ $test -eq 1 ] && echo "mean=$mean; too dark" || echo "mean=$mean; not too dark"
[ $test -eq 1 ] && rm $DIRECTORY/$image || :

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.