add converter.sh

This commit is contained in:
pavel.muhortov 2023-02-01 13:31:36 +03:00
parent 95eee56432
commit 45d30465eb
2 changed files with 213 additions and 3 deletions

View File

@ -2,6 +2,7 @@
PTZ IP-Camera management
* [`sequence.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#sequence-sh)
* [`converter.sh`](https://git.hmp.today/pavel.muhortov/cctv-scheduler#converter-sh)
____
## `sequence.sh`
@ -25,11 +26,60 @@ ____
Example usage in terminal with bash:
```shell
```bash
bash ./sequence.sh - ./sequence.conf
```
Example usage with cron:
```shell
crontab -e
```bash
# crontab -e
0 * * * * bash /home/user/cctv-scheduler/sequence.sh qn
```
____
## `converter.sh`
**Description:**
> JPEG to MP4 converter.
**Dependencies:**
> - bash (tested version 5.1.4 on Debian GNU/Linux 11)
> - ffmpeg (tested version 4.3.4 on Debian GNU/Linux 11)
> - 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
```

160
converter.sh Normal file
View File

@ -0,0 +1,160 @@
#! /bin/bash
#
## DESCRIPTION: JPEG to MP4 converter.
#
#
## DEPENDENCIES: sudo apt|yum install -y ffmpeg
#
#
## FUNCTIONS
#
addtologs() {
echo "$(date +'%Y.%m.%d-%H:%M:%S') $1" | tee -a $logs
}
execpause() {
read -p "Press [ENTER] to continue... "
}
execquite() {
addtologs "execution time is $(($(date +%s)-$time)) seconds, exit"
if [ "${show}" != "qn" ];then
execpause
fi
exit
}
execerror() {
addtologs "error: $1"
execquite
}
getconfig() {
logs=$(cat $conf | grep "logs=" | cut -d= -f2)
list=$(cat $conf | grep "list=" | cut -d= -f2)
imgroot=$(cat $conf | grep "imgroot=" | cut -d= -f2)
imgnames=($(cat $conf | grep "imgnames=" | cut -d= -f2))
xscale=$(cat $conf | grep "xscale=" | cut -d= -f2)
yscale=$(cat $conf | grep "yscale=" | cut -d= -f2)
mp4fps=$(cat $conf | grep "mp4fps=" | cut -d= -f2)
}
#
## PARAMETERS
#
show=${1}
conf=${2}
if [ -z "${conf}" ] || [ "${conf}" == "-" ];then
conf="$(dirname $(realpath "$0"))/$(basename -s .sh $0).conf"
fi
when=${3}
if [ -z "$when" ]; then
d=$(date +"%d")
w=$(date +"%V")
m=$(date +"%m")
y=$(date +"%Y")
duration=1
imgpath=$y/$m/$w/$d
imgname=$y.$m.$d
fi
if [ "$when" == "-d" ]; then
d=$(date -d "-1 day" +"%d")
m=$(date +"%m")
if [ "$(date -d '-1 day' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 day' +'%m')
fi
y=$(date +"%Y")
if [ "$(date -d '-1 day' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 day' +'%Y')
fi
w=$(date +"%V")
if [ "$(date +'%w')" == "1" ]; then
w=$(date -d "-1 week" +"%V")
fi
duration=1
imgpath=$y/$m/$w/$d
imgname=$y.$m.$d
fi
if [ "$when" == "-w" ]; then
w=$(date -d "-1 week" +"%V")
m=$(date +"%m")
if [ "$(date -d '-1 week' +'%m')" != "$(date +'%m')" ]; then
m=$(date -d '-1 week' +'%m')
fi
y=$(date +"%Y")
if [ "$(date -d '-1 week' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 week' +'%Y')
fi
duration=7
imgpath=$y/*/$w
imgname=$y-w$w
fi
if [ "$when" == "-m" ]; then
m=$(date -d "-1 month" +"%m")
y=$(date +"%Y")
if [ "$(date -d '-1 month' +'%Y')" != "$(date +'%Y')" ]; then
y=$(date -d '-1 month' +'%Y')
fi
duration=30
imgpath=$y/$m
imgname=$y.$m
fi
if [ "$when" == "-y" ]; then
y=$(date -d "-1 year" +"%Y")
duration=360
imgpath=$y
imgname=$y
fi
#
## VARIABLES
#
time=$(date +%s)
cd "$(dirname "$(realpath "$0")")"
if [ ! -e ${conf} ];then
execerror "Not found config file: ${conf}"
else
getconfig
fi
if [ -z "${logs}" ];then
logs=/dev/null
elif [ ! -e ${logs} ];then
touch ${logs}
fi
if [ -z "${list}" ];then
list="$(dirname $(realpath "$0"))/$(basename -s .sh $0).list"
fi
if [ ! -e ${list} ];then
touch ${list}
fi
if ! command -v ffmpeg &> /dev/null; then
execerror "Not found dependencies"
fi
#
## RUN
#
for name in ${imgnames[*]}; do
imgmatch="*$name*.jpeg"
imgarray=()
while read FILE; do
imgarray+=("${FILE}")
done < <(find $imgroot/$imgpath -name "$imgmatch" | sort)
imgcount=${#imgarray[*]}
echo '' > $list
for item in ${imgarray[*]}; do
echo file \'$item\' >> $list
done
imgdest=$imgroot/$name\_$imgname.mp4
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
ffmpeg -r $imgcount/$duration -f concat -safe 0 -i $list -c:v libx264 -vf "scale=$xscale:$yscale,fps=$mp4fps,format=yuv420p" $imgdest -y \
&& addtologs "converted $imgcount images to $imgdest with duration $duration" \
|| execerror "converted $imgcount images to $imgdest with duration $duration"
done
execquite