From 8e414228dc557647ed8592ee2a1e5e42b8bc738f Mon Sep 17 00:00:00 2001 From: S D Rausty <27742457+SDRausty@users.noreply.github.com> Date: Sun, 31 Oct 2021 04:16:23 -0400 Subject: [PATCH 1/4] Create termux-camera-record --- termux-camera-record | 62 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 termux-camera-record diff --git a/termux-camera-record b/termux-camera-record new file mode 100644 index 0000000..0117f00 --- /dev/null +++ b/termux-camera-record @@ -0,0 +1,62 @@ +#!/data/data/com.termux/files/usr/bin/env bash +set -eu +CAMID=${1:-0} # camera 0 1 2 3 4 5 6 7 id +FRAMECTOT=${2:-255} # video frame count total +FRAMERATE=${3:-4} # video 2 4 8 16 32 frames per second rendered +THRESHOLDSET=${4:-1024} # byte difference 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 between last two picture frames taken +_CAMS_ () { + FRAMECOUNT=0 + while [ "$FRAMECOUNT" -le "$FRAMECTOT" ] + do + FRAMENAME="$(printf '%s.%04d.jpg' "${PWD##*/}$CAMID" "$FRAMECOUNT")" + printf '%s\n%s\n' "I $((FRAMECOUNT + 1))/$((FRAMECTOT + 1)) frame count; ${THRESHOLDSET:-} threshold" "I $CAMID camid taking picture $FRAMENAME" + sleep 0.0"$(shuf -i 101-420 -n 1)" + "$PREFIX"/libexec/termux-api CameraPhoto --es camera "$CAMID" --es file "$PWD/$FRAMENAME" + _ISZERO_ + done +} +_CHECKMOTIONDIFF_() { + if [ "$FRAMECOUNT" -ne 0 ] + then + THRESHOLD="$((LASTZERO - ISZERO))" + THRESHOLD="${THRESHOLD//-}" + if [ "$THRESHOLD" -le "$THRESHOLDSET" ] + then + printf '%s\n%s\n' "D $THRESHOLD threshold; deleting file $FRAMENAME" "I frame $FRAMENAME; Threshold set to $THRESHOLDSET" + rm -f "$FRAMENAME" + else + printf '%s\n' "S $THRESHOLD threshold; saving file $FRAMENAME" + FRAMECOUNT="$((FRAMECOUNT + 1))" + fi + else + FRAMECOUNT="$((FRAMECOUNT + 1))" + fi +} +_ISZERO_ () { + if [ -n "${ISZERO:-}" ] + then + LASTZERO="$ISZERO" + fi + ISZERO="$(find . -type f -name "$FRAMENAME" -printf "%s\n" )" + if [ "$ISZERO" -eq 0 ] + then + if [ "$FRAMECOUNT" -eq 0 ] + then + printf '%s\n' "E Could not begin shoot; Please check for issues and run '${0##*/}' again; Exiting..." + rm -f "$FRAMENAME" + exit 1 + else + printf '%s\n' "W deleting zero size file $FRAMENAME" + rm -f "$FRAMENAME" + fi + else + _CHECKMOTIONDIFF_ + fi +} +_CAMS_ +VIDEOPREFIX="${FRAMENAME%%.*}." +TIMESTAMP="$(date +%Y%m%d%H%M)" +printf '%s\n' "Making $VIDEOPREFIX$TIMESTAMP.mp4; Please wait..." +nice -n 20 ffmpeg -threads 1 -loglevel -8 -framerate "$FRAMERATE" -i "$VIDEOPREFIX"%04d.jpg -vcodec libx264 -acodec aac "$VIDEOPREFIX$TIMESTAMP".mp4 && printf '%s\n' "Finished making $VIDEOPREFIX$TIMESTAMP.mp4:" +ls -al "$VIDEOPREFIX$TIMESTAMP".mp4 +## termux-camera-record EOF From bc655dbd0c984c2e50907da35a060c3c94b5a398 Mon Sep 17 00:00:00 2001 From: S D Rausty <27742457+SDRausty@users.noreply.github.com> Date: Sun, 31 Oct 2021 04:30:01 -0400 Subject: [PATCH 2/4] Update termux-camera-record --- termux-camera-record | 1 + 1 file changed, 1 insertion(+) diff --git a/termux-camera-record b/termux-camera-record index 0117f00..e3306f4 100644 --- a/termux-camera-record +++ b/termux-camera-record @@ -1,4 +1,5 @@ #!/data/data/com.termux/files/usr/bin/env bash +# This script enables timelapse camera recording with motion detection in Termux. set -eu CAMID=${1:-0} # camera 0 1 2 3 4 5 6 7 id FRAMECTOT=${2:-255} # video frame count total From 45ec57e0663ac058f6c4c857fa32caba97cd8a76 Mon Sep 17 00:00:00 2001 From: S D Rausty <27742457+SDRausty@users.noreply.github.com> Date: Sun, 31 Oct 2021 04:57:10 -0400 Subject: [PATCH 3/4] Update termux-camera-record --- termux-camera-record | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/termux-camera-record b/termux-camera-record index e3306f4..b8789e3 100644 --- a/termux-camera-record +++ b/termux-camera-record @@ -1,4 +1,4 @@ -#!/data/data/com.termux/files/usr/bin/env bash +#!@TERMUX_PREFIX@/bin/env bash # This script enables timelapse camera recording with motion detection in Termux. set -eu CAMID=${1:-0} # camera 0 1 2 3 4 5 6 7 id From 8552bc367f6dab0607297e9826ae4beeb7b4d42a Mon Sep 17 00:00:00 2001 From: S D Rausty <27742457+SDRausty@users.noreply.github.com> Date: Tue, 2 Nov 2021 10:33:24 -0400 Subject: [PATCH 4/4] Update termux-camera-record --- termux-camera-record | 141 ++++++++++++++++++++++++++----------------- 1 file changed, 87 insertions(+), 54 deletions(-) diff --git a/termux-camera-record b/termux-camera-record index b8789e3..ea9768a 100644 --- a/termux-camera-record +++ b/termux-camera-record @@ -1,63 +1,96 @@ #!@TERMUX_PREFIX@/bin/env bash -# This script enables timelapse camera recording with motion detection in Termux. +## Example usage: 'cams.bash 0 255 16 2048 r 90 2' +## Semantic usage: 'cams.bash [0 [255 [16 [2048 [r[otate] [90 [2]]]]]]]' +## Please run 'pkg install ffmpeg imagemagick termux-api' before running this script. Also ensure that Termux-api is installed, which is available at this https://github.com/termux/termux-api/actions/workflows/debug_build.yml webpage. +## VLC APK can be downloaded from these https://www.videolan.org/vlc/download-android.html and https://get.videolan.org/vlc-android/3.3.4/ webpages. +## Options in addition to image rotation can be added by editing this script at the magick rotation command; The command line options for magick are listed at this https://imagemagick.org/script/command-line-options.php webpage. +## All script arguments are listed below, including their defaults: set -eu -CAMID=${1:-0} # camera 0 1 2 3 4 5 6 7 id -FRAMECTOT=${2:-255} # video frame count total -FRAMERATE=${3:-4} # video 2 4 8 16 32 frames per second rendered -THRESHOLDSET=${4:-1024} # byte difference 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 between last two picture frames taken +[[ -n "${1:-}" ]] && [[ "${1//-}" = [Hh]* ]] && { printf '%s\n' "Help for script '${0##*/}':" && TSFILE="($(grep '##\ ' "$0"))" && printf '%s\n%s\n' "$(for i in "${TSFILE[@]}" ; do sed -e "s/##/ /" <<< "$i" | cut -f 2 ; done | cut -d\) -f1)" "Help for script '${0##*/}': DONE" ; exit ; } +CAMID=${1:-0} ## [1] default 0: One camera 0 1 2 3 4 5 6 7 id, +FRAMECTOT=${2:-255} ## [2] default 256: Video frame count total, +FRAMERATE=${3:-16} ## [3] default 16: Video 2 4 8 16 32 frames per second rendered in the mpg file, +THRESHOLDSET=${4:-2048} ## [4] default 2048: Byte difference 64 128 256 512 1024 2048 4096 8192 16384 32768 65536 between last two picture frames taken; Used for motion detection. The greater the number, the lesser the motion sensitivity. Camera resolution also affects this argument, _CAMS_ () { - FRAMECOUNT=0 - while [ "$FRAMECOUNT" -le "$FRAMECTOT" ] - do - FRAMENAME="$(printf '%s.%04d.jpg' "${PWD##*/}$CAMID" "$FRAMECOUNT")" - printf '%s\n%s\n' "I $((FRAMECOUNT + 1))/$((FRAMECTOT + 1)) frame count; ${THRESHOLDSET:-} threshold" "I $CAMID camid taking picture $FRAMENAME" - sleep 0.0"$(shuf -i 101-420 -n 1)" - "$PREFIX"/libexec/termux-api CameraPhoto --es camera "$CAMID" --es file "$PWD/$FRAMENAME" - _ISZERO_ - done +FRAMECOUNT=0 +while [ "$FRAMECOUNT" -le "$FRAMECTOT" ] +do +FRAMENAME="$(printf '%s.%04d.jpg' "${PWD##*/}$CAMID" "$FRAMECOUNT")" +printf '%s\n%s\n' "I $((FRAMECOUNT + 1))/$((FRAMECTOT + 1)) frame count: ${THRESHOLDSET:-} threshold set" "I $CAMID camid taking picture $FRAMENAME" +sleep 0.0"$(shuf -i 101-420 -n 1)" +"${PREFIX:-/data/data/com.termux/files/usr}"/libexec/termux-api CameraPhoto --es camera "$CAMID" --es file "$PWD/$FRAMENAME" +_ISZERO_ "$@" +done } _CHECKMOTIONDIFF_() { - if [ "$FRAMECOUNT" -ne 0 ] - then - THRESHOLD="$((LASTZERO - ISZERO))" - THRESHOLD="${THRESHOLD//-}" - if [ "$THRESHOLD" -le "$THRESHOLDSET" ] - then - printf '%s\n%s\n' "D $THRESHOLD threshold; deleting file $FRAMENAME" "I frame $FRAMENAME; Threshold set to $THRESHOLDSET" - rm -f "$FRAMENAME" - else - printf '%s\n' "S $THRESHOLD threshold; saving file $FRAMENAME" - FRAMECOUNT="$((FRAMECOUNT + 1))" - fi - else - FRAMECOUNT="$((FRAMECOUNT + 1))" - fi +if [ "$FRAMECOUNT" -ne 0 ] +then +THRESHOLD="$((LASTZERO - ISZERO))" +THRESHOLD="${THRESHOLD//-}" +if [ "$THRESHOLD" -le "$THRESHOLDSET" ] +then +printf '%s\n%s\n' "D $THRESHOLD threshold: deleting file $FRAMENAME" "I frame $FRAMENAME: Threshold set to $THRESHOLDSET" +rm -f "$FRAMENAME" +else +printf '%s\n' "S $THRESHOLD threshold: saving file $FRAMENAME" +FRAMECOUNT="$((FRAMECOUNT + 1))" +fi +else +FRAMECOUNT="$((FRAMECOUNT + 1))" +fi } _ISZERO_ () { - if [ -n "${ISZERO:-}" ] - then - LASTZERO="$ISZERO" - fi - ISZERO="$(find . -type f -name "$FRAMENAME" -printf "%s\n" )" - if [ "$ISZERO" -eq 0 ] - then - if [ "$FRAMECOUNT" -eq 0 ] - then - printf '%s\n' "E Could not begin shoot; Please check for issues and run '${0##*/}' again; Exiting..." - rm -f "$FRAMENAME" - exit 1 - else - printf '%s\n' "W deleting zero size file $FRAMENAME" - rm -f "$FRAMENAME" - fi - else - _CHECKMOTIONDIFF_ - fi +if [ -n "${ISZERO:-}" ] +then +LASTZERO="$ISZERO" +fi +sleep 0.0"$(shuf -i 420-640 -n 1)" +ISZERO="$(find . -type f -name "$FRAMENAME" -printf "%s" || ls -al "$FRAMENAME" | awk '{print $5}')" +if [ "$ISZERO" -eq 0 ] +then +if [ "$FRAMECOUNT" -eq 0 ] +then +printf '%s\n%s\n' "E could not begin shoot: ERROR" "Please check for issues and run '${0##*/}' again: Exiting..." +rm -f "$FRAMENAME" +exit 1 +else +printf '%s\n' "W deleting zero size file $FRAMENAME" +rm -f "$FRAMENAME" +fi +else +_CHECKMOTIONDIFF_ +fi +_MAGICKCK_ "$@" } -_CAMS_ +_MAGICKCK_ () { +if [ -e "$FRAMENAME" ] +then +printf '%s' "I checking file $FRAMENAME for errors: " +MAGICKCK="$(nice -n 20 magick identify "$FRAMENAME" 2>&1 ||:)" +if grep -i error <<< "$MAGICKCK" +then +rm -f "$FRAMENAME" +FRAMECOUNT="$((FRAMECOUNT - 1))" +printf '%s\n%s\n%s\n' "DONE" "D deleted file $FRAMENAME: ERROR" "I redoing file $FRAMENAME: ERROR" +else +printf '%s\n' "DONE" +if [ -n "${5:-}" ] +then +if [[ "${5//-}" = [Rr]* ]] ## [5] default no rotation: R|r[otate]: you can use R or r to activate rotation which is preset to 90° rotation. The rotation option can also be applied as a 90° rotation preset in arguments 1 and 2 in which case their default values will become presets, +then +printf '%s' "I rotating file $FRAMENAME by ${6:-90}°: " ## [6] default 90°: Enter desired picture rotation angle in digits if you do not want to use the 90° default rotation, +nice -n 20 magick "$FRAMENAME" -rotate "${6:-90}" "$FRAMENAME".jpg +mv "$FRAMENAME".jpg "$FRAMENAME" +printf '%s\n' "DONE" +fi +fi +fi +fi +} +_MEFFMPEG_ () { printf '%s\n' "I making $VIDEOPREFIX$TIMESTAMP.mp4: This job will complete in the background..." && nice -n 20 ffmpeg -framerate "$FRAMERATE" -i "$VIDEOPREFIX"%04d.jpg "$VIDEOPREFIX$TIMESTAMP".mp4 && { ls -al "$VIDEOPREFIX$TIMESTAMP".mp4 && printf '%s\n' "I done making $VIDEOPREFIX$TIMESTAMP.mp4: DONE" ; } || printf '%s\n' "E creating file $VIDEOPREFIX$TIMESTAMP.mp4: ERROR" ; } +_CAMS_ "$@" VIDEOPREFIX="${FRAMENAME%%.*}." -TIMESTAMP="$(date +%Y%m%d%H%M)" -printf '%s\n' "Making $VIDEOPREFIX$TIMESTAMP.mp4; Please wait..." -nice -n 20 ffmpeg -threads 1 -loglevel -8 -framerate "$FRAMERATE" -i "$VIDEOPREFIX"%04d.jpg -vcodec libx264 -acodec aac "$VIDEOPREFIX$TIMESTAMP".mp4 && printf '%s\n' "Finished making $VIDEOPREFIX$TIMESTAMP.mp4:" -ls -al "$VIDEOPREFIX$TIMESTAMP".mp4 -## termux-camera-record EOF +TIMESTAMP="$(date +%Y%m%d%H%M%S)" +_MEFFMPEG_ & +sleep "${7:-2}.$(shuf -i 420-640 -n 1)" ## [7] default ~2 seconds: Time before script exits; Program ffmpeg will continue to run on in the background until its job of producing an mp4 file ends. This sleep is so the jpg files can be read by ffmpeg when this script is used within a loop. +# termux-camera-record EOF