使用脚本丢弃黑色/深色间隔拍摄照片

使用脚本丢弃黑色/深色间隔拍摄照片

我每分钟(水下)使用 ffmpeg 捕获 rtsp 流,并且只想丢弃捕获的光线低于 10% 的所有照片。这是我非常简单的 cron 脚本,我想对其进行扩充。

#!/bin/bash
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

我找到了一种方法,但从处理的角度来看,这并不是最优雅的解决方案。改编自这个 imagemagick 讨论

#!/bin/bash

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"

cd $DIRECTORY
#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
thresh=10
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 || :

相关内容