From 45d30465ebe02d6be90ba7bc61aba01f5851f2f8 Mon Sep 17 00:00:00 2001 From: "pavel.muhortov" Date: Wed, 1 Feb 2023 13:31:36 +0300 Subject: [PATCH] add converter.sh --- README.md | 56 +++++++++++++++++- converter.sh | 160 +++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 213 insertions(+), 3 deletions(-) create mode 100644 converter.sh diff --git a/README.md b/README.md index 2d608ce..a854dc2 100644 --- a/README.md +++ b/README.md @@ -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 + +``` diff --git a/converter.sh b/converter.sh new file mode 100644 index 0000000..42e718f --- /dev/null +++ b/converter.sh @@ -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