CCTV
买的萤石 C6TC ,有 RTSP 可以玩,萤石的接口比较丰富正好也可以搞自动化
服务器装好 ffmpeg
拍照
photo.sh
将监控 RTSP 流转成照片并按天生成文件夹
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
IP=192.168.2.56
KEY=萤石密码
PHOTO_PATH=/mnt/da/CCTV/living
TIME=$(date +%Y%m%d%H%M%S)
DATE=$(date +%Y%m%d)
SAVE_DAY=365
function photo() {
if [ ! -e $PHOTO_PATH/$DATE ] && [ ! -d $PHOTO_PATH/$DATE ]; then
mkdir -p $PHOTO_PATH/$DATE
fi
ffmpeg -i rtsp://admin:$KEY@$IP:554/h264/ch1/main/av_stream -f image2 -vframes 1 -pix_fmt yuvj420p $PHOTO_PATH/$DATE/$TIME.jpg
}
function del() {
find $PHOTO_PATH -type d -mtime +$SAVE_DAY -exec rm -rf {} \;
}
case $1 in
p)
photo
;;
d)
del
;;
?)
echo "error param"
exit 1
;;
esac
图片转视频(延时摄影)
find -name \*.jpg | sort | xargs cat | ffmpeg -f image2pipe -r 30 -vcodec mjpeg -i - -vcodec libx264 out.mp4
定时任务
拍照
*/5 * * * * root /mnt/da/CCTV/photo.sh p
删除
*/5 * * * * root /mnt/da/CCTV/photo.sh d
录像
record.sh
将监控 RTSP 流转成 MP4 并按天生成文件夹
#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
IP=192.168.2.56
KEY=萤石密码
CCTV_PATH=/mnt/db/cctv/living
TIME=$(date +%Y%m%d%H%M%S)
DATE=$(date +%Y%m%d)
SAVE_DAY=365
function record() {
if [ ! -e $CCTV_PATH/$DATE ] && [ ! -d $CCTV_PATH/$DATE ]; then
mkdir -p $CCTV_PATH/$DATE
fi
ffmpeg -i rtsp://admin:$KEY@192.168.2.56:554/h264/ch1/main/av_stream -acodec copy -vcodec copy $CCTV_PATH/$DATE/$TIME.mp4
}
function kill() {
ps -ef | grep $KEY | grep -v grep | awk '{print $1}' | xargs kill
}
function state() {
ps -ef | grep $KEY | grep -v grep >/dev/null && exit 0 || exit 1
}
function del() {
find $CCTV_PATH -type d -mtime +$SAVE_DAY -exec rm -rf {}
}
case $1 in
r)
record
;;
s)
state
;;
k)
kill
;;
d)
del
;;
?)
echo "error param"
exit 1
;;
esac
定时任务
同拍照
HASS 开关
switch:
- platform: command_line
switches:
living_cctv_record:
command_on: >
nohup /mnt/db/cctv/living/record.sh r &>/dev/null &
command_off: >
/mnt/db/cctv/living/record.sh k
command_state: >
/mnt/db/cctv/living/record.sh s
command_timeout: 3