# cctv-scheduler

PTZ IP-Camera management

____

- [`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)

____

![cctv-scheduler](info/images/cctv-scheduler-0.1.png)

## `Installation`

### `Requirements`

Cameras settings:
>
> - Configuration -> System -> Security -> Authentication -> RTSP 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 -> Hikvision-CGI Authentication: digest/basic

Look at the description of dependencies and install the necessary.

### `Downloading`

Download scripts and configs.

```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.conf -O /home/user/cctv-scheduler/sequences.conf
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/converter.sh -O /home/user/cctv-scheduler/converter.sh
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/converter.conf -O /home/user/cctv-scheduler/converter.conf
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/publisher.sh -O /home/user/cctv-scheduler/publisher.sh
wget https://git.hmp.today/pavel.muhortov/cctv-scheduler/raw/branch/master/publisher.conf -O /home/user/cctv-scheduler/publisher.conf
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
```

### `Configuration`

Edit configs.

```bash
nano /home/user/cctv-scheduler/sequences.conf
nano /home/user/cctv-scheduler/converter.conf
nano /home/user/cctv-scheduler/publisher.conf
```

### `Scheduler`

Look at examples and edit scheduler tasks:

```bash
crontab -e
```

## `sequences`.sh

**Description:**  
> [Hikvision](https://git.hmp.today/pavel.muhortov/cctv-scheduler/src/branch/master/info/hikvision/manual/isapi.pdf) PTZ-camera sequences.  
> Additionally:
>
> - getting temperature from DS18B20 over SSH,
> - saving pictures to FTP.
>
> This is only a local "proof of conept" for testing and debugging.

**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/))
> - [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/))

|  POSITION | PARAMETERS   |       DESCRIPTION      |    DEFAULT    |
|-----------|--------------|------------------------|---------------|
|     1     |    **[qn]**    |execution without pauses||
|     2     |    **[/path/to/conf]**    |path to config|     ./sequences.conf    |

Example usage in terminal with bash:

```bash
bash ./sequences.sh - ./sequences.conf
```

Example usage with cron:

```bash
# crontab -e
0  *  *  *  * bash /home/user/cctv-scheduler/sequences.sh qn
```

____

## `converter`.sh

**Description:**  
> JPEG to MP4 converter.
>
> This is only a local "proof of conept" for testing and debugging.

**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/))
> - [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:
>
>```bash
> # filesystem organisation example
>/root/
>      /2022/
>           /12/
>              /52/
>                 /31/
>                    /image-01_2022.12.31_time.jpeg
>                    /image-02_2022.12.31_time.jpeg
>      /2023/
>           /01/
>              /01/
>                 /02/
>                    /image-01_2023.01.02_time.jpeg
>                    /image-02_2023.01.02_time.jpeg
>                 /03/
>                    /image-01_2023.01.03_time.jpeg
>                    /image-02_2023.01.03_time.jpeg
>```

|  POSITION | PARAMETERS   |       DESCRIPTION      |    DEFAULT    |
|-----------|--------------|------------------------|---------------|
|     1     |    **[qn]**    |execution without pauses||
|     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||

Example usage in terminal with bash for today's MP4 making:

```bash
bash ./converter.sh - ./converter.conf
```

Example usage with cron:

```bash
# crontab -e
1  0  *  *  * bash /home/user/cctv-scheduler/converter.sh qn - -d
7  0  *  *  1 bash /home/user/cctv-scheduler/converter.sh qn - -w
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
```

____

## `publisher`.sh

**Description:**
> Uploading MP4 to [Wordpress](https://wordpress.com/) and [Telegram](https://web.telegram.org/).  
> Additionally:
>
> - 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)
>
> This is only a local "proof of conept" for testing and debugging.

**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/))
> - [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/))
> - [libxml2-utils](https://gitlab.gnome.org/GNOME/libxml2) (tested version 2.9.10 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
> - [jq](https://stedolan.github.io/jq/download/) (tested version 1.6 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))
>

|  POSITION | PARAMETERS   |       DESCRIPTION      |    DEFAULT    |
|-----------|--------------|------------------------|---------------|
|     1     |    **[qn]**    |execution without pauses||
|     2     |    **[/path/to/conf]**    |path to config|`./converter.conf`|
|     3     |    **[-d\|-w\|-m\|-y]**    |periods: '' - 0 day \| '-d' - -X day \| '-w' - -X week \| '-m' - -X month \| '-y' - -X year||
|     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||

Example usage in terminal with bash for publish to Telegram today's MP4:

```bash
bash ./publisher.sh - ./publisher.conf - - - - --onlytg
```

Example usage with cron:

```bash
# crontab -e
1  1  *  *  * bash /home/user/cctv-scheduler/publisher.sh qn - -d
7  1  *  *  1 bash /home/user/cctv-scheduler/publisher.sh qn - -w
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
```

____

## `streaming`.py

**Description:**
> FFmpeg management from Python  

**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/))
> - [ffmpeg](https://ffmpeg.org/download.html) (tested version 4.3.4 on [Debian GNU/Linux 11](http://ftp.debian.org/debian/dists/bullseye/))

|  PARAMETERS | DESCRIPTION | DEFAULT|
|-------------|-------------|--------|
|**-s**, **--src**|sources urls|**REQUIRED**|
|**[-h]**|print help and exit||
|**[--preset]**|240p, 360p, 480p, 720p, 1080p, 1440p, 2160p|`None`|
|**[--fps]**|frame per second encoding output|`None`|
|**[--dst]**|destination url|`None`|
|**[--ffpath]**|alternative path to bin|`None`|
|**[--watchdog]**|detect ffmpeg freeze and terminate||
|**[--sec]**|seconds to wait before the watchdog terminates|`15`|
|**[--mono]**|detect ffmpeg running copy and terminate||

Example usage in terminal with make the script executable:  

```bash
chmod u+x ./streaming.py
./streaming.py -s rtsp://user:pass@host:554/Streaming/Channels/101 --dst rtp://239.0.0.1:5554
```

Example usage with cron:

```bash
# 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 ~/media.mp4 --dst rtmp://b.rtmp.youtube.com/live2?backup=1/YOUKEY --mono >> /dev/null 2>&1
```

Example usage in Python:

```Python
from streaming import FFmpeg

FFmpeg.run(src='null, anull', preset='240p', fps=10)
```
