simple code refactoring

This commit is contained in:
pavel.muhortov 2023-03-11 22:20:57 +03:00
parent a241aa5977
commit d470736839
5 changed files with 1002 additions and 685 deletions

View File

@ -1,18 +1,26 @@
# cctv-scheduler # cctv-scheduler
PTZ IP-Camera management PTZ IP-Camera management
![cctv-scheduler](info/images/cctv-scheduler-0.1.png) ![cctv-scheduler](info/images/cctv-scheduler-0.1.png)
## `Installation` ## `Installation`
### `Requirements` ### `Requirements`
Cameras settings: Cameras settings:
>
> - Configuration -> System -> Security -> Authentication -> RTSP Authentication: digest/basic > - Configuration -> System -> Security -> Authentication -> RTSP Authentication: digest/basic
> - Configuration -> System -> Security -> Authentication -> WEB Authentication: digest/basic > - Configuration -> System -> Security -> Authentication -> WEB Authentication: digest/basic
> - Configuration -> Network -> Advanced Settings -> Integration Protocol -> Enable Hikvision-CGI: Enabled > - Configuration -> Network -> Advanced Settings -> Integration Protocol -> Enable Hikvision-CGI: Enabled
> - Configuration -> Network -> Advanced Settings -> Integration Protocol -> Hikvision-CGI Authentication: digest/basic > - Configuration -> Network -> Advanced Settings -> Integration Protocol -> Hikvision-CGI Authentication: digest/basic
Look at the description of dependencies and install the necessary. Look at the description of dependencies and install the necessary.
### `Downloading` ### `Downloading`
Download scripts and configs. Download scripts and configs.
```bash ```bash
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/sequences.sh -O /home/user/cctv-scheduler/sequences.sh wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/sequences.sh -O /home/user/cctv-scheduler/sequences.sh
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/sequences.conf -O /home/user/cctv-scheduler/sequences.conf wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/sequences.conf -O /home/user/cctv-scheduler/sequences.conf
@ -23,66 +31,84 @@ wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/publi
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/publisher-template-page-1007.xml -O /home/user/cctv-scheduler/publisher-template-page-1007.xml wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/publisher-template-page-1007.xml -O /home/user/cctv-scheduler/publisher-template-page-1007.xml
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/streaming.py -O /home/user/cctv-scheduler/streaming.py wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/streaming.py -O /home/user/cctv-scheduler/streaming.py
``` ```
### `Configuration` ### `Configuration`
Edit configs. Edit configs.
```bash ```bash
nano /home/user/cctv-scheduler/sequences.conf nano /home/user/cctv-scheduler/sequences.conf
nano /home/user/cctv-scheduler/converter.conf nano /home/user/cctv-scheduler/converter.conf
nano /home/user/cctv-scheduler/publisher.conf nano /home/user/cctv-scheduler/publisher.conf
``` ```
### `Scheduler` ### `Scheduler`
Look at examples and edit scheduler tasks: Look at examples and edit scheduler tasks:
```bash ```bash
crontab -e crontab -e
``` ```
____
* [`sequences.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#sequences-sh)
* [`converter.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#converter-sh)
* [`publisher.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#publisher-sh)
* [`streaming.py`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#streaming-py)
____ ____
- [`sequences.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#sequences-sh)
- [`converter.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#converter-sh)
- [`publisher.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#publisher-sh)
- [`streaming.py`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#streaming-py)
____
## `sequences`.sh ## `sequences`.sh
**Description:** **Description:**
> [Hikvision](https://git.hmp.today/pavel.muhortov/cctv-scheduler/src/branch/master/info/hikvision/manual/isapi.pdf) PTZ-camera sequences. > [Hikvision](https://git.hmp.today/pavel.muhortov/cctv-scheduler/src/branch/master/info/hikvision/manual/isapi.pdf) PTZ-camera sequences.
> Additionally: > Additionally:
>
> - getting temperature from DS18B20 over SSH, > - getting temperature from DS18B20 over SSH,
> - saving pictures to FTP. > - saving pictures to FTP.
> >
> This is only a local "proof of conept" for testing and debugging. > This is only a local "proof of conept" for testing and debugging.
**Dependencies:** **Dependencies:**
>
> - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [curl](https://curl.se/download.html) (tested version 7.74 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [curl](https://curl.se/download.html) (tested version 7.74 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [sshpass](https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/) (tested version 1.09 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [sshpass](https://www.cyberciti.biz/faq/noninteractive-shell-script-ssh-password-provider/) (tested version 1.09 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
| POSITION | PARAMETERS | DESCRIPTION | DEFAULT | | POSITION | PARAMETERS | DESCRIPTION | DEFAULT |
|-----------|--------------|------------------------|---------------| |-----------|--------------|------------------------|---------------|
| 1 | **[qn]** |execution without pauses|| | 1 | **[qn]** |execution without pauses||
| 2 | **[/path/to/conf]** |path to config| ./sequences.conf | | 2 | **[/path/to/conf]** |path to config| ./sequences.conf |
Example usage in terminal with bash: Example usage in terminal with bash:
```bash ```bash
bash ./sequences.sh - ./sequences.conf bash ./sequences.sh - ./sequences.conf
``` ```
Example usage with cron: Example usage with cron:
```bash ```bash
# crontab -e # crontab -e
0 * * * * bash /home/user/cctv-scheduler/sequences.sh qn 0 * * * * bash /home/user/cctv-scheduler/sequences.sh qn
``` ```
____ ____
## `converter`.sh ## `converter`.sh
**Description:** **Description:**
> JPEG to MP4 converter. > JPEG to MP4 converter.
> >
> This is only a local "proof of conept" for testing and debugging. > This is only a local "proof of conept" for testing and debugging.
**Dependencies:** **Dependencies:**
>
> - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - filesystem organization: > - filesystem organization:
>
>```bash >```bash
> # filesystem organisation example > # filesystem organisation example
>/root/ >/root/
@ -109,12 +135,14 @@ ____
| 2 | **[/path/to/conf]** |path to config| ./converter.conf | | 2 | **[/path/to/conf]** |path to config| ./converter.conf |
| 3 | **[-d\|-w\|-m\|-y]** |periods: '' - today \| '-d' - yesterday \| '-w' - last week \| '-m' - last month \| '-y' - last year|| | 3 | **[-d\|-w\|-m\|-y]** |periods: '' - today \| '-d' - yesterday \| '-w' - last week \| '-m' - last month \| '-y' - last year||
Example usage in terminal with bash for today's MP4 making: Example usage in terminal with bash for today's MP4 making:
```bash ```bash
bash ./converter.sh - ./converter.conf bash ./converter.sh - ./converter.conf
``` ```
Example usage with cron: Example usage with cron:
```bash ```bash
# crontab -e # crontab -e
1 0 * * * bash /home/user/cctv-scheduler/converter.sh qn - -d 1 0 * * * bash /home/user/cctv-scheduler/converter.sh qn - -d
@ -122,17 +150,22 @@ Example usage with cron:
30 0 1 * * bash /home/user/cctv-scheduler/converter.sh qn - -m 30 0 1 * * bash /home/user/cctv-scheduler/converter.sh qn - -m
36 0 1 1 * bash /home/user/cctv-scheduler/converter.sh qn - -y 36 0 1 1 * bash /home/user/cctv-scheduler/converter.sh qn - -y
``` ```
____ ____
## `publisher`.sh ## `publisher`.sh
**Description:** **Description:**
> Uploading MP4 to [Wordpress](https://wordpress.com/) and [Telegram](https://web.telegram.org/). > Uploading MP4 to [Wordpress](https://wordpress.com/) and [Telegram](https://web.telegram.org/).
> Additionally: > Additionally:
>
> - editing [Wordpress](https://codex.wordpress.org/XML-RPC_WordPress_API) page from template > - editing [Wordpress](https://codex.wordpress.org/XML-RPC_WordPress_API) page from template
> - recompressing video if size [over 50MB](https://core.telegram.org/bots/api#sendvideo) > - recompressing video if size [over 50MB](https://core.telegram.org/bots/api#sendvideo)
> >
> This is only a local "proof of conept" for testing and debugging. > This is only a local "proof of conept" for testing and debugging.
**Dependencies:** **Dependencies:**
>
> - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [bash](https://www.gnu.org/software/bash/) (tested version 5.1.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [curl](https://curl.se/download.html) (tested version 7.74 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [curl](https://curl.se/download.html) (tested version 7.74 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
@ -148,12 +181,14 @@ ____
| 4 | **[1\|2\|3..XXX]** |multiplier for period: '' - 1 day\|week\|month\|year|1| | 4 | **[1\|2\|3..XXX]** |multiplier for period: '' - 1 day\|week\|month\|year|1|
| 5 | **[--onlytg\|--onlywp]** |'--onlytg' - only publish to Telegram \|'--onlywp' - only publish to Wordpress|| | 5 | **[--onlytg\|--onlywp]** |'--onlytg' - only publish to Telegram \|'--onlywp' - only publish to Wordpress||
Example usage in terminal with bash for publish to Telegram today's MP4:
Example usage in terminal with bash for publish today's MP4 maked:
```bash ```bash
bash ./publisher.sh - ./publisher.conf - --onlytg bash ./publisher.sh - ./publisher.conf - - - - --onlytg
``` ```
Example usage with cron: Example usage with cron:
```bash ```bash
# crontab -e # crontab -e
1 1 * * * bash /home/user/cctv-scheduler/publisher.sh qn - -d 1 1 * * * bash /home/user/cctv-scheduler/publisher.sh qn - -d
@ -161,12 +196,16 @@ Example usage with cron:
30 1 1 * * bash /home/user/cctv-scheduler/publisher.sh qn - -m 30 1 1 * * bash /home/user/cctv-scheduler/publisher.sh qn - -m
36 1 1 1 * bash /home/user/cctv-scheduler/publisher.sh qn - -y 36 1 1 1 * bash /home/user/cctv-scheduler/publisher.sh qn - -y
``` ```
____ ____
## `streaming`.py ## `streaming`.py
**Description:** **Description:**
> FFmpeg management from Python > FFmpeg management from Python
**Dependencies:** **Dependencies:**
>
> - [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [Python 3](https://www.python.org/downloads/) (tested version 3.9.5 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/)) > - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
@ -183,17 +222,22 @@ ____
|**[--mono]**|detect ffmpeg running copy and terminate|| |**[--mono]**|detect ffmpeg running copy and terminate||
Example usage in terminal with make the script executable: Example usage in terminal with make the script executable:
```bash ```bash
chmod u+x ./streaming.py chmod u+x ./streaming.py
./streaming.py -s rtsp://user:pass@host:554/Streaming/Channels/101 --dst rtp://239.0.0.1:5554 ./streaming.py -s rtsp://user:pass@host:554/Streaming/Channels/101 --dst rtp://239.0.0.1:5554
``` ```
Example usage with cron: Example usage with cron:
```bash ```bash
# crontab -e # crontab -e
* * * * * /usr/bin/python3 /home/user/cctv-scheduler/streaming.py -s rtsp://user:pass@host:554/Streaming/Channels/video,http://Streaming/Channels/audio --dst rtmp://a.rtmp.youtube.com/live2/YOUKEY --mono --watchdog --sec 30 >> /dev/null 2>&1 * * * * * /usr/bin/python3 /home/user/cctv-scheduler/streaming.py -s rtsp://user:pass@host:554/Streaming/Channels/video,http://Streaming/Channels/audio --dst rtmp://a.rtmp.youtube.com/live2/YOUKEY --mono --watchdog --sec 30 >> /dev/null 2>&1
* * * * * /usr/bin/python3 /home/user/cctv-scheduler/streaming.py -s ~/media.mp4 --dst rtmp://b.rtmp.youtube.com/live2?backup=1/YOUKEY --mono >> /dev/null 2>&1 * * * * * /usr/bin/python3 /home/user/cctv-scheduler/streaming.py -s ~/media.mp4 --dst rtmp://b.rtmp.youtube.com/live2?backup=1/YOUKEY --mono >> /dev/null 2>&1
``` ```
Example usage in Python: Example usage in Python:
```Python ```Python
from streaming import FFmpeg from streaming import FFmpeg

View File

@ -1,165 +1,208 @@
#! /bin/bash #! /bin/bash
# DESCRIPTION:
# Converting JPEG collection to MP4.
# This is only a local "proof of conept" for testing and debugging.
# #
## DESCRIPTION: # DEPENDENCIES:
# JPEG to MP4 converter. # - ffmpeg
# This is only a local "proof of conept" for testing and debugging. #
# # PARAMETERS:
# 1: "qn" - execution without pauses
# # 2: custom configuration file path
## DEPENDENCIES: sudo apt|yum install -y ffmpeg # 3: periods: '' - today | '-d' - yesterday | '-w' - last week | '-m' - last month | '-y' - last year
# #
# FUNCTIONS:
#
## FUNCTIONS
# #
#######################################
# Print message and add to log.
# Globals:
# logs
# Arguments:
# 1: message to print and logging
#######################################
addtologs() { addtologs() {
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
} }
#######################################
# Waiting for press [ENTER].
# Globals:
# None
# Arguments:
# None
#######################################
execpause() { execpause() {
read -p "Press [ENTER] to continue... " read -r -p "Press [ENTER] to continue... "
} }
#######################################
# Exit procedure.
# Globals:
# show
# Arguments:
# None
#######################################
execquite() { execquite() {
addtologs "execution time is $(($(date +%s)-$time)) seconds, exit" addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
if [ "${show}" != "qn" ];then if [ "${show}" != "qn" ]; then
execpause execpause
fi fi
exit exit
} }
#######################################
# Error exit procedure with Telegram notification.
# Globals:
# telegramapiurl
# telegramchatid
# Arguments:
# 1: message to print and logging
#######################################
execerror() { execerror() {
addtologs "error: $1" addtologs "error: $1"
curl -s -X POST $telegramapiurl/sendMessage -d chat_id=$telegramchatid -d text="$(basename -s .sh $0) error: $1" >> /dev/null 2>&1 curl -s -X POST "${telegramapiurl}/sendMessage" \
execquite -d "chat_id=${telegramchatid}" \
-d "text=$(basename -s .sh "$0") error: $1" \
>> /dev/null 2>&1
execquite
} }
#######################################
# Parsing config file and creating global vars.
# Globals:
# None
# Arguments:
# None
#######################################
getconfig() { getconfig() {
logs=$(cat $conf | grep "logs=" | cut -d= -f2) logs=$(grep "logs=" "${conf}" | cut -d= -f2)
list=$(cat $conf | grep "list=" | cut -d= -f2) list=$(grep "list=" "${conf}" | cut -d= -f2)
imgroot=$(cat $conf | grep "imgroot=" | cut -d= -f2) imgroot=$(grep "imgroot=" "${conf}" | cut -d= -f2)
imgnames=($(cat $conf | grep "imgnames=" | cut -d= -f2)) IFS=" " read -r -a imgnames <<< "$(grep "imgnames=" "${conf}" | cut -d= -f2)"
xscale=$(cat $conf | grep "xscale=" | cut -d= -f2) xscale=$(grep "xscale=" "${conf}" | cut -d= -f2)
yscale=$(cat $conf | grep "yscale=" | cut -d= -f2) yscale=$(grep "yscale=" "${conf}" | cut -d= -f2)
mp4fps=$(cat $conf | grep "mp4fps=" | cut -d= -f2) mp4fps=$(grep "mp4fps=" "${conf}" | cut -d= -f2)
telegramapiurl=$(cat $conf | grep "telegramapiurl=" | cut -d= -f2) telegramapiurl=$(grep "telegramapiurl=" "${conf}" | cut -d= -f2)
telegramchatid=$(cat $conf | grep "telegramchatid=" | cut -d= -f2) telegramchatid=$(grep "telegramchatid=" "${conf}" | cut -d= -f2)
} }
# #
## PARAMETERS ## VARIABLES:
# #
show=${1} show=$1
conf=${2} conf=$2
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then
conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf" conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf"
fi fi
when=${3} when=$3
if [ -z "$when" ]; then if [ -z "${when}" ]; then
d=$(date +"%d") d=$(date +"%d")
w=$(date +"%V") w=$(date +"%V")
m=$(date +"%m") m=$(date +"%m")
y=$(date +"%Y") y=$(date +"%Y")
duration=1 duration=1
imgpath=$y/$m/$w/$d imgpath="${y}/${m}/${w}/${d}"
imgname=$y.$m.$d imgname="${y}.${m}.${d}"
fi fi
if [ "$when" == "-d" ]; then if [ "${when}" == "-d" ]; then
d=$(date -d "-1 day" +"%d") d=$(date -d "-1 day" +"%d")
m=$(date +"%m") m=$(date +"%m")
if [ "$(date -d '-1 day' +'%m')" != "$(date +'%m')" ]; then if [ "$(date -d '-1 day' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 day' +'%m') m=$(date -d '-1 day' +'%m')
fi fi
y=$(date +"%Y") y=$(date +"%Y")
if [ "$(date -d '-1 day' +'%Y')" != "$(date +'%Y')" ]; then if [ "$(date -d '-1 day' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 day' +'%Y') y=$(date -d '-1 day' +'%Y')
fi fi
w=$(date +"%V") w=$(date +"%V")
if [ "$(date +'%w')" == "1" ]; then if [ "$(date +'%w')" == "1" ]; then
w=$(date -d "-1 week" +"%V") w=$(date -d "-1 week" +"%V")
fi fi
duration=1 duration=1
imgpath=$y/$m/$w/$d imgpath="${y}/${m}/${w}/${d}"
imgname=$y.$m.$d imgname="${y}.${m}.${d}"
fi fi
if [ "$when" == "-w" ]; then if [ "${when}" == "-w" ]; then
w=$(date -d "-1 week" +"%V") w=$(date -d "-1 week" +"%V")
m=$(date +"%m") m=$(date +"%m")
if [ "$(date -d '-1 week' +'%m')" != "$(date +'%m')" ]; then if [ "$(date -d '-1 week' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 week' +'%m') m=$(date -d '-1 week' +'%m')
fi fi
y=$(date +"%Y") y=$(date +"%Y")
if [ "$(date -d '-1 week' +'%Y')" != "$(date +'%Y')" ]; then if [ "$(date -d '-1 week' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 week' +'%Y') y=$(date -d '-1 week' +'%Y')
fi fi
duration=7 duration=7
imgpath=$y/*/$w imgpath="${y}/*/${w}"
imgname=$y-w$w imgname="${y}-w${w}"
fi fi
if [ "$when" == "-m" ]; then if [ "${when}" == "-m" ]; then
m=$(date -d "-1 month" +"%m") m=$(date -d "-1 month" +"%m")
y=$(date +"%Y") y=$(date +"%Y")
if [ "$(date -d '-1 month' +'%Y')" != "$(date +'%Y')" ]; then if [ "$(date -d '-1 month' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 month' +'%Y') y=$(date -d '-1 month' +'%Y')
fi fi
duration=30 duration=30
imgpath=$y/$m imgpath="${y}/${m}"
imgname=$y.$m imgname="${y}.${m}"
fi fi
if [ "$when" == "-y" ]; then if [ "${when}" == "-y" ]; then
y=$(date -d "-1 year" +"%Y") y=$(date -d "-1 year" +"%Y")
duration=360 duration=360
imgpath=$y imgpath="${y}"
imgname=$y imgname="${y}"
fi fi
#
## VARIABLES
#
time=$(date +%s) time=$(date +%s)
cd "$(dirname "$(realpath "$0")")" cd "$(dirname "$(realpath "$0")")" || execerror
if [ ! -e ${conf} ];then if [ ! -e "${conf}" ]; then
execerror "Not found config file: ${conf}" execerror "Not found config file: ${conf}"
else else
getconfig getconfig
fi fi
if [ -z "${logs}" ];then if [ -z "${logs}" ];then
logs=/dev/null logs=/dev/null
elif [ ! -e ${logs} ];then elif [ ! -e "${logs}" ];then
touch ${logs} touch "${logs}"
fi fi
if [ -z "${list}" ];then if [ -z "${list}" ];then
list="$(dirname $(realpath "$0"))/$(basename -s .sh $0).list" list="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").list"
fi fi
if [ ! -e ${list} ];then if [ ! -e "${list}" ];then
touch ${list} touch "${list}"
fi fi
if ! command -v ffmpeg &> /dev/null; then if ! command -v ffmpeg &> /dev/null; then
execerror "Not found dependencies" execerror "Not found dependencies"
fi fi
# #
## RUN # MAIN:
# #
for name in ${imgnames[*]}; do for name in "${imgnames[@]}"; do
imgmatch="*$name*.jpeg" imgmatch="*${name}*.jpeg"
imgarray=() imgarray=()
while read FILE; do while read -r FILE; do
imgarray+=("${FILE}") imgarray+=("${FILE}")
done < <(find $imgroot/$imgpath -name "$imgmatch" | sort) done < <(find "${imgroot}/${imgpath}" -name "${imgmatch}" | sort)
imgcount=${#imgarray[*]} imgcount=${#imgarray[*]}
echo '' > $list echo '' > "${list}"
for item in ${imgarray[*]}; do for item in "${imgarray[@]}"; do
echo file \'$item\' >> $list echo file \'"${item}"\' >> "${list}"
done done
imgdest=$imgroot/$name\_$imgname.mp4 imgdest="${imgroot}/${name}_${imgname}.mp4"
echo $imgdest echo "${imgdest}"
#ffmpeg -r $imgcount/$duration -f concat -safe 0 -i $list -f lavfi -i anullsrc=channel_layout=stereo:sample_rate=44100 -t $duration -c:v libx264 -vf "scale=$xscale:$yscale,fps=$mp4fps,format=yuv420p" -c:a aac $imgdest -y if ffmpeg -r "${imgcount}/${duration}" -f concat -safe 0 -i "${list}" \
ffmpeg -r $imgcount/$duration -f concat -safe 0 -i $list -c:v libx264 -vf "scale=$xscale:$yscale,fps=$mp4fps,format=yuv420p" $imgdest -y \ -c:v libx264 -vf "scale=${xscale}:${yscale},fps=${mp4fps},format=yuv420p" \
&& addtologs "converted $imgcount images to $imgdest with duration $duration" \ "${imgdest}" -y; then
|| execerror "converted $imgcount images to $imgdest with duration $duration" addtologs "converted ${imgcount} images to ${imgdest} with duration ${duration}"
else
execerror "converted ${imgcount} images to ${imgdest} with duration ${duration}"
fi
done done
execquite execquite

File diff suppressed because it is too large Load Diff

View File

@ -9,6 +9,7 @@ ftpuser=user
ftppass=pass ftppass=pass
ftpaddr=192.168.254.254 ftpaddr=192.168.254.254
ftpport=21 ftpport=21
ftppath=Records/cam.test.local
tmpuser=user tmpuser=user
tmppass=pass tmppass=pass

View File

@ -1,137 +1,291 @@
#! /bin/bash #! /bin/bash
# DESCRIPTION:
# Handling Hikvision PTZ-cameras sequences.
# Additionally:
# - get ds18b20 sensor value over SSH,
# - saving pictures to FTP.
# This is only a local "proof of conept" for testing and debugging.
# #
## DESCRIPTION: # DEPENDENCIES:
# Hikvision PTZ-camera sequences. # - curl
# Additionally: # - sshpass
# - getting temperature from ds-18b20 over SSH, #
# - saving pictures to FTP. # PARAMETERS:
# This is only a local "proof of conept" for testing and debugging. # 1: "qn" - execution without pauses
# # 2: custom configuration file path
#
# # FUNCTIONS:
## DEPENDENCIES: sudo apt|yum install -y curl sshpass
#
#
## FUNCTIONS
# #
#######################################
# Print message and add to log.
# Globals:
# logs
# Arguments:
# 1: message to print and logging
#######################################
addtologs() { addtologs() {
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a "${logs}"
} }
#######################################
# Waiting for press [ENTER].
# Globals:
# None
# Arguments:
# None
#######################################
execpause() { execpause() {
read -p "Press [ENTER] to continue... " read -r -p "Press [ENTER] to continue... "
} }
#######################################
# Exit procedure.
# Globals:
# show
# Arguments:
# None
#######################################
execquite() { execquite() {
addtologs "execution time is $(($(date +%s)-$time)) seconds, exit" addtologs "execution time is $(($(date +%s)-time)) seconds, exit"
if [ "${show}" != "qn" ];then if [ "${show}" != "qn" ]; then
execpause execpause
fi fi
exit exit
} }
#######################################
# Error exit procedure with Telegram notification.
# Globals:
# telegramapiurl
# telegramchatid
# Arguments:
# 1: message to print and logging
#######################################
execerror() { execerror() {
addtologs "error: $1" addtologs "error: $1"
curl -s -X POST $telegramapiurl/sendMessage -d chat_id=$telegramchatid -d text="$(basename -s .sh $0) error: $1" >> /dev/null 2>&1 curl -s -X POST "${telegramapiurl}/sendMessage" \
execquite -d "chat_id=${telegramchatid}" \
-d "text=$(basename -s .sh "$0") error: $1" \
>> /dev/null 2>&1
execquite
} }
#######################################
# Parsing config file and creating global vars.
# Globals:
# None
# Arguments:
# None
#######################################
getconfig() { getconfig() {
logs=$(cat $conf | grep "logs=" | cut -d= -f2) logs=$(grep "logs=" "${conf}" | cut -d= -f2)
camuser=$(cat $conf | grep "camuser=" | cut -d= -f2) camuser=$(grep "camuser=" "${conf}" | cut -d= -f2)
campass=$(cat $conf | grep "campass=" | cut -d= -f2) campass=$(grep "campass=" "${conf}" | cut -d= -f2)
camaddr=$(cat $conf | grep "camaddr=" | cut -d= -f2) camaddr=$(grep "camaddr=" "${conf}" | cut -d= -f2)
camport=$(cat $conf | grep "camport=" | cut -d= -f2) camport=$(grep "camport=" "${conf}" | cut -d= -f2)
ftpuser=$(cat $conf | grep "ftpuser=" | cut -d= -f2) ftpuser=$(grep "ftpuser=" "${conf}" | cut -d= -f2)
ftppass=$(cat $conf | grep "ftppass=" | cut -d= -f2) ftppass=$(grep "ftppass=" "${conf}" | cut -d= -f2)
ftpaddr=$(cat $conf | grep "ftpaddr=" | cut -d= -f2) ftpaddr=$(grep "ftpaddr=" "${conf}" | cut -d= -f2)
ftpport=$(cat $conf | grep "ftpport=" | cut -d= -f2) ftpport=$(grep "ftpport=" "${conf}" | cut -d= -f2)
tmpuser=$(cat $conf | grep "tmpuser=" | cut -d= -f2) ftppath=$(grep "ftppath=" "${conf}" | cut -d= -f2)
tmppass=$(cat $conf | grep "tmppass=" | cut -d= -f2) tmpuser=$(grep "tmpuser=" "${conf}" | cut -d= -f2)
tmpaddr=$(cat $conf | grep "tmpaddr=" | cut -d= -f2) tmppass=$(grep "tmppass=" "${conf}" | cut -d= -f2)
tmpport=$(cat $conf | grep "tmpport=" | cut -d= -f2) tmpaddr=$(grep "tmpaddr=" "${conf}" | cut -d= -f2)
tmpnode=$(cat $conf | grep "tmpnode=" | cut -d= -f2) tmpport=$(grep "tmpport=" "${conf}" | cut -d= -f2)
telegramapiurl=$(cat $conf | grep "telegramapiurl=" | cut -d= -f2) tmpnode=$(grep "tmpnode=" "${conf}" | cut -d= -f2)
telegramchatid=$(cat $conf | grep "telegramchatid=" | cut -d= -f2) telegramapiurl=$(grep "telegramapiurl=" "${conf}" | cut -d= -f2)
telegramchatid=$(grep "telegramchatid=" "${conf}" | cut -d= -f2)
} }
#######################################
# Get ds18b20 sensor value over SSH.
# Globals:
# tmpaddr
# tmpport
# tmpuser
# tmppass
# tmpnode
# Arguments:
# None
# return:
# temperature in Celsius
#######################################
getairtmp() { getairtmp() {
local temp=$( \ temp=$(sshpass -p "${tmppass}" \
sshpass -p $tmppass ssh $tmpuser@$tmpaddr -p $tmpport -o StrictHostKeyChecking=no -o ConnectTimeout=10 \ ssh "${tmpuser}@${tmpaddr}" -p "${tmpport}" \
"cat /sys/bus/w1/devices/$tmpnode/temperature" \ -o StrictHostKeyChecking=no -o ConnectTimeout=10 \
) && temp=$(($temp / 1000)) "cat /sys/bus/w1/devices/${tmpnode}/temperature" \
if [ ! -z "${temp}" ]; then ) && temp=$((temp / 1000))
addtologs "air temperature now is $temp'C" > /dev/null if [ -n "${temp}" ]; then
echo $temp addtologs "getairtmp: air temperature now is ${temp}'C" > /dev/null
else echo "${temp}"
execerror "air temperature now is $temp'C" else
fi execerror "getairtmp: air temperature now is ${temp}'C"
fi
} }
#######################################
# Get static picture from camera and upload to FTP.
# Globals:
# camaddr
# camport
# camuser
# campass
# ftpaddr
# ftpport
# ftppath
# ftpuser
# ftppass
# Arguments:
# 1: filename prefix
#######################################
getcamimg() { getcamimg() {
local name=$1 local name
local file="$ftpaddr:$ftpport/Records/cam-street.home.local/$(date +"%Y")/$(date +"%m")/$(date +"%V")/$(date +"%d")/${name}_$(date +"%Y.%m.%d_%H-%M-%S").jpeg" name=$1
url='/Streaming/channels/101/picture?snapShotImageType=JPEG&videoResolutionWidth=1920&videoResolutionHeight=1080' path="${ftppath}/$(date +"%Y")/$(date +"%m")/$(date +"%V")/$(date +"%d")"
curl -vs "http://$camuser:$campass@$camaddr:$camport/$url" -o tmp \ file="${ftpaddr}:${ftpport}/${path}/${name}_$(date +"%Y.%m.%d_%H-%M-%S").jpeg"
&& curl -T tmp "ftp://$ftpuser:$ftppass@$file" --ftp-create-dirs \ url='Streaming/channels/101/picture?snapShotImageType=JPEG&videoResolutionWidth=1920&videoResolutionHeight=1080'
&& rm tmp && addtologs "save photo to ftp://$file" || execerror "save photo to ftp://$file" if curl -vs "http://$camuser:$campass@$camaddr:$camport/$url" -o tmp \
&& curl -T tmp "ftp://$ftpuser:$ftppass@$file" --ftp-create-dirs; then
addtologs "getcamimg: save photo to ftp://${file}"
rm tmp
else
execerror "getcamimg: save photo to ftp://${file}"
fi
} }
#######################################
# Set message as video overlay text.
# Globals:
# camaddr
# camport
# camuser
# campass
# Arguments:
# 1: overlay text content
#######################################
setcamtxt() { setcamtxt() {
local message=$1 local message=$1
url='/Video/inputs/channels/101/overlays/text' url='ISAPI/System/Video/inputs/channels/101/overlays/text'
xml='<TextOverlayList><TextOverlay><id>1</id><enabled>true</enabled><posX>0</posX><posY>0</posY><message>'"${message}"'</message></TextOverlay></TextOverlayList>' xml='<TextOverlayList>
curl -d "$xml" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" && addtologs "text on video is $message" && sleep 5 || execerror "text on video is $message" <TextOverlay>
<id>1</id>
<enabled>true</enabled>
<posX>0</posX><posY>0</posY>
<displayText>'"${message}"'</displayText>
</TextOverlay>
</TextOverlayList>'
if curl -d "${xml}" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" \
| grep -q '<statusString>OK</statusString>'; then
addtologs "setcamtxt: ${message}"
sleep 5
else
execerror "setcamtxt: ${message}"
fi
} }
#######################################
# Set camera moving to absolute position.
# Globals:
# camaddr
# camport
# camuser
# campass
# Arguments:
# 1: horisontal camera position from 0 to 3600
# 2: vertical camera position from -900 to 2700
# 3: zoom camera position from 0 to 1000
# 4: time to wait
# 5: message to print and logging
#######################################
setcampos() { setcampos() {
local x=$1 local x=$1
local y=$2 local y=$2
local z=$3 local z=$3
local t=$4 local t=$4
local message=$5 local message=$5
url='/ISAPI/PTZCtrl/channels/101/absolute' url='ISAPI/PTZCtrl/channels/101/absolute'
xml='<PTZData><AbsoluteHigh><elevation>'"${y}"'</elevation><azimuth>'"${x}"'</azimuth><absoluteZoom>'"${z}"'</absoluteZoom></AbsoluteHigh></PTZData>' xml='<PTZData>
curl -d "$xml" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" && addtologs "$message" && sleep $t || execerror "$message" <AbsoluteHigh>
<elevation>'"${y}"'</elevation>
<azimuth>'"${x}"'</azimuth>
<absoluteZoom>'"${z}"'</absoluteZoom>
</AbsoluteHigh>
</PTZData>'
if curl -d "${xml}" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" \
| grep -q '<statusString>OK</statusString>'; then
addtologs "setcampos: ${message}"
sleep "${t}"
else
execerror "setcampos: ${message}"
fi
} }
#######################################
# Set camera moving to direction.
# Globals:
# camaddr
# camport
# camuser
# campass
# Arguments:
# 1: acceleration of horizontal camera movement from -100 to 100
# 2: acceleration of vertical camera movement from -100 to 100
# 3: acceleration of zoom camera movement from -100 to 100
# 4: time to wait
# 5: message to print and logging
#######################################
setcammov() { setcammov() {
local x=$1 local x=$1
local y=$2 local y=$2
local z=$3 local z=$3
local t=$4 local t=$4
local message=$5 local message=$5
url='/ISAPI/PTZCtrl/channels/101/continuous' url='ISAPI/PTZCtrl/channels/101/continuous'
xml='<PTZData><pan>'"${x}"'</pan><tilt>'"${y}"'</tilt><zoom>'"${z}"'</zoom></PTZData>' xml='<PTZData>
curl -d "$xml" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" && addtologs "$message" && sleep $t || execerror "$message" <pan>'"${x}"'</pan>
<tilt>'"${y}"'</tilt>
<zoom>'"${z}"'</zoom>
</PTZData>'
if curl -d "${xml}" -X PUT "http://$camuser:$campass@$camaddr:$camport/$url" \
| grep -q '<statusString>OK</statusString>'; then
addtologs "setcammov: ${message}"
sleep "${t}"
else
execerror "setcammov: ${message}"
fi
} }
# #
## PARAMETERS ## VARIABLES:
# #
show=${1} show=$1
conf=${2} conf=$2
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then if [ -z "${conf}" ] || [ "${conf}" == "-" ]; then
conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf" conf="$(dirname "$(realpath "$0")")/$(basename -s .sh "$0").conf"
fi fi
#
## VARIABLES
#
time=$(date +%s) time=$(date +%s)
cd "$(dirname "$(realpath "$0")")" cd "$(dirname "$(realpath "$0")")" || execerror
if [ ! -e ${conf} ];then if [ ! -e "${conf}" ]; then
execerror "Not found config file: ${conf}" execerror "Not found config file: ${conf}"
else else
getconfig getconfig
fi fi
if [ -z "${logs}" ];then if [ -z "${logs}" ];then
logs=/dev/null logs=/dev/null
elif [ ! -e ${logs} ];then elif [ ! -e "${logs}" ];then
touch ${logs} touch "${logs}"
fi fi
if ! command -v curl &> /dev/null || ! command -v sshpass &> /dev/null; then if ! command -v curl &> /dev/null || ! command -v sshpass &> /dev/null; then
execerror "Not found dependencies" execerror "Not found dependencies"
fi fi
# #
## RUN # MAIN:
# #
setcamtxt "$(getairtmp)'C" setcamtxt "$(getairtmp)'C"
@ -162,12 +316,13 @@ sleep 4m
setcammov 0 -44 33 1.50s '08 >> 09 Точка - Стадион 8x' setcammov 0 -44 33 1.50s '08 >> 09 Точка - Стадион 8x'
setcampos 2860 -170 80 5.00s '08 >| 09 Точка - Стадион 8x' setcampos 2860 -170 80 5.00s '08 >| 09 Точка - Стадион 8x'
sleep 1m sleep 1m
setcammov 0 -99 0 7.50s '09 >> 10 Точка - Въезд во двор 8x'
setcampos 2805 200 80 5.00s '09 >| 10 Точка - Въезд во двор 8x' setcampos 2805 200 80 5.00s '09 >| 10 Точка - Въезд во двор 8x'
sleep 5m sleep 5m
setcammov -88 99 0 5.00s '10 >> 11 Точка - Бассейн 8x' setcammov -88 99 0 5.00s '10 >> 11 Точка - Бассейн 8x'
setcammov -99 88 0 2.00s '10 >> 11 Точка - Бассейн 8x' setcammov -99 88 0 2.00s '10 >> 11 Точка - Бассейн 8x'
setcammov -44 99 0 4.25s '10 >> 11 Точка - Бассейн 8x' setcammov -44 99 0 4.25s '10 >> 11 Точка - Бассейн 8x'
setcampos 2510 -100 80 5.00s '10 >| 11 Точка - Бассейн 8x' setcampos 2510 -20 80 5.00s '10 >| 11 Точка - Бассейн 8x'
sleep 1m sleep 1m
setcammov -88 99 33 2.50s '11 >> 12 Точка - Общежитие дальнее 16x' setcammov -88 99 33 2.50s '11 >> 12 Точка - Общежитие дальнее 16x'
setcampos 2415 -200 160 5.00s '11 >| 12 Точка - Общежитие дальнее 16x' setcampos 2415 -200 160 5.00s '11 >| 12 Точка - Общежитие дальнее 16x'
@ -176,7 +331,7 @@ setcammov 66 0 0 22.5s '12 >> 13 Точка - Общ
setcampos 2640 -200 160 5.00s '12 >| 13 Точка - Общежите ближнее 16x' setcampos 2640 -200 160 5.00s '12 >| 13 Точка - Общежите ближнее 16x'
sleep 1m sleep 1m
setcammov 66 0 0 90.5s '13 >> 14 Точка - Крыша магазина 4x' setcammov 66 0 0 90.5s '13 >> 14 Точка - Крыша магазина 4x'
setcammov 99 0 -33 3.25s '13 >> 14 Точка - Крыша магазина 4x' setcammov 99 0 -44 3.25s '13 >> 14 Точка - Крыша магазина 4x'
setcampos 335 -200 40 5.00s '13 >| 14 Точка - Крыша магазина 4x' setcampos 335 -200 40 5.00s '13 >| 14 Точка - Крыша магазина 4x'
sleep 1m sleep 1m
setcamtxt '' && getcamimg 'point-11' && setcamtxt "$(getairtmp)'C" setcamtxt '' && getcamimg 'point-11' && setcamtxt "$(getairtmp)'C"